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

392 lines
11 KiB
PHP

<?php
class Rujukan_panel extends MY_Controller
{
function __construct()
{
parent::__construct();
}
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, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Content-Type: application/text",
"Content-Length: " . strlen($data),
]);
$result = 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);
return $result;
}
function update_origin($j_param)
{
$targetIP = "";
$param = [];
foreach ($j_param as $p) {
$targetIP = $p["originBranchIP"];
$param[] = [
"price" => $p["Price_Rujukan"],
"detailID" => $p["originT_OrderDetailID"],
];
}
$payload = json_encode($param);
if ($targetIP == "") {
return [false, "Error Target IP"];
}
$url = "http://$targetIP/one-api/fix/rujukan_panel/r_fix";
$resp = $this->post($url, $payload);
$jresp = json_decode($resp, true);
if (json_last_error() > 0) {
return [false, "Json Error : " . json_last_error_msg() . "| $resp"];
}
if ($jresp["status"] == "OK") {
return [true, ""];
}
return [false, $jresp["message"]];
}
function fix()
{
$body = file_get_contents("php://input");
$i_param = base64_decode($body);
$j_param = json_decode($i_param, true);
$this->db->trans_begin();
list($status, $message) = $this->update_origin($j_param);
if (!$status) {
echo json_encode([
"status" => "ERR",
"message" => $message,
]);
exit();
}
$sql =
"update t_orderdetail set T_OrderDetailTotal =? where T_OrderDetailID = ?";
foreach ($j_param as $r) {
$qry = $this->db->query($sql, [
$r["Price_Rujukan"],
$r["T_OrderDetailID"],
]);
if (!$qry) {
echo json_encode([
"status" => "ERR",
"message" => $this->db->error()["message"],
]);
$this->db->trans_rollback();
exit();
}
}
echo json_encode(["status" => "OK", "message" => ""]);
$this->db->trans_commit();
}
/*
create table t_orderdetail_rujukan (
T_OrderDetailRujukanID int not null auto_increment primary key,
T_OrderDetailRujukanT_OrderDetailID int,
T_OrderDetailRujukanPrice decimal(15,2),
T_OrderDetailRujukanIsActive varchar(1) default 'Y',
T_OrderDetailCreated DateTime default current_timestamp(),
T_OrderDetailLastUpdated DateTime default current_timestamp() on update current_timestamp(),
key(T_OrderDetailRujukanT_OrderDetailID),
key(T_OrderDetailRujukanIsActive)
);
*/
function R_fix()
{
$this->db->trans_begin();
foreach ($this->sys_input as $p) {
$arr = [
"T_OrderDetailRujukanT_OrderDetailID" => $p["detailID"],
"T_OrderDetailRujukanPrice" => $p["price"],
];
$res = $this->insert_or_update("t_orderdetail_rujukan", $arr, [
"T_OrderDetailRujukanT_OrderDetailID",
]);
if ($res["status"] == "ERR") {
echo json_encode($res);
$this->db->trans_rollback();
exit();
}
}
echo json_encode(["status" => "OK"]);
$this->db->trans_commit();
}
function info($nolab)
{
$sql = "select distinct T_OrderHeaderLabNumber, T_OrderHeaderDate, T_OrderHeaderID
,T_OrderHeaderAddOnLabNumberOrigin, T_OrderHeaderM_MouID,
incomingRefM_BranchID originBranchID,
incomingRefDetailT_TestID, incomingRefDetailT_OrderDetailID,
M_BranchIPAddress
from t_orderheader
join t_orderheaderaddon on
(
T_OrderHeaderLabNumber like ?
or
T_OrderHeaderLabNumberExt like ?
)
and T_OrderHeaderAddOnT_OrderHeaderID = T_OrderHeaderID
join incoming_ref_detail on incomingRefDetailNewT_OrderHeaderID = T_OrderHeaderID
join incoming_ref on incomingRefDetailIncomingRefID = incomingRefID
join m_branch on incomingRefM_BranchID = M_BranchID";
$qry = $this->db->query($sql, [$nolab, $nolab]);
$this->print_table_style();
if (!$qry) {
$xrows = ["Error", $this->db->error()["message"]];
$this->print_table_one_column($xrows);
exit();
}
$rows = $qry->result_array();
if (count($rows) == 0) {
$xrows = ["Error", "Nolab not found $nolab"];
$this->print_table_one_column($xrows);
exit();
}
$headerID = $rows[0]["T_OrderHeaderID"];
$mouID = $rows[0]["T_OrderHeaderM_MouID"];
$originBranchID = $rows[0]["originBranchID"];
$originBranchIP = $rows[0]["M_BranchIPAddress"];
$arr_origin = [];
foreach ($rows as $r) {
$arr_origin[$r["incomingRefDetailT_TestID"]] =
$r["incomingRefDetailT_OrderDetailID"];
}
unset($rows[0]["T_OrderHeaderID"]);
unset($rows[0]["T_OrderHeaderM_MouID"]);
unset($rows[0]["originBranchID"]);
unset($rows[0]["M_BranchIPAddress"]);
$this->print_table($rows, array_keys($rows[0]));
echo "<br/>";
$sql = "select T_OrderDetailT_TestSasCode,T_OrderDetailT_TestName,
T_OrderDetailPrice, T_OrderDetailID,
T_OrderDetailTotal,
T_OrderDetailT_TestID, 0 Price_Rujukan, 0 Parent_Price
from t_orderdetail
where
T_OrderDetailIsActive = 'Y'
and T_OrderDetailT_OrderHeaderID = ?";
$qry = $this->db->query($sql, [$headerID]);
if (!$qry) {
$xrows = ["Error", $this->db->error()["message"]];
$this->print_table_one_column($xrows);
exit();
}
$rows = $qry->result_array();
if (count($rows) == 0) {
$xrows = ["Error", "Nolab not found $nolab"];
$this->print_table_one_column($xrows);
exit();
}
$parent_price = [];
$s_id = "0";
foreach ($rows as $r) {
$parent_price[$r["T_OrderDetailT_TestSasCode"]] =
$r["T_OrderDetailTotal"];
$s_id .= "," . $r["T_OrderDetailT_TestID"];
}
$sql = "select *
from m_mou_rujukan
where M_MouRujukanM_BranchID = ? and M_MouRujukanM_MouID= ?
and M_MouRujukanT_TestID in ($s_id)";
$qry = $this->db->query($sql, [$originBranchID, $mouID]);
$rj_rows = $qry->result_array();
$rujukan_price = [];
foreach ($rj_rows as $r) {
$testID = $r["M_MouRujukanT_TestID"];
$price = $r["M_MouRujukanPrice"];
$rujukan_price[$testID] = $price;
}
foreach ($rows as $idx => $r) {
if (isset($rujukan_price[$r["T_OrderDetailT_TestID"]])) {
$rows[$idx]["Price_Rujukan"] =
$rujukan_price[$r["T_OrderDetailT_TestID"]];
}
$parentCode = substr(
$r["T_OrderDetailT_TestSasCode"],
0,
strlen($r["T_OrderDetailT_TestSasCode"]) - 2
);
if (isset($parent_price[$parentCode])) {
$rows[$idx]["Parent_Price"] = $parent_price[$parentCode];
} else {
$parentCode = substr(
$r["T_OrderDetailT_TestSasCode"],
0,
strlen($r["T_OrderDetailT_TestSasCode"]) - 4
);
if (isset($parent_price[$parentCode])) {
$rows[$idx]["Parent_Price"] = $parent_price[$parentCode];
}
}
}
$haveFixation = false;
$arr_fix = [];
foreach ($rows as $idx => $r) {
if (
//$r["T_OrderDetailTotal"] == 0 &&
$r["Price_Rujukan"] > 0 &&
$r["Parent_Price"] == 0
) {
$rows[$idx]["Fix Rujukan"] = "Fix";
$haveFixation = true;
$arr_fix[] = [
"T_OrderDetailID" => $r["T_OrderDetailID"],
"Price_Rujukan" => $r["Price_Rujukan"],
"originBranchIP" => $originBranchIP,
"originT_OrderDetailID" =>
$arr_origin[$r["T_OrderDetailT_TestID"]],
];
}
}
$fixBody = base64_encode(json_encode($arr_fix));
$script = "<script>
console.log('$fixBody');
function fixRujukan() {
fetch('/one-api/fix/rujukan_panel/fix',{'method':'POST','cache':'no-cache','body':'$fixBody' })
.then((response) => response.json())
.then((data) => {
if (data.status == 'ERR') {
alert(data.message);
return;
}
document.location.reload();
});
}</script>";
$button = "<button style='padding:20px;' onClick='fixRujukan()'>Fix Harga Rujukan</button>$script";
$this->print_table($rows, array_keys($rows[0]));
echo "<br/>";
if ($haveFixation) {
echo $button;
}
}
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 print_table_style()
{
echo "
<style>
th, td {
padding: 15px;
text-align: left;
}
tr:nth-child(even) {background-color: #f2f2f2;}
table {
border: solid 1px ;
min-width:600px;
}
</style>
";
}
public function print_table_one_column($rows)
{
$rst = "<table>";
foreach ($rows as $r) {
$rst .= "<tr>";
$rst .= "<td>" . $r . "</td>";
$rst .= "</tr>";
}
$rst .= "</table>";
echo $rst;
}
public function print_table($rows, $keys)
{
echo "<table>";
echo "<tr>";
foreach ($keys as $k) {
echo "<td>$k</td>";
}
echo "</tr>\n";
foreach ($rows as $r) {
echo "<tr>";
foreach ($keys as $k) {
echo "<td>" . $r[$k] . "</td>";
}
echo "</tr>";
}
echo "</table>";
}
}