Files
BE_CPONE/application/controllers/mockup/process/referenceout/Ro_master.php
2026-04-27 10:31:17 +07:00

230 lines
7.4 KiB
PHP

<?php
class Ro_master extends MY_Controller
{
var $db_smartone;
public function index()
{
echo "RO Master Reference API";
}
public function __construct()
{
parent::__construct();
$this->db_smartone = $this->load->database("onedev", true);
}
public function search()
{
$prm = $this->sys_input;
$max_rst = 10;
$search = '%' . $prm["search"] . '%';
$page = $prm['page'];
if ($page == null)
$page = 1;
$offset = ($page - 1) * $max_rst;
// QUERY TOTAL
$sql = "select count(DISTINCT t_orderrefmasterID) total
from t_orderrefmaster
join t_test on t_orderrefmastert_testid = t_testid
join m_company on t_orderrefmastertom_companyid = m_companyid
where T_OrderRefMasterIsActive = 'Y'
and T_TestName LIKE ?
order by T_TestName ASC";
$query = $this->db_smartone->query($sql, [$search]);
if ($query) {
$tot_count = $query->result_array()[0]["total"];
}
else {
$this->sys_error_db("RO Master Reference API Count", $this->db_smartone);
exit;
}
$sql = "select T_OrderRefMasterID, T_TestID, T_TestName, T_TestCode,
IFNULL(M_CompanyID, 0) M_CompanyID, IFNULL(M_CompanyName, '') M_CompanyName,
IFNULL(M_BranchID, 0) M_BranchID, IFNULL(M_BranchName, '') M_BranchName,
t_orderrefmasterisinternal is_internal,
IFNULL(S_RegionalID, 0) S_RegionalID, IFNULL(S_RegionalName, '') S_RegionalName
from t_orderrefmaster
join t_test on t_orderrefmastert_testid = t_testid
left join m_company on t_orderrefmastertom_companyid = m_companyid
left join m_branch on t_orderrefmastertom_branchid = m_branchid
left join s_regional on t_orderrefmastertos_regionalid = s_regionalid
where T_OrderRefMasterIsActive = 'Y'
and T_TestName LIKE ?
order by T_TestName ASC
limit {$offset}, {$max_rst}";
$query = $this->db_smartone->query($sql, [$search]);
if ($query) {
$rows = $query->result_array();
$result = array("total" => $tot_count, "total_page" => ceil($tot_count/$max_rst), "cur_page" => $page, "records" => $rows, "total_display" => sizeof($rows), "q" => $this->db_smartone->last_query());
$this->sys_ok($result);
}
else {
$this->sys_error_db("RO Master Reference API", $this->db_smartone);
exit;
}
}
public function search_company()
{
$prm = $this->sys_input;
$max_rst = 12;
$tot_count =0;
$q = [
'search' => '%'
];
if ($prm['search'] != '')
{
$q['search'] = "%{$prm['search']}%";
}
// QUERY TOTAL
$sql = "select count(*) total
from
m_company
where M_CompanyIsActive = 'Y'
and M_CompanyIsLabTo = 'Y'
and M_CompanyName like ?";
$query = $this->db_smartone->query($sql, array($q['search']));
if ($query) {
$tot_count = $query->result_array()[0]["total"];
}
else {
$this->sys_error_db("m_patient count",$this->db_smartone);
exit;
}
$sql = "select M_CompanyID, M_CompanyName,
IFNULL( concat('[', group_concat( json_object('M_MouID', M_MouID, 'M_MouName', M_MouName, 'M_MouStartDate', M_MouStartDate, 'M_MouEndDate', M_MouEndDate) ), ']'), '[]') as mou
from m_company
left join m_mou on M_MouM_CompanyID = M_CompanyID and M_MouIsActive = 'Y'
where M_CompanyIsActive = 'Y'
and M_CompanyIsLabTo = 'Y'
and M_CompanyName like ?
group by m_companyid";
$query = $this->db_smartone->query($sql, array($q['search']));
if ($query) {
$rows = $query->result_array();
foreach ($rows as $k => $v)
$rows[$k]['mou'] = json_decode($v['mou']);
$result = array("total" => $tot_count, "records" => $rows, "total_display" => sizeof($rows));
$this->sys_ok($result);
}
else {
$this->sys_error_db("m_company rows",$this->db_smartone);
exit;
}
}
public function search_px()
{
$prm = $this->sys_input;
$search = $prm["search"];
$exclude = join(",", json_decode($prm["exclude"]));
$sql_param = array("%$search%", $exclude);
$sql = "select count(distinct T_TestID) total
from t_test
where T_TestName like ?
and T_TestIsResult = 'Y'
and NOT FIND_IN_SET(T_TestID, ?)";
$query = $this->db_smartone->query($sql, $sql_param);
$tot_count =0;
if ($query) {
$tot_count = $query->result_array()[0]["total"];
} else {
$this->sys_error_db("PX count", $this->db_smartone);
exit;
}
$sql = "select distinct T_TestID, T_TestName, 'N' IsFromPanel, T_TestRequirement
from t_test
where T_TestName like ?
and T_TestIsResult = 'Y'
and NOT FIND_IN_SET(T_TestID, ?)
order by T_TestName ASC
limit 0, 20";
$query = $this->db_smartone->query($sql, $sql_param);
$rows = $query->result_array();
$result = array("total" => $tot_count, "records" => $rows, "query" => $this->db_smartone->last_query() );
$this->sys_ok($result);
exit;
}
public function save()
{
$prm = $this->sys_input;
$data = $prm['data'];
$sql = "CALL sp_process_ref_master_save('{$data}');";
$query = $this->db_smartone->query($sql);
if ($query)
{
$rst = $query->row();
echo json_encode($rst);
}
else
{
$this->sys_error_db("save master", $this->db_smartone);
exit;
}
}
public function search_regional()
{
$prm = $this->sys_input;
$sql = "select count(*) total
from s_regional
where S_RegionalIsActive = 'Y'";
$query = $this->db_smartone->query($sql);
$tot_count =0;
if ($query) {
$tot_count = $query->result_array()[0]["total"];
} else {
$this->sys_error_db("Regional count", $this->db_smartone);
exit;
}
$sql = "select S_RegionalID, S_RegionalName,
CONCAT('[', GROUP_CONCAT(JSON_OBJECT('M_BranchID', M_BranchID, 'M_BranchName', M_BranchName) SEPARATOR ','), ']') branches
from s_regional
left join m_branch on m_branchs_regionalid = s_regionalid and m_branchisactive = 'Y'
where S_RegionalIsActive = 'Y'
group by S_RegionalID
order by S_RegionalName ASC";
$query = $this->db_smartone->query($sql);
if ($query)
{
$rows = $query->result_array();
foreach ($rows as $k => $v)
{
$rows[$k]['branches'] = json_decode($v['branches']);
}
$result = array("total" => $tot_count, "records" => $rows, "query" => $this->db_smartone->last_query() );
$this->sys_ok($result);
exit;
}
}
}