host = $db["default"]["hostname"]; $dbh->username = $db["default"]["username"]; $dbh->password = $db["default"]["password"]; $dbh->database = $db["default"]["database"]; $p = $dbh->get_param(); header("Content-Type: application/json"); if ($p["cmd"] == "test") { $resp = $dbh->connect(); $dbh->exit_if_error($resp[0], $resp[1]); $sql = "show databases"; $resp = $dbh->query($sql); $dbh->exit_if_error($resp[0], $resp[1]); $dbh->jsonReply($resp); $sql = "select * from m_branch"; $resp = $dbh->query($sql); $dbh->exit_if_error($resp[0], $resp[1]); $dbh->jsonReply($resp); exit; } if ($p["cmd"] == "service_req") { $resp = $dbh->connect(); $dbh->exit_if_error($resp[0], $resp[1]); $ids = implode(",", $p["ids"]); if ($ids == "") { $ids = "0"; } $sql = "select T_OrderDetailUserID, T_OrderHeaderID, T_OrderDetailID,T_OrderDetailT_TestID, T_OrderDetailT_TestCode, T_OrderDetailT_TestName, T_OrderDetailLastUpdate, T_OrderHeaderDate, concat(T_OrderDetailSamplingDate,' ',T_OrderDetailSamplingTime) T_OrderDetailSamplingDate, M_UserFullName from t_orderdetail join t_orderheader on T_OrderDetailT_OrderHeaderID in ($ids) and T_OrderHeaderID = T_OrderDetailT_OrderHeaderID and T_OrderDetailIsActive='Y' and T_OrderDetailPrice > 0 join m_user on T_OrderDetailUserID = M_UserID join t_test on T_OrderDetailT_TestID = T_TestID and T_TestIsActive = 'Y' join t_testgroup on T_TestT_TestGroupID = T_TestGroupID and T_TestGroupT_ServiceID = 1 and T_TestGroupIsActive = 'Y'"; $resp = $dbh->query($sql); $dbh->exit_if_error($resp[0], $resp[1]); $dbh->jsonReply($resp, $dbh->info()); exit; } if ($p["cmd"] == "clinic-service_req") { $resp = $dbh->connect(); $dbh->exit_if_error($resp[0], $resp[1]); $ids = isset($p["ids"]) ? implode(",", $p["ids"]) : ""; $start_date = isset($p["start_date"]) ? $p["start_date"] : ""; $end_date = isset($p["end_date"]) ? $p["end_date"] : ""; $params = []; $where_date = ""; if ($start_date != "" || $end_date != "") { if ($start_date == "") { $start_date = date("Y-m-d"); } if ($end_date == "") { $end_date = $start_date; } $params = [$start_date . " 00:00:00", $end_date . " 23:59:59"]; $where_date = " and T_OrderHeaderDate >= ? and T_OrderHeaderDate <= ? "; } $where_ids = ""; if ($ids != "") { $where_ids = " and T_OrderDetailT_OrderHeaderID in ($ids) "; } else if ($start_date == "" && $end_date == "") { //legacy: no ids and no date -> empty result $where_ids = " and T_OrderDetailT_OrderHeaderID in (0) "; } $sql = "select T_OrderDetailUserID, T_OrderHeaderID, T_OrderDetailID,T_OrderDetailT_TestID, T_OrderDetailT_TestCode, T_OrderDetailT_TestName, T_OrderDetailLastUpdate, T_OrderHeaderDate, concat(T_OrderDetailSamplingDate,' ',T_OrderDetailSamplingTime) T_OrderDetailSamplingDate, M_UserFullName from t_orderdetail join t_orderheader on T_OrderHeaderID = T_OrderDetailT_OrderHeaderID and T_OrderDetailIsActive='Y' and T_OrderDetailPrice > 0 $where_ids $where_date join m_user on T_OrderDetailUserID = M_UserID join t_test on T_OrderDetailT_TestID = T_TestID and T_TestIsActive = 'Y' join t_testgroup on T_TestT_TestGroupID = T_TestGroupID and T_TestGroupT_ServiceID <> 1 and T_TestGroupIsActive = 'Y'"; $resp = $dbh->query($sql, $params); $dbh->exit_if_error($resp[0], $resp[1]); $dbh->jsonReply($resp, $dbh->info()); exit; } if ($p["cmd"] == "clinic-diagnose") { $resp = $dbh->connect(); $dbh->exit_if_error($resp[0], $resp[1]); $ids = isset($p["ids"]) ? implode(",", $p["ids"]) : ""; $start_date = isset($p["start_date"]) ? $p["start_date"] : ""; $end_date = isset($p["end_date"]) ? $p["end_date"] : ""; if ($start_date == "") { $start_date = date("Y-m-d"); } if ($end_date == "") { $end_date = $start_date; } $params = [$start_date . " 00:00:00", $end_date . " 23:59:59"]; $where_ids = ""; if ($ids != "") { $where_ids = " and T_OrderHeaderID in ($ids) "; } $sql = "select T_OrderHeaderID, T_OrderHeaderNote, T_OrderHeaderDate from t_orderheader where T_OrderHeaderIsActive = 'Y' and T_OrderHeaderDate >= ? and T_OrderHeaderDate <= ? $where_ids and exists (select 1 from t_orderdetail d join t_test tt on d.T_OrderDetailT_TestID = tt.T_TestID and tt.T_TestIsActive = 'Y' join t_testgroup g on tt.T_TestT_TestGroupID = g.T_TestGroupID and g.T_TestGroupT_ServiceID <> 1 and g.T_TestGroupIsActive = 'Y' where d.T_OrderDetailT_OrderHeaderID = T_OrderHeaderID and d.T_OrderDetailIsActive = 'Y')"; $resp = $dbh->query($sql, $params); $dbh->exit_if_error($resp[0], $resp[1]); $dbh->jsonReply($resp, $dbh->info()); exit; } if ($p["cmd"] == "clinic-encounter") { $resp = $dbh->connect(); $dbh->exit_if_error($resp[0], $resp[1]); $sql = "select order.orderID, order.orderCreated orderDate, order.orderM_DoctorID, order.orderM_PatientID, order.orderNumbering, m_patient.M_PatientNIK, m_patient.M_PatientName, a_doctor.A_DoctorName, a_doctor.A_DoctorID, NIK A_DoctorNIK from one_clinic.order join m_patient on order.orderDate >=? and order.orderDate < ? and order.orderM_PatientID = m_patient.M_PatientID and order.orderIsActive = 'Y' left join a_doctor on orderM_DoctorID = a_doctor.A_DoctorID"; $date = $p["date"]; if ($date == "") { $date = date("Y-m-d"); } $sdate = $date . " 00:00:00"; if ($p["edate"] != "") { $edate = $p["edate"] . " 23:59:59"; } else { $edate = $date . " 23:59:59"; } $resp = $dbh->query($sql, [$sdate, $edate]); $dbh->exit_if_error($resp[0], $resp[1]); if (isset($p["sum"]) && $p["sum"] != "") { $first = true; $a_doctor = []; foreach ($resp[1] as $d) { if ($first) { $edate = substr($edate, 0, 10); echo "\nFrom $date to {$edate} :\n"; $first = false; } $name = $d["A_DoctorName"]; if (!isset($a_doctor[$name])) { $a_doctor[$name] = 1; } else { $a_doctor[$name] += 1; } } foreach ($a_doctor as $d => $count) { echo "\t{$d} \t\t: $count\n"; } exit; } $dbh->jsonReply($resp); exit; } function get_clinic_encounter($sdate, $edate) { global $dbh; $resp = $dbh->connect(); $dbh->exit_if_error($resp[0], $resp[1]); $sql = "select distinct m_branch.M_BranchID, m_branch.M_BranchCode, t_orderheader.T_OrderHeaderID orderID, t_orderheader.T_OrderHeaderDate orderDate, t_orderheader.T_OrderHeaderM_DoctorPJID orderM_DoctorID, t_orderheader.T_OrderHeaderM_PatientID orderM_PatientID, t_orderheader.T_OrderHeaderLabNumber orderNumbering, m_patient.M_PatientNIK, m_patient.M_PatientName, m_doctor.M_DoctorID A_DoctorID, m_doctor.M_DoctorName A_DoctorName, t_orderheader.T_OrderHeaderReceivedSample , T_OrderHeaderUserID, M_UserUsername from t_orderheader join m_patient on t_orderheader.T_OrderHeaderDate > ? and t_orderheader.T_OrderHeaderDate < ? and t_orderheader.T_OrderHeaderM_PatientID = m_patient.M_PatientID join t_orderdetail on T_OrderHeaderID = T_OrderDetailT_OrderHeaderID and T_OrderHeaderIsActive = 'Y' and T_OrderDetailIsActive = 'Y' join t_test on T_OrderDetailT_TestID = T_TestID and T_TestIsActive = 'Y' join t_testgroup on T_TestT_TestGroupID = T_TestGroupID and T_TestGroupT_ServiceID <> 1 left join m_doctor on T_OrderHeaderM_DoctorPJID = m_doctor.M_DoctorID left join m_user on T_OrderHeaderUserID = M_UserID left join m_branch on m_branch.M_BranchIsDefault = 'Y' and m_branch.M_BranchIsActive = 'Y' "; $resp = $dbh->query($sql, [$sdate, $edate]); $dbh->exit_if_error($resp[0], $resp[1]); return $resp; } if ($p["cmd"] == "clinic-encounter-v2") { $date = $p["date"]; $edate = $p["edate"]; if ($date == "") { $date = date("Y-m-d"); } if ($edate == "") { $edate = $date; } $sdate = $date . " 00:00:01"; $edate = $edate . " 23:59:59"; $resp = get_clinic_encounter($sdate, $edate); $dbh->exit_if_error($resp[0], $resp[1]); $dbh->jsonReply($resp); exit; } if ($p["cmd"] == "encounter") { $resp = $dbh->connect(); $dbh->exit_if_error($resp[0], $resp[1]); $sql = "select m_branch.M_BranchID, m_branch.M_BranchCode, t_orderheader.T_OrderHeaderID, t_orderheader.T_OrderHeaderDate, t_orderheader.T_OrderHeaderM_DoctorPJID, t_orderheader.T_OrderHeaderM_PatientID, t_orderheader.T_OrderHeaderLabNumber, m_patient.M_PatientNIK, m_patient.M_PatientName, m_doctor.M_DoctorName, t_orderheader.T_OrderHeaderReceivedSample, T_OrderHeaderUserID, M_UserUsername from t_orderheader join m_patient on t_orderheader.T_OrderHeaderDate > ? and t_orderheader.T_OrderHeaderDate < ? and t_orderheader.T_OrderHeaderM_PatientID = m_patient.M_PatientID left join m_doctor on T_OrderHeaderM_DoctorPJID = m_doctor.M_DoctorID left join m_user on T_OrderHeaderUserID = M_UserID left join m_branch on m_branch.M_BranchIsDefault = 'Y' and m_branch.M_BranchIsActive = 'Y' "; $date = $p["date"]; if ($date == "") { $date = date("Y-m-d"); } $sdate = $date . " 00:00:01"; $edate = $date . " 23:59:59"; $resp = $dbh->query($sql, [$sdate, $edate]); $dbh->exit_if_error($resp[0], $resp[1]); $dbh->jsonReply($resp); exit; } if ($p["cmd"] == "encounter-v2") { $resp = $dbh->connect(); $dbh->exit_if_error($resp[0], $resp[1]); $date = $p["date"]; if ($date == "") { $date = date("Y-m-d"); } $sdate = $date . " 00:00:01"; $edate = $date . " 23:59:59"; $cl_resp = get_clinic_encounter($sdate, $edate); $dbh->exit_if_error($cl_resp[0], $cl_resp[1]); $cl_arr = array_map(function ($r) { return $r["orderID"]; }, $cl_resp[1]); $cl_ids = implode(",", $cl_arr); if ($cl_ids == "") { $cl_ids = "0"; } $sql = "select m_branch.M_BranchID, m_branch.M_BranchCode, t_orderheader.T_OrderHeaderID, t_orderheader.T_OrderHeaderDate, t_orderheader.T_OrderHeaderM_DoctorPJID, t_orderheader.T_OrderHeaderM_PatientID, t_orderheader.T_OrderHeaderLabNumber, m_patient.M_PatientNIK, m_patient.M_PatientName, m_doctor.M_DoctorName, t_orderheader.T_OrderHeaderReceivedSample, T_OrderHeaderUserID, M_UserUsername from t_orderheader join m_patient on t_orderheader.T_OrderHeaderDate > ? and t_orderheader.T_OrderHeaderDate < ? and t_orderheader.T_OrderHeaderID not in ($cl_ids) and t_orderheader.T_OrderHeaderM_PatientID = m_patient.M_PatientID left join m_doctor on T_OrderHeaderM_DoctorPJID = m_doctor.M_DoctorID left join m_user on T_OrderHeaderUserID = M_UserID left join m_branch on m_branch.M_BranchIsDefault = 'Y' and m_branch.M_BranchIsActive = 'Y' "; $resp = $dbh->query($sql, [$sdate, $edate]); $dbh->exit_if_error($resp[0], $resp[1]); $dbh->jsonReply($resp); exit; } echo json_encode(["status" => "ERR", "message" => "[sys] cmd not found"]); //helper class Database { public $host; public $username; public $password; public $database; private $conn; public function __construct() { $this->host = "localhost"; $this->username = "root"; $this->password = "smb1122"; $this->database = "one_health"; } public function jsonReply($resp, $sql = "") { $resp = ["status" => "OK", "data" => $resp[1]]; if ($sql != "") { $resp["sql"] = $sql; } echo json_encode($resp); } function get_param() { $body = file_get_contents("php://input"); $res = json_decode($body, true); if ($res == null) { return ["cmd" => "xx-unknown"]; } return $res; } function connect() { $this->conn = new mysqli($this->host, $this->username, $this->password, $this->database); if ($this->conn->connect_error) { return [false, "Connection failed: " . $this->conn->connect_error]; } return [true, "Connection OK "]; } public function query($sql, $params = array()) { if (!$this->conn) { $this->connect(); } $statement = $this->conn->prepare($sql); if (!$statement) { return [false, "Error preparing SQL statement: " . $this->conn->error]; } if (!empty($params)) { $types = ''; foreach ($params as $param) { if (is_int($param)) { $types .= 'i'; // integer } elseif (is_float($param)) { $types .= 'd'; // double } elseif (is_string($param)) { $types .= 's'; // string } else { $types .= 'b'; // blob } } $xparams = [$types]; foreach ($params as $param) { $xparams[] = $param; } $tmp = array(); foreach ($xparams as $key => $value) $tmp[$key] = &$xparams[$key]; call_user_func_array(array($statement, 'bind_param'), $tmp); } $result = $statement->execute(); if (!$result) { return [false, "Error executing SQL statement: " . $statement->error]; } // $result = $statement->get_result(); // if (!$result) { // return [false, "Error getting result set: " . $statement->error]; // } $rows = array(); $result = $this->get_result($statement); while ($row = array_shift($result)) { $rows[] = $row; } $statement->close(); return [true, $rows]; } function info() { $param = mysqli_info($this->conn); return $param; } public function exec($sql, $params = array()) { if (!$this->conn) { $this->connect(); } $statement = $this->conn->prepare($sql); if (!$statement) { return [false, "Error preparing SQL statement: " . $this->conn->error]; } if (!empty($params)) { $types = ''; foreach ($params as $param) { if (is_int($param)) { $types .= 'i'; // integer } elseif (is_float($param)) { $types .= 'd'; // double } elseif (is_string($param)) { $types .= 's'; // string } else { $types .= 'b'; // blob } } $xparams = [$types]; foreach ($params as $param) { $xparams[] = $param; } $tmp = array(); foreach ($xparams as $key => $value) $tmp[$key] = &$xparams[$key]; call_user_func_array(array($statement, 'bind_param'), $tmp); } $result = $statement->execute(); if (!$result) { return [false, "Error executing SQL statement: " . $statement->error]; } $statement->close(); return [true, "Query executed successfully."]; } public function close() { if ($this->conn) { $this->conn->close(); } } public function exit_if_error($status, $data) { if ($status === false) { echo json_encode(["status" => "ERR", "message" => $data]); exit; } } function get_result($statement) { $result = array(); $statement->store_result(); for ($i = 0; $i < $statement->num_rows; $i++) { $metadata = $statement->result_metadata(); $params = array(); while ($field = $metadata->fetch_field()) { $params[] = &$result[$i][$field->name]; } call_user_func_array(array($statement, 'bind_result'), $params); $statement->fetch(); } return $result; } }