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

167 lines
6.2 KiB
PHP

<?php
class Populate_patient extends MY_Controller
{
function __construct()
{
parent::__construct();
$this->db->query("use pat_lookup");
}
function go()
{
$source = ["pat_riau", "pat_toha", "pat_pajajaran", "pat_cimahi"];
foreach ($source as $src) {
$this->db->query("use $src");
$sql = "select count(*) as total from m_patient";
$total = $this->db->query($sql)->result_array()[0]["total"];
$sql = "select M_BranchID,M_BranchCode,M_BranchS_RegionalID
from m_branch where M_BranchIsDefault = 'Y'
and M_BranchIsActive = 'Y'";
$qry = $this->db->query($sql);
if (!$qry) {
echo "Error : " . $this->db->error()["message"];
echo "Error : " . $this->db->last_query() . "\n";
exit;
}
$rows = $qry->result_array();
if (count($rows) == 0) {
echo "No Default Branch";
exit;
}
$regionalID = $rows[0]["M_BranchS_RegionalID"];
$branchID = $rows[0]["M_BranchID"];
$branchCode = $rows[0]["M_BranchCode"];
$batch = 100;
for ($idx = 0; $idx < $total; $idx = $idx + $batch) {
$sql = "select
M_PatientID,
M_PatientName,
concat(
M_TitleName, ' ',
if(M_PatientPrefix is not null and trim(M_PatientPrefix) <> '', concat(M_PatientPrefix,' '),''),
M_PatientName,
if(M_PatientSuffix is not null and trim(M_PatientSuffix) <> '', concat(' ',M_PatientSuffix),'')
) patientName,
M_PatientDOB patientDOB,
if (M_PatientM_IdTypeID = 1 and length(M_PatientIdNumber) = 16, M_PatientIdNumber,'') patientNIK,
M_PatientEmail,
M_PatientHP,
case
when M_PatientM_SexID = 1 then 'M'
when M_PatientM_SexID = 2 then 'F'
else 'U'
end patientSex,
group_concat(concat(M_PatientAddressDescription, ' ', ifnull(M_CityName,'')) separator '\n')
patientAddress
from m_patient
join m_title on M_PatientM_TitleID = M_TitleID
join m_patientaddress on M_PatientAddressM_PatientID = M_PatientID
left join m_kelurahan on M_PatientAddressM_KelurahanID = M_KelurahanID
left join m_district on M_KelurahanM_DistrictID = M_DistrictID
left join m_city on M_DistrictM_CityID = M_CityID
group by M_PatientID
limit ?,?";
$qry = $this->db->query($sql, [$idx, $batch]);
if (!$qry) {
echo "Error : " . $this->db->error()["message"];
echo "Error : " . $this->db->last_query() . "\n";
exit;
}
$rows = $qry->result_array();
if (count($rows) == 0) {
continue;
}
$s_ids = "0";
foreach ($rows as $r) {
$s_ids .= "," . $r["M_PatientID"];
}
$sql = "select * from m_patient where M_PatientID in ($s_ids)";
$qry = $this->db->query($sql);
if (!$qry) {
echo "Error : " . $this->db->error()["message"];
echo "Error : " . $this->db->last_query() . "\n";
exit;
}
$rows_patient = $qry->result_array();
$sql = "select * from m_patientaddress where M_PatientAddressM_PatientID in($s_ids)";
$qry = $this->db->query($sql);
if (!$qry) {
echo "Error : " . $this->db->error()["message"];
echo "Error : " . $this->db->last_query() . "\n";
exit;
}
$rows_address = $qry->result_array();
foreach ($rows as $r) {
$f_patient = array_values(array_filter($rows_patient, function ($rp) use ($r) {
return $rp["M_PatientID"] == $r["M_PatientID"];
}));
$bz_patient = [];
if (count($f_patient) > 0) $bz_patient = $f_patient[0];
$f_address = array_values(array_filter($rows_address, function ($ra) use ($r) {
return $ra["M_PatientAddressM_PatientID"] == $r["M_PatientID"];
}));
$patientJson = json_encode(["patient" => $bz_patient, "address" => $f_address]);
$full_text = "";
//nik
if ($r["patientNIK"] != "") $full_text .= $r["patientNIK"];
//dob
if ($r["M_PatientName"] != "") {
if ($full_text != "") $full_text .= " ";
$full_text .= $r["M_PatientName"];
}
if ($r["patientDOB"] != "") {
if ($full_text != "") $full_text .= " ";
$dob_tm = strtotime($r["patientDOB"]);
$full_text .= date("dmY", $dob_tm) . " " . date("dmy", $dob_tm) .
" " . date("mY", $dob_tm) . " " . date("my", $dob_tm);
}
//hp
if ($r["M_PatientHP"] != "") {
if ($full_text != "") $full_text .= " ";
$hp = $r["M_PatientHP"];
$full_text .= $r["M_PatientHP"];
if (strlen($hp) > 5) {
$full_text .= " " . substr($hp, -3) . " " . substr($hp, -4) . " " .
substr($hp, -5);
}
}
if ($r["M_PatientEmail"] != "") {
if ($full_text != "") $full_text .= " ";
$full_text .= $r["M_PatientEmail"];
}
if ($r["patientAddress"] != "") {
if ($full_text != "") $full_text .= " ";
$full_text .= $r["patientAddress"];
}
$new_data = [
"patientName" => $r["patientName"],
"patientAddress" => $r["patientAddress"],
"patientJson" => $patientJson,
"patientName" => $r["patientName"],
"patientDOB" => $r["patientDOB"],
"patientNIK" => $r["patientNIK"],
"patientEmail" => $r["M_PatientEmail"],
"patientHp" => $r["M_PatientHP"],
"patientAddress" => $r["patientAddress"],
"patientSex" => $r["patientSex"],
"patientText" => $full_text,
"patientBizoneID" => $r["M_PatientID"],
"patientRegionalID" => $regionalID,
"patientM_BranchID" => $branchID,
"patientM_BranchCode" => $branchCode
];
$qry = $this->db->insert("pat_lookup.patient_v2", $new_data);
if (!$qry) {
echo "Error : " . $this->db->error()["message"];
echo "Error : " . $this->db->last_query() . "\n";
exit;
}
}
sleep(1);
}
}
}
}