Files
BE_IBL/application/controllers/tools/regonline/Demolabku_download.php
2026-04-15 15:23:57 +07:00

343 lines
12 KiB
PHP

<?php
class Demolabku_download extends MY_Controller
{
public function __construct()
{
parent::__construct();
$this->debug = false;
$this->ONLINE_USER_ID = 1500;
$this->SENDER_DOCTOR_ID = 0;
$this->SENDER_ADDRESS_ID = 0;
$this->PJ_DOCTOR_ID = 0;
$this->KASIR_ONLINE_USER = 1500;
$this->url = "https://sasmobile.aplikasi.web.id/one_api_coba/tools/regonline/r_download/";
$this->url_v2 = "https://sasmobile.aplikasi.web.id/one_api_coba/tools/regonline/r_download_v2/";
}
public function now()
{
return Date("Y-m-d H:i:s");
}
public function get_tx_id($orgID)
{
$result = 0;
$sql = "select T_OnlineTxID
from t_onlinetx
where T_OnlineTxOrgID=?";
$qry = $this->db->query($sql, array($orgID));
if (!$qry) {
echo "{$this->now()} ERR Exist Tx : {$this->db->error()['message']}\n";
exit;
}
$rows = $qry->result_array();
if (count($rows) > 0) {
return $rows[0]["T_OnlineTxID"];
}
return $result;
}
public function tx_ehac($tx, $txID)
{
$order = $tx["order"];
$sum_ehac = 0;
foreach ($order as $o) {
$sum_ehac += $o["T_OrderEHAC"];
}
$arr = [
"T_OnlineTxID" => $txID,
"T_OnlineTxEhacFee" => $sum_ehac,
];
$qry = $this->db->where("T_OnlineTxID", $txID);
$qry = $this->db->update("t_onlinetx", $arr);
if (!$qry) {
return [false, $this->db->error()["message"]];
}
return [true, ""];
}
public function strip_unicode($inp)
{
$result = mb_convert_encoding($inp, "US-ASCII", "UTF-8");
return $result;
}
public function index()
{
echo "{$this->now()} Start Download Online Order [Duitku]\n";
$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";
exit();
}
$rows = $qry->result_array();
if (count($rows) == 0) {
echo "{$this->now()} ERR : No Default Branch\n";
exit();
}
$branchID = $rows[0]["M_BranchID"];
$date = date("Y-m-d H:i:s");
$url = $this->url;
$param = ["branchID" => $branchID, "date" => $date];
$z_param = gzdeflate(json_encode($param), 9);
$resp = $this->post($url, $z_param);
if ($resp["status"] == "OK") {
$this->db->trans_begin();
$db_msg = "";
$tx_counter = 0;
$order_counter = 0;
foreach ($resp["txs"] as $tx) {
$ref = "";
foreach ($tx["duitku"] as $d) {
if ($ref != "") {
$ref .= "|";
}
$ref .=
$d["duitkuCbReference"] .
"^" .
$d["duitkuCbPaymentResultCode"];
}
$tx_order = $tx["order"];
$px_name = "";
$px_qrcode = "";
$px_date = "";
$arr_px_date = [];
foreach ($tx_order as $txo) {
if ($px_date != "") {
$px_date .= ",";
}
$x_date = $txo["T_OrderDate"] . " " . $txo["T_OrderTime"];
if (strtotime($x_date)) {
$px_date .= date("d-m-Y H:i", strtotime($x_date));
} else {
$px_date .=
$txo["T_OrderDate"] . " " . $txo["T_OrderTime"];
}
if ($px_qrcode != "") {
$px_qrcode .= ",";
}
$px_qrcode .= $txo["T_OrderQrCode"];
if ($px_name != "") {
$px_name .= ",";
}
$px_name .= $txo["patient"]["M_PatientName"];
$arr_px_date[] = $px_date;
}
$dt = [
"T_OnlineTxOrgID" => $tx["T_TransactionID"],
"T_OnlineTxDate" => $tx["T_TransactionDate"],
"T_OnlineTxNumbering" => $tx["T_TransactionNumbering"],
"T_OnlineTxTotal" => $tx["T_TransactionTotal"],
"T_OnlineTxFee" => $tx["T_TransactionFee"],
"T_OnlineTxPgReference" => $ref,
"T_OnlineTxJsonGz" => gzdeflate(json_encode($tx), 9),
"T_OnlineTxBookingQrCode" => $px_qrcode,
"T_OnlineTxBookingDate" => $px_date,
"T_OnlineTxBookingName" => $px_name,
"T_OnlineTxConfig" => json_encode($tx["config"]),
];
if ($this->exists_tx($tx["T_TransactionID"])) {
$this->db->where("T_OnlineTxOrgID", $tx["T_TransactionID"]);
$qry = $this->db->update("t_onlinetx", $dt);
$tOnlineTxID = $this->get_tx_id($tx["T_TransactionID"]);
} else {
$qry = $this->db->insert("t_onlinetx", $dt);
$tOnlineTxID = $this->db->insert_id();
}
if (!$qry) {
$db_msg .=
"|Insert T_OnlineTx " . $this->db->error()["message"];
$db_msg .= $this->db->last_query();
}
list($statusEhac, $db_msg) = $this->tx_ehac($tx, $tOnlineTxID);
$tx_counter++;
}
if ($db_msg != "") {
$this->db->trans_rollback();
echo "{$this->now()} ERR : {$db_msg}\n";
exit();
}
if ($this->db->trans_status === false) {
$this->db->trans_rollback();
echo "{$this->now()} ERR : {$db_msg}\n";
} else {
$this->db->trans_commit();
echo "{$this->now()} Downloaded $tx_counter Transaction\n";
$this->download_bayar_ditempat();
}
} else {
echo "{$this->now()} ERR : {$resp["message"]}\n";
}
}
function download_bayar_ditempat()
{
echo "{$this->now()} Start Download Online Order [Bayar di tempat]\n";
$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";
exit();
}
$rows = $qry->result_array();
if (count($rows) == 0) {
echo "{$this->now()} ERR : No Default Branch\n";
exit();
}
$branchID = $rows[0]["M_BranchID"];
$date = date("Y-m-d H:i:s");
$url = $this->url_v2;
$param = ["branchID" => $branchID, "date" => $date];
$z_param = gzdeflate(json_encode($param), 9);
$resp = $this->post($url, $z_param);
if ($resp["status"] == "OK") {
$this->db->trans_begin();
$db_msg = "";
$tx_counter = 0;
$order_counter = 0;
foreach ($resp["txs"] as $tx) {
$ref = "";
foreach ($tx["duitku"] as $d) {
if ($ref != "") {
$ref .= "|";
}
$ref .=
$d["duitkuCbReference"] .
"^" .
$d["duitkuCbPaymentResultCode"];
}
$tx_order = $tx["order"];
$px_name = "";
$px_qrcode = "";
$px_date = "";
foreach ($tx_order as $txo) {
if ($px_date != "") {
$px_date .= ",";
}
$x_date = $txo["T_OrderDate"] . " " . $txo["T_OrderTime"];
if (strtotime($x_date)) {
$px_date .= date("d-m-Y H:i", strtotime($x_date));
} else {
$px_date .=
$txo["T_OrderDate"] . " " . $txo["T_OrderTime"];
}
if ($px_qrcode != "") {
$px_qrcode .= ",";
}
$px_qrcode .= $txo["T_OrderQrCode"];
if ($px_name != "") {
$px_name .= ",";
}
$px_name .= $txo["patient"]["M_PatientName"];
}
$dt = [
"T_OnlineTxOrgID" => $tx["T_TransactionID"],
"T_OnlineTxDate" => $tx["T_TransactionDate"],
"T_OnlineTxNumbering" => $tx["T_TransactionNumbering"],
"T_OnlineTxTotal" => $tx["T_TransactionTotal"],
"T_OnlineTxFee" => $tx["T_TransactionFee"],
"T_OnlineTxPgReference" => $ref,
"T_OnlineTxJsonGz" => gzdeflate(json_encode($tx), 9),
"T_OnlineTxBookingQrCode" => $px_qrcode,
"T_OnlineTxBookingDate" => $px_date,
"T_OnlineTxBookingName" => $px_name,
"T_OnlineTxConfig" => json_encode($tx["config"]),
];
if ($this->exists_tx($tx["T_TransactionID"])) {
$this->db->where("T_OnlineTxOrgID", $tx["T_TransactionID"]);
$qry = $this->db->update("t_onlinetx", $dt);
$tOnlineTxID = $this->get_tx_id($tx["T_TransactionID"]);
} else {
$qry = $this->db->insert("t_onlinetx", $dt);
$tOnlineTxID = $this->db->insert_id();
}
if (!$qry) {
$db_msg .=
"|Insert T_OnlineTx " . $this->db->error()["message"];
$db_msg .= $this->db->last_query();
}
list($statusEhac, $db_msg) = $this->tx_ehac($tx, $tOnlineTxID);
$tx_counter++;
}
if ($db_msg != "") {
$this->db->trans_rollback();
echo "{$this->now()} ERR : {$db_msg}\n";
exit();
}
if ($this->db->trans_status === false) {
$this->db->trans_rollback();
echo "{$this->now()} ERR : {$db_msg}\n";
} else {
$this->db->trans_commit();
echo "{$this->now()} Downloaded $tx_counter Transaction\n";
}
} else {
echo "{$this->now()} ERR : {$resp["message"]}\n";
}
}
public function post($url, $data)
{
echo "{$this->now()} DEBUG Connect to : $url\n";
$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/json",
"Content-Length: " . strlen($data),
]);
$z_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 | $z_result",
];
}
$result = gzinflate($z_result);
$j_result = json_decode($result, true);
if (!$j_result) {
return [
"status" => "ERR",
"message" => "JSON invalid: $z_result",
];
}
return $j_result;
}
public function exists_tx($txID)
{
$sql = "select count(*) total
from t_onlinetx
where T_OnlineTxOrgID=?";
$qry = $this->db->query($sql, [$txID]);
if (!$qry) {
echo "{$this->now()} ERR Exist Tx : {$this->db->error()["message"]}\n";
exit();
}
$rows = $qry->result_array();
return $rows[0]["total"] > 0;
}
}