Files
BE_CPONE/application/controllers/hs/Download_hs.php
2026-04-27 10:26:26 +07:00

1002 lines
32 KiB
PHP

<?php
ini_set("display_errors", "1");
ini_set("display_startup_errors", "1");
error_reporting(E_ALL);
// drop table ol_t_transaction; create table ol_t_transaction like t_transaction;
// drop table ol_t_transactionHS; create table ol_t_transactionHS like t_transactionHS;
// drop table ol_t_order; create table ol_t_order like t_order;
// drop table ol_t_orderdetail; create table ol_t_orderdetail like t_orderdetail;
// drop table ol_m_patient; create table ol_m_patient like m_patient;
// drop table ol_m_patientaddress; create table ol_m_patientaddress like m_patientaddress;
// alter table m_patient add M_PatientOldID int ; add key(M_PatientOldID);
// alter table m_patientaddress add M_PatientAddressOldID int; add key(M_PatientAddressOldID);
// alter table ol_m_patient add M_PatientOldID int , add key(M_PatientOldID);
// alter table ol_m_patientaddress add M_PatientAddressOldID int, add key(M_PatientAddressOldID);
// alter table hs_address add HS_AddressOldID int after HS_AddressID, add key (HS_AddressOldID);
class Download_hs extends MY_Controller
{
var $base_url;
function __construct()
{
parent::__construct();
$this->debug = false;
$this->base_url = "https://mobile.pramita.co.id/";
}
function now()
{
return Date("Y-m-d H:i:s");
}
function log($msg)
{
echo "{$this->now()} $msg\n";
}
function duitku_cb($debug = "")
{
//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->base_url}one-api/hs/r_duitku/download/$branchID";
$this->log("Download Duitku CallBack from $url");
$jresp = file_get_contents($url);
$resp = json_decode($jresp, true);
if ($resp["status"] != "OK") {
$this->log("ERR Download " . $resp["message"]);
exit();
}
$md5 = $resp["md5"];
$jdata = gzuncompress(base64_decode($resp["data"]));
$l_md5 = md5($jdata);
if ($md5 != $l_md5) {
$this->log(" ERR mD5 $l_md5 <> $md5");
exit();
}
$data = json_decode($jdata, true);
if (count($data) > 0) {
//insert into one_hs.duitku_cb
$this->db->trans_begin();
$tot_row = 0;
$tot_dl = 0;
foreach ($data as $r) {
$sql = "select duitkuCbID
from
one_hs.duitku_cb where duitkuCbID =?";
$id = $r["duitkuCbID"];
$qry = $this->db->query($sql, [$id]);
if (!$qry) {
$this->log("Err ", $this->db->error()["message"]);
$this->db->trans_rollback();
exit;
}
$rows = $qry->result_array();
if (count($rows) == 0) {
$qry = $this->db->insert("one_hs.duitku_cb", $r);
if (!$qry) {
$this->log("Err " . $this->db->error()["message"]);
$this->db->trans_rollback();
exit;
}
$tot_row++;
}
$tot_dl++;
}
$this->db->trans_commit();
$this->log("Done $tot_dl downloaded, $tot_row inserted.");
}
// process duitku_cb
$sql = "select * from one_hs.duitku_cb where duitkuCbIsProcessed = 'N'";
$qry = $this->db->query($sql);
if (!$qry) {
$this->log("Err ", $this->db->error()["message"]);
$this->db->trans_rollback();
exit;
}
$arr_id = [];
$rows = $qry->result_array();
$this->db->trans_begin();
foreach ($rows as $r) {
$sql = "update one_hs.t_transaction
set T_TransactionIsLunas = 'Y' where
T_TransactionNumbering = ? and T_TransactionIsLunas = 'N'";
$orderID = $r["duitkuCbMerchantOrderId"];
$qry = $this->db->query($sql, [$orderID]);
if (!$qry) {
$this->log("Err ", $this->db->error()["message"]);
$this->db->trans_rollback();
exit;
}
$arr_id[] = $r["duitkuCbID"];
}
if (count($arr_id) > 0) {
$s_ids = implode(",", $arr_id);
$sql = "update one_hs.duitku_cb set duitkuCbIsProcessed = 'Y'
where duitkuCbID in ($s_ids)";
$qry = $this->db->query($sql);
if (!$qry) {
$this->log("Err ", $this->db->error()["message"]);
$this->db->trans_rollback();
exit;
}
$this->log("Done Update one_hs duitku_cb ". count($arr_id));
}
$this->db->trans_commit();
}
function force($trNo, $debug = "")
{
//hs_upload
$this->log("Force Download Online HS Transaction $trNo");
$this->db->trans_begin();
//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->base_url}one-api/hs/r_hs/force_download/$branchID/$trNo";
$this->log("Download from $url");
$jresp = file_get_contents($url);
$resp = json_decode($jresp, true);
if ($resp["status"] != "OK") {
$this->log("ERR Download " . $resp["message"]);
exit();
}
if ($resp["total"] == 0) {
$this->log(" No Pending Transaction.");
exit();
}
$md5 = $resp["md5"];
$jdata = gzuncompress(base64_decode($resp["data"]));
$l_md5 = md5($jdata);
if ($md5 != $l_md5) {
$this->log(" ERR mD5 $l_md5 <> $md5");
exit();
}
$data = json_decode($jdata, true);
if ($debug != "") {
print_r($data);
}
foreach ($data as $table => $rows) {
$this->log(" Insert Update ol_$table");
$count = 0;
foreach ($rows as $r) {
$count++;
if ($table == "t_transaction") {
unset($r["T_TransactionIsHS"]);
$r["T_TransactionLastUpdated"] = date("Y-m-d H:i:s");
}
if ($table == "t_transactionHS") {
$r["t_transactionHSLastUpdated"] = date("Y-m-d H:i:s");
}
if ($table == "t_order") {
$r["T_OrderLastUpdated"] = date("Y-m-d H:i:s");
}
if ($table == "t_orderdetail") {
//saat download di skip
//di pakai saat upload
unset($r["T_OrderDetailIsAdditional"]);
$r["T_OrderDetailLastUpdated"] = date("Y-m-d H:i:s");
}
if ($table == "t_orderdeliveries") {
$r["T_OrderDeliveriesLastUpdated"] = date("Y-m-d H:i:s");
}
if ($table == "m_patient") {
$r["M_PatientLastUpdated"] = date("Y-m-d H:i:s");
}
if ($table == "m_patientaddress") {
$r["M_PatientAddressLastUpdated"] = date("Y-m-d H:i:s");
}
list($xstatus, $message) = $this->updateOnDuplicate(
"one_hs.ol_{$table}",
$r
);
if (!$xstatus) {
$this->log(" Error one_hs.ol_{$table} | " . $message);
$this->db->trans_rollback();
exit();
}
}
$this->log(" $count records");
}
$tx_ids = [];
foreach ($data["t_transaction"] as $r) {
$tx_ids[] = $r["T_TransactionID"];
}
$this->log("Force Download OK");
$this->db->trans_commit();
$this->insert_ol_table();
//rollback for ol table
$this->db->trans_commit();
}
public function index()
{
//hs_upload
$this->log("Download Online HS Transaction");
$this->db->trans_begin();
//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->base_url}one-api/hs/r_hs/download/$branchID";
$this->log("Download from $url");
$jresp = file_get_contents($url);
$resp = json_decode($jresp, true);
if ($resp["status"] != "OK") {
$this->log("ERR Download " . $resp["message"]);
exit();
}
if ($resp["total"] == 0) {
$this->log(" No Pending Transaction.");
exit();
}
$md5 = $resp["md5"];
$jdata = gzuncompress(base64_decode($resp["data"]));
$l_md5 = md5($jdata);
if ($md5 != $l_md5) {
$this->log(" ERR mD5 $l_md5 <> $md5");
exit();
}
$data = json_decode($jdata, true);
//insert data here ...
foreach ($data as $table => $rows) {
$this->log(" Insert Update ol_$table");
$count = 0;
foreach ($rows as $r) {
$count++;
if ($table == "t_transaction") {
unset($r["T_TransactionIsHS"]);
$r["T_TransactionLastUpdated"] = date("Y-m-d H:i:s");
$this->log(
" T_TransactionNumber : " . $r["T_TransactionNumbering"]
);
}
if ($table == "t_transactionHS") {
$r["t_transactionHSLastUpdated"] = date("Y-m-d H:i:s");
}
if ($table == "t_order") {
$r["T_OrderLastUpdated"] = date("Y-m-d H:i:s");
}
if ($table == "t_orderdetail") {
//saat download di skip
//di pakai saat upload
unset($r["T_OrderDetailIsAdditional"]);
$r["T_OrderDetailLastUpdated"] = date("Y-m-d H:i:s");
}
if ($table == "t_orderdeliveries") {
$r["T_OrderDeliveriesLastUpdated"] = date("Y-m-d H:i:s");
}
if ($table == "m_patient") {
$r["M_PatientLastUpdated"] = date("Y-m-d H:i:s");
}
if ($table == "m_patientaddress") {
$r["M_PatientAddressLastUpdated"] = date("Y-m-d H:i:s");
}
if ($table == "hs_address") {
$r["HS_AddressLastUpdated"] = date("Y-m-d H:i:s");
}
list($xstatus, $message) = $this->updateOnDuplicate(
"one_hs.ol_{$table}",
$r
);
if (!$xstatus) {
$this->log(" Error one_hs.ol_{$table} | " . $message);
$this->db->trans_rollback();
exit();
}
}
$this->log(" $count records");
}
$tx_ids = [];
foreach ($data["t_transaction"] as $r) {
$tx_ids[] = $r["T_TransactionID"];
}
if (count($tx_ids) > 0) {
$param = ["branchID" => $branchID, "transactionID" => $tx_ids];
$this->log(" Update T_Transaction Download Status");
$url = "{$this->base_url}one-api/hs/r_hs/update_status";
$jparam = json_encode($param);
$zparam = gzcompress($jparam);
$resp = $this->post($url, $zparam);
if ($resp["status"] != "OK") {
$this->log(" ERR update status " . $resp["message"]);
exit();
}
}
$this->log("Download OK");
$this->db->trans_commit();
$arrTxIds = $this->insert_ol_table();
//rollback for ol table
$this->db->trans_commit();
//Add whatsapp notif
if (count($arrTxIds) > 0) {
foreach ($arrTxIds as $tx_id) {
$add_whatsapp_url = "http://localhost/one-api/hs/new_order_whatsapp/add_order/" . $tx_id;
$resp = $this->get($add_whatsapp_url);
if ($resp["status"] != "OK") {
$this->log(" ERR Add Whatsapp Order $add_whatsapp_url" . $resp["message"]);
} else {
$this->log(" Add Whatsapp Order $add_whatsapp_url [OK]");
}
}
}
$this->fix_thsaddr();
$this->duitku_cb();
}
function get_count($table, $field, $value)
{
$sql = "select count(*) total from $table where $field = '$value'";
$qry = $this->db->query($sql);
if (!$qry) {
return [false, 0, $this->db->error()["message"]];
}
return [true, $qry->result_array()[0]["total"], ""];
}
function fix_thsaddr()
{
//cari 7 hari terakhir
$sql = "select t.t_transactionHSID, t.t_transactionHSOldID,
t.t_transactionHSHS_AddressID, ol.t_transactionHSID olID ,
ol.t_transactionHSHS_AddressID olAddressID,
HS_AddressID newAddressID,
HS_AddressOldID oldAddressID
from one_hs.t_transactionHS t
join one_hs.ol_t_transactionHS ol
on t.t_transactionHSHS_AddressID = 0
and t.T_TransactionHSIsActive = 'Y'
and t.t_transactionHSLastUpdated + interval 7 day > now()
and t.t_transactionHSOldID = ol.t_transactionHSID
left join one_hs.hs_address
on ol.t_transactionHSHS_AddressID = HS_AddressOldID
and HS_AddressIsActive = 'Y'
";
$qry = $this->db->query($sql);
if (!$qry) {
echo "{$this->now()} ERR : {$this->db->error()["message"]}\n . {$this->db->last_query} \n";
exit;
}
$rows = $qry->result_array();
$sql = "update one_hs.t_transactionHS
set t_transactionHSHS_AddressID = ?
where t_transactionHSID = ?";
$this->db->trans_begin();
$total = count($rows);
foreach ($rows as $r) {
$newAddressID = $r["newAddressID"];
$thsID = $r["t_transactionHSID"];
$qry = $this->db->query($sql, [$newAddressID, $thsID]);
if (!$qry) {
echo "{$this->now()} ERR : {$this->db->error()["message"]}\n . {$this->db->last_query} \n";
$this->db->trans_rollback();
exit;
}
}
echo "{$this->now()} OK : Fix $total t_transactionHSHS_AddressID \n";
$this->db->trans_commit();
}
function insert_ol_table($stand_alone = "", $debug = "x")
{
$this->db->trans_begin();
$this->log(" Inserting ol_t_transaction ");
$sql =
"select * from one_hs.ol_t_transaction where T_TransactionOldID = 0 or T_TransactionOldID is null";
$qry = $this->db->query($sql);
if (!$qry) {
$this->log(
" ERR insert ol_t_transaction" . $this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
$arrTxIds = [];
foreach ($qry->result_array() as $r) {
$oldID = $r["T_TransactionID"];
unset($r["T_TransactionID"]);
$r["T_TransactionOldID"] = $oldID;
$this->log(" one_hs.t_transaction $oldID ");
$qry = $this->db->insert("one_hs.t_transaction", $r);
if (!$qry) {
$this->log(
" ERR insert one_hs.ol_t_transaction" .
$this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
$arrTxIds[] = $this->db->insert_id();
$sql =
"update one_hs.ol_t_transaction set T_TransactionOldID = ? where T_TransactionID = ?";
$qry = $this->db->query($sql, [$oldID, $oldID]);
if (!$qry) {
$this->log(
" ERR update one_hs.ol_t_transaction" .
$this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
}
//hs_address
$this->log(" Inserting ol_hs_address");
$sql =
"select * from one_hs.ol_hs_address where HS_AddressOldID = 0 or HS_AddressOldID is null ";
$qry = $this->db->query($sql);
if (!$qry) {
$this->log(
" ERR insert ol_hs_address " . $this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
$mapHsAddress = [];
foreach ($qry->result_array() as $r) {
$oldID = $r["HS_AddressID"];
list($c_status, $c_count, $msg) = $this->get_count(
"one_hs.hs_address",
"HS_AddressOldID",
$oldID
);
if (!$c_status) {
$this->log(
" ERR get existing one_hs.hs_address " .
$this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
if ($c_count == 0) {
unset($r["HS_AddressID"]);
$r["HS_AddressOldID"] = $oldID;
$this->log(" one_hs.hs_address $oldID ");
$qry = $this->db->insert("one_hs.hs_address", $r);
if (!$qry) {
$this->log(
" ERR insert one_hs.hs_address " .
$this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
$newHSAddressID = $this->db->insert_id();
$mapHsAddress[$oldID] = $newHSAddressID;
}
$sql =
"update one_hs.ol_hs_address set HS_AddressOldID = ? where HS_AddressID = ?";
$qry = $this->db->query($sql, [$oldID, $oldID]);
if (!$qry) {
$this->log(
" ERR update one_hs.ol_hs_address " .
$this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
}
//re-create mapHsAddress
/* $sql = "select HS_AddressID, HS_AddressOldID from one_hs.hs_address */
/* where HS_AddressLastUpdated + interval 3 day > now()"; */
$sql = "select HS_AddressID, HS_AddressOldID
from one_hs.hs_address
where HS_AddressOldID in (
select HS_AddressID
from one_hs.ol_hs_address
where HS_AddressLastUpdated + interval 3 day > now()
)";
$qry = $this->db->query($sql);
if (!$qry) {
$this->log(
" ERR insert ol_t_transactionHS " .
$this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
$rows = $qry->result_array();
foreach ($rows as $r) {
$mapHsAddress[$r["HS_AddressOldID"]] = $r["HS_AddressID"];
}
$this->log(" Inserting ol_t_transactionHS ");
$sql =
"select * from one_hs.ol_t_transactionHS where T_TransactionHSOldID = 0 or T_TransactionHSOldID is null";
$qry = $this->db->query($sql);
if (!$qry) {
$this->log(
" ERR insert ol_t_transactionHS" . $this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
foreach ($qry->result_array() as $r) {
$oldTxID = $r["t_transactionHST_TransactionID"];
$oldID = $r["t_transactionHSID"];
unset($r["t_transactionHSID"]);
$r["t_transactionHSOldID"] = $oldID;
$r["t_transactionHST_TransactionID"] = 0;
$r["t_transactionHST_TransactionOldID"] = $oldTxID;
if (isset($mapHsAddress[$r["t_transactionHSHS_AddressID"]])) {
$r["t_transactionHSHS_AddressID"] =
$mapHsAddress[$r["t_transactionHSHS_AddressID"]];
} else {
$r["t_transactionHSHS_AddressID"] = 0;
}
$qry = $this->db->insert("one_hs.t_transactionHS", $r);
$this->log(" one_hs.t_transactionHS $oldID $oldTxID");
if (!$qry) {
$this->log(
" ERR insert one_hs.ol_t_transactionHS" .
$this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
$sql = "update one_hs.t_transactionHS
join one_hs.t_transaction on t_transactionHST_TransactionID = 0
and t_transactionHST_TransactionOldID = T_TransactionOldID
and T_TransactionIsActive = 'Y'
set t_transactionHST_TransactionID = T_TransactionID";
$qry = $this->db->query($sql);
if (!$qry) {
$this->log(
" ERR update one_hs.t_transactionHS " .
$this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
$sql =
"update one_hs.ol_t_transactionHS set t_transactionHSOldID = ? where t_transactionHSID = ?";
$qry = $this->db->query($sql, [$oldID, $oldID]);
if (!$qry) {
$this->log(
" ERR update one_hs.ol_t_transactionHS" .
$this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
}
//m_patient
$this->log(" Inserting ol_m_patient");
$sql =
"select * from one_hs.ol_m_patient where M_PatientOldID = 0 or M_PatientOldID is null";
$qry = $this->db->query($sql);
if (!$qry) {
$this->log(
" ERR insert ol_m_patient " . $this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
foreach ($qry->result_array() as $r) {
$oldID = $r["M_PatientID"];
list($c_status, $c_count, $msg) = $this->get_count(
"one_hs.m_patient",
"M_PatientOldID",
$oldID
);
if (!$c_status) {
$this->log(
" ERR get existing one_hs.m_patient " .
$this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
if ($c_count == 0) {
unset($r["M_PatientID"]);
$r["M_PatientOldID"] = $oldID;
$qry = $this->db->insert("one_hs.m_patient", $r);
$this->log(" one_hs.m_patient $oldID");
if (!$qry) {
$this->log(
" ERR insert one_hs.m_patient " .
$this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
}
$sql =
"update one_hs.ol_m_patient set M_PatientOldID = ? where M_PatientID = ?";
$qry = $this->db->query($sql, [$oldID, $oldID]);
if (!$qry) {
$this->log(
" ERR update one_hs.ol_m_patient " .
$this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
}
$this->log(" Inserting ol_m_patientaddress");
$sql =
"select * from one_hs.ol_m_patientaddress where M_PatientAddressOldID = 0 or M_PatientAddressOldID is null";
$qry = $this->db->query($sql);
if (!$qry) {
$this->log(
" ERR get ol_m_patientaddress " . $this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
foreach ($qry->result_array() as $r) {
$oldPatientID = $r["M_PatientAddressM_PatientID"];
$oldID = $r["M_PatientAddressID"];
list($c_status, $c_count, $msg) = $this->get_count(
"one_hs.m_patientaddress",
"M_PatientAddressOldID",
$oldID
);
if (!$c_status) {
$this->log(
" ERR get existing one_hs.m_patientaddress " .
$this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
if ($c_count == 0) {
unset($r["M_PatientAddressID"]);
$r["M_PatientAddressOldID"] = $oldID;
$r["M_PatientAddressM_PatientOldID"] = $oldPatientID;
$r["M_PatientAddressM_PatientID"] = 0;
$qry = $this->db->insert("one_hs.m_patientaddress", $r);
$this->log(" one_hs.m_patientaddress $oldID");
if (!$qry) {
$this->log(
" ERR insert one_hs.ol_m_patientaddress " .
$this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
$newID = $this->db->insert_id();
$sql = "
update
one_hs.m_patient
join one_hs.m_patientaddress on M_PatientOldID = ?
and M_PatientAddressID= ?
set M_PatientAddressM_PatientID = M_PatientID";
$qry = $this->db->query($sql, [$oldPatientID, $newID]);
if (!$qry) {
$this->log(
" ERR update one_hs.m_patientaddress " .
$this->db->error()["message"]
);
echo $this->db->last_query();
$this->db->trans_rollback();
exit();
}
}
$sql =
"update one_hs.ol_m_patientaddress set M_PatientAddressOldID = ? where M_PatientAddressID = ?";
$qry = $this->db->query($sql, [$oldID, $oldID]);
if (!$qry) {
$this->log(
" ERR update one_hs.ol_m_patientaddress " .
$this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
}
$this->log(" Inserting ol_t_order");
$sql =
"select * from one_hs.ol_t_order where T_OrderOldID = 0 or T_OrderOldID is null";
$qry = $this->db->query($sql);
if (!$qry) {
$this->log(
" ERR insert ol_t_order " . $this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
foreach ($qry->result_array() as $r) {
$oldTxID = $r["T_OrderT_TransactionID"];
$oldID = $r["T_OrderID"];
$oldPatientID = $r["T_OrderM_PatientID"];
unset($r["T_OrderID"]);
$r["T_OrderOldID"] = $oldID;
$r["T_OrderT_TransactionID"] = 0;
$r["T_OrderT_TransactionOldID"] = $oldTxID;
$r["T_OrderM_PatientOldID"] = $oldPatientID;
$r["T_OrderM_PatientID"] = 0;
$this->log(" one_hs.t_order $oldID");
$qry = $this->db->insert("one_hs.t_order", $r);
if (!$qry) {
$this->log(
" ERR insert one_hs.t_order" . $this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
$newID = $this->db->insert_id();
//update t_order
$sql = "update one_hs.t_order
join one_hs.t_transaction on T_OrderID=?
and T_OrderT_TransactionOldID = T_TransactionOldID
set T_OrderT_TransactionID = T_TransactionID";
$qry = $this->db->query($sql, [$newID]);
if (!$qry) {
$this->log(
" ERR update one_hs.t_order " .
$this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
//update t_order m_patient
$sql = "update one_hs.t_order
join one_hs.m_patient on T_OrderID= ?
and M_PatientOldID = ?
set T_OrderM_PatientID= M_PatientID";
$qry = $this->db->query($sql, [$newID, $oldPatientID]);
if (!$qry) {
$this->log(
" ERR update one_hs.t_order m_patientid " .
$this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
$sql =
"update one_hs.ol_t_order set T_OrderOldID= ? where T_OrderID= ?";
$qry = $this->db->query($sql, [$oldID, $oldID]);
if (!$qry) {
$this->log(
" ERR update one_hs.ol_t_order" .
$this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
}
$this->log(" Inserting ol_t_orderdetail");
$sql =
"select * from one_hs.ol_t_orderdetail where T_OrderDetailOldID = 0 or T_OrderDetailOldID is null";
$qry = $this->db->query($sql);
if (!$qry) {
$this->log(
" ERR get ol_t_orderdetail " . $this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
foreach ($qry->result_array() as $r) {
$oldOrderID = $r["T_OrderDetailT_OrderID"];
$oldID = $r["T_OrderDetailID"];
unset($r["T_OrderDetailID"]);
$r["T_OrderDetailOldID"] = $oldID;
$r["T_OrderDetailT_OrderID"] = 0;
$r["T_OrderDetailT_OrderOldID"] = $oldOrderID;
$this->log(" one_hs.t_orderdetail $oldID");
$qry = $this->db->insert("one_hs.t_orderdetail", $r);
if (!$qry) {
$this->log(
" ERR insert one_hs.ol_t_orderdetail " .
$this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
$newID = $this->db->insert_id();
//update t_order
$sql = "update one_hs.t_orderdetail
join one_hs.t_order on T_OrderDetailID = ?
and T_OrderDetailT_OrderOldID = T_OrderOldID
set T_OrderDetailT_OrderID = T_OrderID";
$qry = $this->db->query($sql, [$newID]);
if (!$qry) {
$this->log(
" ERR update one_hs.t_orderdetail " .
$this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
$sql =
"update one_hs.ol_t_orderdetail set T_OrderDetailOldID= ? where T_OrderDetailID= ?";
$qry = $this->db->query($sql, [$oldID, $oldID]);
if (!$qry) {
$this->log(
" ERR update one_hs.ol_t_orderdetail " .
$this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
}
//$this->db->trans_rollback();
$this->log(" Inserting ol_t_orderdeliveries");
$sql =
"select * from one_hs.ol_t_orderdeliveries where T_OrderDeliveriesOldID = 0 or T_OrderDeliveriesOldID is null";
$qry = $this->db->query($sql);
if (!$qry) {
$this->log(
" ERR get ol_t_orderdetail " . $this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
foreach ($qry->result_array() as $r) {
$oldOrderID = $r["T_OrderDeliveriesT_OrderID"];
$oldID = $r["T_OrderDeliveriesID"];
unset($r["T_OrderDeliveriesID"]);
$r["T_OrderDeliveriesOldID"] = $oldID;
$r["T_OrderDeliveriesT_OrderID"] = 0;
$r["T_OrderDeliveriesT_OrderOldID"] = $oldOrderID;
$this->log(" one_hs.t_orderdeliveries $oldID");
$qry = $this->db->insert("one_hs.t_orderdeliveries", $r);
if (!$qry) {
$this->log(
" ERR insert one_hs.ol_t_orderdeliveries" .
$this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
$newID = $this->db->insert_id();
//update t_order
$sql = "update one_hs.t_orderdeliveries
join one_hs.t_order on T_OrderDeliveriesID = ?
and T_OrderDeliveriesT_OrderOldID = T_OrderOldID
set T_OrderDeliveriesT_OrderID = T_OrderID";
$qry = $this->db->query($sql, [$newID]);
if (!$qry) {
$this->log(
" ERR update one_hs.t_orderdeliveries " .
$this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
$sql =
"update one_hs.ol_t_orderdeliveries set T_OrderDeliveriesOldID= ? where T_OrderDeliveriesID= ?";
$qry = $this->db->query($sql, [$oldID, $oldID]);
if (!$qry) {
$this->log(
" ERR update one_hs.ol_t_orderdeliveries " .
$this->db->error()["message"]
);
$this->db->trans_rollback();
exit();
}
}
if ($stand_alone != "") {
$this->db->trans_commit();
}
return $arrTxIds;
}
function updateOnDuplicate($table, $data)
{
if (empty($table) || empty($data)) {
return [false, "Empty Data"];
}
$duplicate_data = [];
foreach ($data as $key => $value) {
$duplicate_data[] = sprintf("%s='%s'", $key, $value);
}
$sql = sprintf(
"%s ON DUPLICATE KEY UPDATE %s",
$this->db->insert_string($table, $data),
implode(",", $duplicate_data)
);
$qry = $this->db->query($sql);
if (!$qry) {
return [
false,
$this->db->error()["message"] . "\n" . $this->db->last_query(),
];
}
return [true, ""];
}
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;
}
}