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

182 lines
5.7 KiB
PHP

<?php
class Nat_patient_api extends MY_Controller
{
function index()
{
}
function validasi()
{
$param = $this->get_param();
$branchCode = $param["branchCode"];
$username = $param["username"];
$this->db->trans_begin();
$sql = "insert into nat_patient.stage_03(M_BranchID,M_BranchCode,Username)
select M_BranchID,M_BranchCode,?
from nat_patient.m_branch where M_BranchCode=? ";
$qry = $this->db->query($sql, [$username, $branchCode]);
if (!$qry) {
echo json_encode([
"status" => "ERR",
"message" => $this->db->error()["message"],
]);
$this->db->trans_rollback();
exit();
}
$stage03ID = $this->db->insert_id();
$sql = "insert into nat_patient.stage_03_detail(stage03ID,M_BranchID,
M_BranchCode,M_PatientID,Status)
select ?,M_BranchID,M_BranchCode,?,?
from nat_patient.m_branch where M_BranchCode =?";
foreach ($param["data"] as $d) {
$M_PatientID = $d["M_PatientID"];
$M_BranchCode = $d["branchCode"];
$Status = $d["Status"];
$qry = $this->db->query($sql, [
$stage03ID,
$M_PatientID,
$Status,
$M_BranchCode,
]);
if (!$qry) {
echo json_encode([
"status" => "ERR",
"message" => $this->db->error()["message"],
]);
$this->db->trans_rollback();
exit();
}
}
echo json_encode([
"status" => "OK",
"message" => "Validasi updated",
]);
$this->db->trans_commit();
}
function get_param()
{
$j_data = file_get_contents("php://input");
$data = json_decode($j_data, true);
return $data;
}
function search($nik)
{
$sql = "select nat_patient.fn_simple_nik_check(?) as simple_check";
$qry = $this->db->query($sql, [$nik]);
if (!$qry) {
echo json_encode([
"status" => "ERR",
"message" => $this->db->error()["message"],
]);
exit();
}
$rows = $qry->result_array();
if (count($rows) == 0) {
echo json_encode([
"status" => "ERR",
"message" => "Invalid NIK : $nik",
]);
exit();
}
if ($rows[0]["simple_check"] != "Y") {
echo json_encode([
"status" => "ERR",
"message" => "Invalid NIK : $nik | " . $rows[0]["simple_check"],
]);
exit();
}
$sql = "select * from nat_patient.stage_02 where nik=?";
$qry = $this->db->query($sql, [$nik]);
if (!$qry) {
echo json_encode([
"status" => "ERR",
"message" => $this->db->error()["message"],
]);
exit();
}
$rows = $qry->result_array();
if (count($rows) == 0) {
echo json_encode([
"status" => "ERR",
"message" => "NIK $nik not found",
]);
exit();
}
$total = $rows[0]["total"];
$j_member = $rows[0]["member"];
$member = json_decode($j_member, true);
$sql = "select *
from nat_patient.nat_patient np
where ";
$w_sql = "";
$w_sql_addr = "";
$param = [];
foreach ($member as $m) {
$pat_member = $m["member"];
foreach ($pat_member as $pid) {
if ($w_sql != "") {
$w_sql .= " OR ";
}
if ($w_sql_addr != "") {
$w_sql_addr .= " OR ";
}
$w_sql .= " (np.M_BranchID =? and np.M_PatientID = ?)";
$w_sql_addr .=
" (na.M_BranchID =? and na.Nat_PatientAddressM_PatientID = ?)";
$param[] = $m["M_BranchID"];
$param[] = $pid;
}
}
$sql = $sql . $w_sql;
$qry = $this->db->query($sql, $param);
if (!$qry) {
echo json_encode([
"status" => "ERR",
"message" =>
$this->db->error()["message"] .
"|\n" .
$this->db->last_query(),
]);
exit();
}
$rows_patient = $qry->result_array();
$sql = "select *
from nat_patient.nat_patientaddress na
where $w_sql_addr";
$qry = $this->db->query($sql, $param);
if (!$qry) {
echo json_encode([
"status" => "ERR",
"message" =>
$this->db->error()["message"] .
"|\n" .
$this->db->last_query(),
]);
exit();
}
$rows_addr = $qry->result_array();
//global address
foreach ($rows_patient as $idx => $p) {
$p_addr = array_filter($rows_addr, function ($addr) use ($p) {
if (
$addr["M_BranchID"] == $p["M_BranchID"] &&
$p["M_PatientID"] == $addr["Nat_PatientAddressM_PatientID"]
) {
return true;
}
return false;
});
$np_addr = [];
//reset index
foreach ($p_addr as $p) {
$np_addr[] = $p;
}
$rows_patient[$idx]["address"] = $np_addr;
}
echo json_encode([
"status" => "OK",
"data" => $rows_patient,
"all_address" => $rows_addr,
]);
}
}