236 lines
6.0 KiB
PHP
236 lines
6.0 KiB
PHP
<?php
|
|
class Download_aol extends MY_Controller
|
|
{
|
|
function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->baseUrl = "https://mobile.pramita.co.id/one_api_coba/antrian_online/downloader/";
|
|
}
|
|
function index()
|
|
{
|
|
|
|
//branch
|
|
$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 . {$this->db->last_query}";
|
|
exit();
|
|
}
|
|
$rows = $qry->result_array();
|
|
if (count($rows) == 0) {
|
|
$this->log("ERR : No Default Branch");
|
|
exit();
|
|
}
|
|
$branchID = $rows[0]["M_BranchID"];
|
|
$url = $this->baseUrl . "download/$branchID";
|
|
$resp = $this->get($url);
|
|
if ($resp["status"] != "OK") {
|
|
echo json_encode($resp);
|
|
exit;
|
|
}
|
|
$total = 0;
|
|
foreach ($resp["data"] as $data) {
|
|
$total++;
|
|
$this->db->trans_begin();
|
|
$queueID = $data["queueID"];
|
|
$queue = [];
|
|
$subservice = [];
|
|
$patient = [];
|
|
foreach ($data as $k => $v) {
|
|
if (strpos($k, "queue") === 0) {
|
|
$queue[$k] = $v;
|
|
}
|
|
if (strpos($k, "M_Patient") === 0) {
|
|
$patient[$k] = $v;
|
|
}
|
|
if (strpos($k, "subService") === 0) {
|
|
$subservice[$k] = $v;
|
|
}
|
|
}
|
|
//insert into antrian_online.queue
|
|
$resp = $this->insert_or_update("antrian_online.queue", $queue, ["queueID"]);
|
|
if ($resp["status"] != "OK") {
|
|
echo json_encode($resp);
|
|
exit;
|
|
}
|
|
//insert or update antrian_online.subservice
|
|
$resp = $this->insert_or_update(
|
|
"antrian_online.subservice",
|
|
$subservice,
|
|
["subServiceID"]
|
|
);
|
|
if ($resp["status"] != "OK") {
|
|
echo json_encode($resp);
|
|
exit;
|
|
}
|
|
//insert or update antrian_online.m_patient
|
|
$resp = $this->insert_or_update(
|
|
"antrian_online.m_patient",
|
|
$patient,
|
|
["M_PatientID"]
|
|
);
|
|
if ($resp["status"] != "OK") {
|
|
echo json_encode($resp);
|
|
exit;
|
|
}
|
|
// post to update
|
|
$prm = ["queueID" => $queueID, "status" => "Y"];
|
|
$url = $this->baseUrl . "/update";
|
|
$resp = $this->post($url, json_encode($prm));
|
|
if ($resp["status"] == "OK") {
|
|
$this->db->trans_commit();
|
|
} else {
|
|
$this->db->trans_rollback();
|
|
echo json_encode($resp);
|
|
exit;
|
|
}
|
|
}
|
|
echo json_encode(["status" => "OK", "message" => "$total Queue Downloaded"]);
|
|
}
|
|
function insert_or_update($table, $dt, $keys)
|
|
{
|
|
$s_where = "";
|
|
$param = [];
|
|
foreach ($keys as $k) {
|
|
if ($s_where != "") {
|
|
$s_where .= " and ";
|
|
}
|
|
$s_where .= " $k = ?";
|
|
$param[] = $dt[$k];
|
|
}
|
|
$sql = "select count(*) as total
|
|
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) {
|
|
if ($rows[0]["total"] > 0) {
|
|
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 {
|
|
//insert
|
|
$qry = $this->db->insert($table, $dt);
|
|
if (!$qry) {
|
|
return [
|
|
"status" => "ERR",
|
|
"message" =>
|
|
"ERR Insert : " .
|
|
$this->db->error()["message"] .
|
|
"|" .
|
|
$this->db->last_query(),
|
|
];
|
|
}
|
|
}
|
|
} else {
|
|
//insert
|
|
$qry = $this->db->insert($table, $dt);
|
|
if (!$qry) {
|
|
return [
|
|
"status" => "ERR",
|
|
"message" =>
|
|
"ERR Insert : " .
|
|
$this->db->error()["message"] .
|
|
"|" .
|
|
$this->db->last_query(),
|
|
];
|
|
}
|
|
}
|
|
return ["status" => "OK", "message" => $status];
|
|
}
|
|
function log($msg)
|
|
{
|
|
echo "{$this->now()} $msg\n";
|
|
}
|
|
function get($url)
|
|
{
|
|
$ch = curl_init($url);
|
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
|
|
$result = curl_exec($ch);
|
|
if (curl_errno($ch) > 0) {
|
|
return [
|
|
"status" => "ERR",
|
|
"message" => curl_error($ch),
|
|
];
|
|
}
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
if ($httpCode != 200) {
|
|
return [
|
|
"status" => "ERR",
|
|
"message" => "Http Response : $httpCode",
|
|
];
|
|
}
|
|
$j_result = json_decode($result, true);
|
|
if (!$j_result) {
|
|
return [
|
|
"status" => "ERR",
|
|
"message" => "JSON invalid: $result",
|
|
];
|
|
}
|
|
return $j_result;
|
|
}
|
|
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, 120);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
"Content-Type: application/octet-stream",
|
|
"Content-Length: " . strlen($data),
|
|
]);
|
|
$result = curl_exec($ch);
|
|
if (curl_errno($ch) > 0) {
|
|
return [
|
|
"status" => "ERR",
|
|
"message" => curl_error($ch),
|
|
];
|
|
}
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
if ($httpCode != 200) {
|
|
return [
|
|
"status" => "ERR",
|
|
"message" => "Http Response : $httpCode",
|
|
];
|
|
}
|
|
$j_result = json_decode($result, true);
|
|
if (!$j_result) {
|
|
return [
|
|
"status" => "ERR",
|
|
"message" => "JSON invalid: $result",
|
|
];
|
|
}
|
|
return $j_result;
|
|
}
|
|
}
|