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

243 lines
7.6 KiB
PHP

<?php
class Patient extends MY_Controller
{
function __construct()
{
parent::__construct();
}
function get_param()
{
$raw = file_get_contents("php://input");
return json_decode($raw, true);
}
function index()
{
$this->last_day();
}
function last_day($last_days = 3)
{
$sql = "select distinct m_patient.*
from t_orderheader
join m_patient on T_OrderHeaderDate + interval ? day >= now()
and T_OrderHeaderIsActive = 'Y'
and T_OrderHeaderM_PatientID = M_PatientID
and M_PatientIsActive = 'Y'
and ( M_PatientM_IdTypeID = 1 or M_PatientM_IdTypeID = 2)
and M_PatientIDNumber is not null
and M_PatientIDNumber <> ''";
$resp = $this->get_rows($sql, [$last_days]);
if ($resp["status"] == -1) {
$this->error_reply_gz([
"status" => "ERR",
"message" => $resp["message"],
]);
}
$patient = $resp["data"];
$s_ids = "-1";
foreach ($resp["data"] as $r) {
$s_ids .= "," . $r["M_PatientID"];
}
list($branchID, $branchCode) = $this->get_branch();
$branch = ["branchID" => $branchID, "branchCode" => $branchCode];
// address
$sql = "select * from m_patientaddress
where M_PatientAddressM_PatientID in ( $s_ids)
and M_PatientAddressIsActive = 'Y'";
$resp = $this->get_rows($sql);
if ($resp["status"] == -1) {
$this->error_reply_gz([
"status" => "ERR",
"message" => $resp["message"],
]);
}
$address = $resp["data"];
$s_ids = "-1";
foreach ($address as $r) {
$s_ids .= "," . $r["M_PatientAddressM_KelurahanID"];
}
//kelurahan
$sql = "select * from m_kelurahan where M_KelurahanID in ($s_ids)";
$resp = $this->get_rows($sql);
if ($resp["status"] == -1) {
$this->error_reply_gz([
"status" => "ERR",
"message" => $resp["message"],
]);
}
$kelurahan = $resp["data"];
//district;
$s_ids = "-1";
foreach ($kelurahan as $r) {
$s_ids .= "," . $r["M_KelurahanM_DistrictID"];
}
$sql = "select * from m_district where M_DistrictID in($s_ids)";
$resp = $this->get_rows($sql);
if ($resp["status"] == -1) {
$this->error_reply_gz([
"status" => "ERR",
"message" => $resp["message"],
]);
}
$district = $resp["data"];
//city;
$s_ids = "-1";
foreach ($district as $r) {
$s_ids .= "," . $r["M_DistrictM_CityID"];
}
$sql = "select * from m_city where M_CityID in($s_ids)";
$resp = $this->get_rows($sql);
if ($resp["status"] == -1) {
$this->error_reply_gz([
"status" => "ERR",
"message" => $resp["message"],
]);
}
$city = $resp["data"];
//province
$s_ids = "-1";
foreach ($city as $r) {
$s_ids .= "," . $r["M_CityM_ProvinceID"];
}
$sql = "select * from m_province where M_ProvinceID in($s_ids)";
$resp = $this->get_rows($sql);
if ($resp["status"] == -1) {
$this->error_reply_gz([
"status" => "ERR",
"message" => $resp["message"],
]);
}
$province = $resp["data"];
foreach ($patient as $idx => $p) {
$pa = array_filter($address,function($a) use($p) {
return $a["M_PatientAddressM_PatientID"] == $p["M_PatientID"];
});
foreach($pa as $pa_idx => $addr) {
$tmp = array_filter($kelurahan,function($k) use($addr) {
return $k["M_KelurahanID"] == $addr["M_PatientAddressM_KelurahanID"];
} );
$sel_kelurahan = [];
$sel_district = [];
$sel_city = [];
$sel_province = [];
if(count($tmp) > 0) {
$sel_kelurahan = $tmp[0];
$tmp = array_filter($district,function($d) use($sel_kelurahan) {
return $sel_kelurahan["M_KelurahanM_DistrictID"] = $d["M_DistrictID"];
});
if (count($tmp) > 0) {
$sel_district = $tmp[0];
$tmp = array_filter($city,function($c) use($sel_district) {
return $sel_district["M_DistrictM_CityID"] = $c["M_CityID"];
});
if (count($tmp) > 0) {
$sel_city = $tmp[0];
$tmp = array_filter($province,function($p) use($sel_city) {
return $sel_city["M_CityM_ProvinceID"] = $p["M_ProvinceID"];
});
if (count($tmp) > 0) {
$sel_province = $tmp[0];
}
}
}
}
$pa[$pa_idx]["kelurahan"] = $sel_kelurahan;
$pa[$pa_idx]["district"] = $sel_district;
$pa[$pa_idx]["city"] = $sel_city;
$pa[$pa_idx]["province"] = $sel_province;
}
$patient[$idx]["address"] = $pa;
if ($p["M_PatientPhoto"] != "") {
$patient[$idx]["PhotoBase64"] = $this->get_photo($p["M_PatientPhoto"]);
}
}
//foto
$this->reply_gz([
"status" => "OK",
"patient" => $patient,
"branch" => $branch,
]);
}
function get_photo($path)
{
$fname = "/home/one/project/one" . $path;
if (file_exists($fname)) {
$data = file_get_contents($fname);
if (strlen($data) > 0) {
return base64_encode($data);
}
}
return "";
}
//Get Detail Mou
function reply_gz($resp)
{
echo gzcompress(json_encode($resp));
exit();
}
function get_branch()
{
$sql =
"select M_BranchID,M_BranchCode from m_branch where M_BranchIsActive='Y' and M_BranchIsDefault ='Y'";
$resp = $this->get_row($sql);
if ($resp["status"] != 1) {
echo json_encode($resp);
exit();
}
return [$resp["data"]["M_BranchID"], $resp["data"]["M_BranchCode"]];
}
function log($msg)
{
$dt = date("Y-m-d H:i:s");
echo "$dt $msg\n";
}
function error_reply_gz($msg)
{
echo gzcompress(json_encode(["status" => "ERR", "message" => $msg]));
exit();
}
function reply($resp)
{
echo json_encode($resp);
exit();
}
function error_reply($msg)
{
echo json_encode(["status" => "ERR", "message" => $msg]);
exit();
}
function get_rows($sql, $param = false)
{
if ($param) {
$qry = $this->db->query($sql, $param);
} else {
$qry = $this->db->query($sql);
}
if (!$qry) {
return [
"status" => -1,
"message" =>
$this->db->last_query() .
"|" .
$this->db->error()["message"],
];
}
return ["status" => 0, "data" => $qry->result_array()];
}
function get_row($sql, $param = false)
{
$resp = $this->get_rows($sql, $param);
if ($resp["status"] == -1) {
return $resp;
}
if (count($resp["data"]) == 0) {
return ["status" => 0, "message" => "Not found."];
}
return ["status" => 1, "data" => $resp["data"][0]];
}
}
?>