Files
biotest-satu-sehat-web/be_clinic.php
2024-04-15 10:26:43 +07:00

584 lines
17 KiB
PHP

<?php
if (true) {
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
}
define('BASEPATH', "/home/smartlab");
include_once("./smartlab_biotest/application/config/database.php");
$dbh = new Database();
$dbh->host = $db["default"]["hostname"];
$dbh->username = $db["default"]["username"];
$dbh->password = $db["default"]["password"];
$dbh->database = $db["default"]["database"];
$p = $dbh->get_param();
if (isset($_GET['cmd']) && $_GET["cmd"] == "csv") {
$sdate = $_GET["sdate"] . " 00:00:00";
$edate = $_GET["edate"] . " 23:59:59";
$sql = "
SELECT
'Order Number' orderNumbering,
'Date' orderDate,
'Age' orderAge,
'Patient' M_PatientName,
'Keluhan' orderKeluhan,
'Objective' orderObjective,
'Kesimpulan' orderKesimpulan,
'Saran' orderSaran
union
SELECT
orderNumbering,
orderDate,
orderAge,
M_PatientName,
orderKeluhan,
orderObjective,
orderKesimpulan,
orderSaran
FROM one_clinic.order
JOIN m_patient ON OrderM_PatientID = M_PatientID
WHERE
orderIsActive = 'Y' AND
orderDate BETWEEN '$sdate' AND '$edate'";
$rows = $dbh->query($sql);
$dbh->exit_if_error($rows[0], $rows[1]);
$result = $rows[1];
array_to_csv_download($result, "export-$sdate-$edate.csv", ",");
exit;
}
function array_to_csv_download($array, $filename = "export.csv", $delimiter = ";")
{
$f = fopen('php://memory', 'w');
foreach ($array as $line) {
fputcsv($f, $line, $delimiter);
}
fseek($f, 0);
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '";');
fpassthru($f);
}
header("Content-Type: application/json");
if ($p["cmd"] == "add") {
$resp = $dbh->connect();
$dbh->exit_if_error($resp[0], $resp[1]);
$sql = "SELECT one_clinic.fn_numbering_klinik('C') as xnumber";
$resp_func = $dbh->query($sql);
$dbh->exit_if_error($resp_func[0], $resp_func[1]);
$numbering = $resp_func[1][0]['xnumber'];
//echo $numbering;
$sql = "SELECT * FROM m_patient WHERE M_PatientID = ?";
$resp_pat = $dbh->query($sql, array($p['M_PatientID']));
$dbh->exit_if_error($resp_pat[0], $resp_pat[1]);
$data_patient = $resp_pat[1][0];
$tanggal_lahir = $data_patient['M_PatientDOB'];
//echo $tanggal_lahir;
// Ambil tanggal sekarang
$dateorder = isset($p["orderDate"]) && $p["orderDate"] != "" ? date('Y-m-d', strtotime($p["orderDate"])) : "";
$tanggal_sekarang = new DateTime($dateorder);
// Ubah tanggal lahir ke objek DateTime
$tanggal_lahir = new DateTime($tanggal_lahir);
// Hitung selisih antara tanggal sekarang dan tanggal lahir
$perbedaan = $tanggal_sekarang->diff($tanggal_lahir);
// Dapatkan tahun, bulan, dan hari dari selisih
$umur_tahun = $perbedaan->y;
$umur_bulan = $perbedaan->m;
$umur_hari = $perbedaan->d;
$age_now = $umur_tahun . " Tahun " . $umur_bulan . " Bulan " . $umur_hari . " Hari";
$sql = "INSERT INTO one_clinic.order (
orderDate,
orderNumbering,
orderM_PatientID,
orderAge,
orderNurseID,
orderM_CompanyID,
orderKeluhan,
orderObjective,
orderKesimpulan,
orderSaran,
orderCreated,
orderCreatedUserID
) VALUES(?,?,?,?,?,?,?,?,?,?,NOW(),1)";
$resp_add = $dbh->query($sql, array(
$dateorder,
$numbering,
$p['M_PatientID'],
$age_now,
$p['NurseID'],
$p['M_CompanyID'],
$p['Keluhan'],
$p['Objective'],
$p['Kesimpulan'],
$p['Saran']
));
$dbh->exit_if_error($resp_add[0], $resp_add[1]);
$sql = "INSERT INTO one_clinic.order_log(
orderLogOrderNumber,
orderLogStatus,
orderLogJSONAfter,
orderLogCreated,
orderLogUserID
)
VALUES(?,'ADD',?,NOW(),1)";
$resp_log = $dbh->query($sql, array($numbering, json_encode($p)));
$dbh->exit_if_error($resp_log[0], $resp_log[1]);
$respx[] = true;
$respx[] = array('order_number' => $numbering, 'params' => $p);
$dbh->jsonReply($respx);
exit;
}
if ($p["cmd"] == "test") {
$resp = $dbh->connect();
$dbh->exit_if_error($resp[0], $resp[1]);
$sql = "select * from m_branch";
$resp = $dbh->query($sql);
$dbh->exit_if_error($resp[0], $resp[1]);
$dbh->jsonReply($resp);
exit;
}
if ($p["cmd"] == "getpatient") {
$resp = $dbh->connect();
$dbh->exit_if_error($resp[0], $resp[1]);
$resp = [];
$dob = isset($p["dob"]) && $p["dob"] != "" ? date('Y-m-d', strtotime($p["dob"])) : "";
$number_limit = 10;
$number_offset = (!isset($p['current_page']) ? 1 : $p['current_page'] - 1) * $number_limit;
if (isset($p["nik"]) && $p["nik"] != "") {
$sql = "SELECT m_patient.*, M_PatientIDTypeName, M_PatientIDNumber, M_SexName,
DATE_FORMAT(M_PatientDOB,'%d-%m-%Y') as dob_ina
FROM m_patient
JOIN m_sex ON M_PatientM_SexID = M_SexID
JOIN m_patientidnumber ON M_PatientIDNumberM_PatientID = M_PatientID AND
M_PatientIDNumberIsActive = 'Y' AND
M_PatientIDNumber LIKE CONCAT('%',?,'%')
JOIN m_patientidtype ON M_PatientIDNumberM_PatientIDTypeID = M_PatientIDTypeID
WHERE
M_PatientIsActive = 'Y' AND
(
M_PatientName LIKE CONCAT('%',?,'%') AND
M_PatientDOB LIKE CONCAT('%',?,'%') AND
M_PatientHP LIKE CONCAT('%',?,'%')
)
LIMIT $number_limit OFFSET $number_offset";
$resp = $dbh->query($sql, [$p['nik'], $p['name'], $dob, $p['hp']]);
} else {
$sql = "SELECT m_patient.*,
IFNULL(M_PatientIDTypeName,'-') as M_PatientIDTypeName,
IFNULL(M_PatientIDNumber,'') as M_PatientIDNumber,
M_SexName,
DATE_FORMAT(M_PatientDOB,'%d-%m-%Y') as dob_ina
FROM m_patient
JOIN m_sex ON M_PatientM_SexID = M_SexID
LEFT JOIN m_patientidnumber ON M_PatientIDNumberM_PatientID = M_PatientID AND
M_PatientIDNumberIsActive = 'Y'
LEFT JOIN m_patientidtype ON M_PatientIDNumberM_PatientIDTypeID = M_PatientIDTypeID
WHERE
M_PatientIsActive = 'Y' AND
(
M_PatientName LIKE CONCAT('%',?,'%') AND
M_PatientDOB LIKE CONCAT('%',?,'%') AND
M_PatientHP LIKE CONCAT('%',?,'%')
)
LIMIT $number_limit OFFSET $number_offset";
$resp = $dbh->query($sql, array($p['name'], $dob, $p['hp']));
}
$dbh->exit_if_error($resp[0], $resp[1]);
$dbh->jsonReply($resp);
exit;
}
if ($p["cmd"] == "getcompany") {
$resp = $dbh->connect();
$dbh->exit_if_error($resp[0], $resp[1]);
$resp = [];
$sql = "SELECT *
FROM m_patienttype
WHERE
M_PatientTypeIsActive = 'Y'
LIMIT 25";
$resp = $dbh->query($sql);
$dbh->exit_if_error($resp[0], $resp[1]);
$dbh->jsonReply($resp);
exit;
}
if ($p["cmd"] == "getnurse") {
$resp = $dbh->connect();
$dbh->exit_if_error($resp[0], $resp[1]);
$resp = [];
$sql = "SELECT *
FROM a_nurse
WHERE
A_NurseMetaActive = 'Y'
LIMIT 25";
$resp = $dbh->query($sql);
$dbh->exit_if_error($resp[0], $resp[1]);
$dbh->jsonReply($resp);
exit;
}
if ($p["cmd"] == "getdoctor") {
$resp = $dbh->connect();
$dbh->exit_if_error($resp[0], $resp[1]);
$resp = [];
$sql = "SELECT *
FROM a_doctor
WHERE
A_DoctorMetaActive = 'Y'
LIMIT 25";
$resp = $dbh->query($sql);
$dbh->exit_if_error($resp[0], $resp[1]);
$dbh->jsonReply($resp);
exit;
}
if ($p["cmd"] == "listingorder") {
$resp = $dbh->connect();
$dbh->exit_if_error($resp[0], $resp[1]);
$resp = [];
if (DateTime::createFromFormat('Y-m-d', $p['startdate']) == false) {
echo json_encode(["status" => "ERR", "message" => "Order Date required!"]);
exit;
}
$number_limit = 10;
$number_offset = (!isset($p['current_page']) ? 1 : $p['current_page'] - 1) * $number_limit;
$sql = "SELECT *, DATE_FORMAT(M_PatientDOB,'%d-%m-%Y') as dob_ina
FROM one_clinic.order
JOIN m_patient ON OrderM_PatientID = M_PatientID
WHERE
orderIsActive = 'Y' AND
(
orderNumbering LIKE CONCAT('%',?,'%') OR
M_PatientName LIKE CONCAT('%',?,'%') OR
M_PatientNoReg LIKE CONCAT('%',?,'%')
) AND
( DATE(orderDate) = ? )
LIMIT $number_limit OFFSET $number_offset";
$resp = $dbh->query($sql, array($p['search'], $p['search'], $p['search'], $p['startdate']));
$dbh->exit_if_error($resp[0], $resp[1]);
$dbh->jsonReply($resp);
exit;
}
if ($p["cmd"] == "deleteorder") {
$resp = $dbh->connect();
$dbh->exit_if_error($resp[0], $resp[1]);
$resp = [];
$sql = "UPDATE one_clinic.order SET
orderIsActive = 'N',
orderDeleteTime = NOW(),
orderDeleteUserID = 1
WHERE
orderID = ?";
$resp = $dbh->query($sql, array($p['orderID']));
$dbh->exit_if_error($resp[0], $resp[1]);
$sql = "INSERT INTO one_clinic.order_log(
orderLogOrderNumber,
orderLogStatus,
orderLogCreated,
orderLogUserID
)
VALUES(?,'DELETE',NOW(),1)";
$resp_log = $dbh->query($sql, array($p['orderNumber']));
$dbh->exit_if_error($resp_log[0], $resp_log[1]);
$dbh->jsonReply($resp);
exit;
}
if ($p["cmd"] == "updateorder") {
$resp = $dbh->connect();
$dbh->exit_if_error($resp[0], $resp[1]);
$resp = [];
$orderDate = isset($p["orderDate"]) && $p["orderDate"] != "" ? date('Y-m-d', strtotime($p["orderDate"])) : "";
$sql = "SELECT * FROM m_patient WHERE M_PatientID = ?";
$resp_pat = $dbh->query($sql, array($p['M_PatientID']));
$dbh->exit_if_error($resp_pat[0], $resp_pat[1]);
$data_patient = $resp_pat[1][0];
$tanggal_lahir = $data_patient['M_PatientDOB'];
//echo $tanggal_lahir;
// Ambil tanggal sekarang
$dateorder = isset($p["orderDate"]) && $p["orderDate"] != "" ? date('Y-m-d', strtotime($p["orderDate"])) : "";
$tanggal_sekarang = new DateTime($dateorder);
// Ubah tanggal lahir ke objek DateTime
$tanggal_lahir = new DateTime($tanggal_lahir);
// Hitung selisih antara tanggal sekarang dan tanggal lahir
$perbedaan = $tanggal_sekarang->diff($tanggal_lahir);
// Dapatkan tahun, bulan, dan hari dari selisih
$umur_tahun = $perbedaan->y;
$umur_bulan = $perbedaan->m;
$umur_hari = $perbedaan->d;
$age_now = $umur_tahun . " Tahun" . $umur_bulan . " Bulan" . $umur_hari . " Hari";
$sql = "UPDATE one_clinic.order SET
orderDate = ?,
orderM_PatientID = ?,
orderAge = ?,
orderNurseID = ?,
orderM_CompanyID = ?,
orderKeluhan = ?,
orderObjective = ?,
orderKesimpulan = ?,
orderSaran = ?,
orderLastUpdated = NOW(),
orderLastUpdateUserID = 1
WHERE
orderID = ?";
$resp = $dbh->query($sql, array(
$orderDate,
$p['M_PatientID'],
$age_now,
$p['NurseID'],
$p['M_CompanyID'],
$p['Keluhan'],
$p['Objective'],
$p['Kesimpulan'],
$p['Saran'],
$p['orderID']
));
$dbh->exit_if_error($resp[0], $resp[1]);
$sql = "SELECT order.*
FROM one_clinic.order
WHERE
orderID = ?";
$resp_order = $dbh->query($sql, array($p['orderID']));
$dbh->exit_if_error($resp_order[0], $resp_order[1]);
$order_before = $resp_order[1][0];
$sql = "INSERT INTO one_clinic.order_log(
orderLogOrderNumber,
orderLogStatus,
orderLogJSONBefore,
orderLogJSONAfter,
orderLogCreated,
orderLogUserID
)
VALUES(?,'UPDATE',?,?,NOW(),1)";
$resp_log = $dbh->query($sql, array(
$p['orderNumber'],
json_encode($order_before),
json_encode($p)
));
$dbh->exit_if_error($resp_log[0], $resp_log[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_clinic";
}
public function jsonReply($resp)
{
echo json_encode(["status" => "OK", "data" => $resp[1]]);
}
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];
}
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;
}
}