Files
2026-05-25 20:01:37 +07:00

247 lines
8.0 KiB
PHP

<?php
class Testref extends MY_Controller
{
var $db_smartone;
public function index()
{
echo "TEST REF MASTER API";
}
public function __construct()
{
parent::__construct();
$this->db_smartone = $this->load->database("onedev", true);
}
public function search()
{
$prm = $this->sys_input;
$max_rst = 10;
$tot_count = 0;
$page = $prm['page'];
if ($page == null)
$page = 1;
$offset = ($page - 1) * $max_rst;
$q = [
'search' => isset($prm['search']) ? "%{$prm['search']}%" : '%',
'company_id'=> isset($prm['company_id']) ? $prm['company_id'] : '0'
];
// QUERY TOTAL
$sql = "SELECT COUNT(Nat_TestID) total
FROM nat_test
LEFT JOIN t_testref ON T_TestRefNat_TestID = Nat_TestID
AND ((T_TestRefM_CompanyID = ? and ? <> 0) OR ? = 0)
AND T_TestRefIsActive = 'Y'
LEFT JOIN m_company ON T_TestRefM_CompanyID = M_CompanyID
WHERE (Nat_TestName LIKE ? OR Nat_TestCode LIKE ?)";
$query = $this->db_smartone->query($sql, array($q['company_id'], $q['company_id'], $q['company_id'],
$q['search'], $q['search']));
if ($query) {
$tot_count = $query->result_array()[0]["total"];
}
else {
$this->sys_error_db("Test Ref Master count",$this->db_smartone);
exit;
}
$sql = "SELECT T_TestRefID, Nat_TestID, Nat_TestName, Nat_TestCode, M_CompanyID, M_CompanyName,
T_TestRefIsInternal, M_BranchID, M_BranchName, T_SampleTypeID, T_SampleTypeName
FROM nat_test
LEFT JOIN t_testref ON T_TestRefNat_TestID = Nat_TestID
AND ((T_TestRefM_CompanyID = ? and ? <> 0) OR ? = 0)
AND T_TestRefIsActive = 'Y'
LEFT JOIN m_company ON T_TestRefM_CompanyID = M_CompanyID
LEFT JOIN m_branch ON T_TestRefM_BranchID = M_BranchID
LEFT JOIN t_sampletype ON T_SampleTypeID = T_TestRefT_SampleTypeID
WHERE (Nat_TestName LIKE ? OR Nat_TestCode LIKE ?)
limit {$offset}, {$max_rst}";
$query = $this->db_smartone->query($sql, array($q['company_id'], $q['company_id'], $q['company_id'],
$q['search'], $q['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));
$this->sys_ok($result);
}
else {
$this->sys_error_db("Test Ref Master rows",$this->db_smartone);
exit;
}
}
public function search_company_all()
{
$prm = $this->sys_input;
$max_rst = 12;
$tot_count =0;
$q = [
'search' => '%'
];
if ($prm['search'] != '')
{
$q['search'] = "%{$prm['search']}%";
}
$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'
and M_MouIsApproved = 'Y' and M_MouStartDate <= date(now()) and M_MouEndDate >= date(now())
where M_CompanyIsActive = '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_branch_all()
{
$prm = $this->sys_input;
$sql = "SELECT M_BranchID, M_BranchName
FROM m_branch
WHERE M_BranchIsActive = 'Y'
ORDER BY M_BranchName";
$query = $this->db_smartone->query($sql);
if ($query)
{
$rows = $query->result_array();
$result = array("records" => $rows);
$this->sys_ok($result);
}
else
{
$this->sys_error_db("Branch rows",$this->db_smartone);
exit;
}
}
public function save()
{
$prm = $this->sys_input;
$data = (array) json_decode($prm['data']);
if (!isset($data['company_id']))
$data['company_id'] = 0;
if (!isset($data['branch_id']))
$data['branch_id'] = 0;
// overwrite
$prm = $data;
if (isset($prm['testref_id']))
{
$sql = "UPDATE t_testref
SET T_TestRefIsInternal = ?,
T_TestRefM_BranchID = ?,
T_TestRefM_CompanyID= ?,
T_TestRefT_SampleTypeID = ?
WHERE T_TestRefID = ?";
$query = $this->db_smartone->query($sql, [$prm['is_internal'], $prm['branch_id'], $prm['company_id'], $prm['sampletype_id'], $prm['testref_id']]);
}
else
{
$sql = "INSERT INTO t_testref(T_TestRefIsInternal, T_TestRefM_BranchID, T_TestRefM_CompanyID, T_TestRefNat_TestID, T_TestRefT_SampleTypeID)
VALUES(?, ?, ?, ?, ?)";
$query = $this->db_smartone->query($sql, [$prm['is_internal'], $prm['branch_id'], $prm['company_id'], $prm['nattest_id'], $prm['sampletype_id']]);
}
if ($query)
{
$this->sys_ok($this->db_smartone->insert_id());
return;
}
else
{
$this->sys_error_db("Test Ref Insert Update",$this->db_smartone);
}
}
public function remove()
{
$prm = $this->sys_input;
if (isset($prm['testref_id']))
{
$sql = "UPDATE t_testref
SET T_TestRefIsActive = 'N', T_TestRefUserID = ?
WHERE T_TestRefID = ?";
$query = $this->db_smartone->query($sql, [$this->sys_user['M_UserID'], $prm['testref_id']]);
if ($query)
{
$this->sys_ok("OK");
return;
}
else
{
$this->sys_error_db("Test Ref Remove",$this->db_smartone);
}
}
else
{
$this->sys_error_db("Test Ref Remove",$this->db_smartone);
}
}
public function search_sampletype()
{
$prm = $this->sys_input;
$max_rst = 12;
$tot_count = 0;
$q = [
'search' => '%'
];
if ($prm['search'] != '')
{
$q['search'] = "%{$prm['search']}%";
}
$sql = "select T_SampleTypeID, T_SampleTypeName
from t_sampletype
where T_SampleTypeIsActive = 'Y'
and T_SampleTypeName like ?
order by T_SampleTypeName";
$query = $this->db_smartone->query($sql, array($q['search']));
if ($query) {
$rows = $query->result_array();
$result = array("total" => $tot_count, "records" => $rows, "total_display" => sizeof($rows));
$this->sys_ok($result);
}
else {
$this->sys_error_db("Sample Type rows",$this->db_smartone);
exit;
}
}
}
?>