Files
2026-04-15 15:23:57 +07:00

169 lines
5.9 KiB
PHP

<?php
class Company extends MY_Controller
{
var $db_smartone;
public function index()
{
echo "Company API";
}
public function __construct()
{
parent::__construct();
$this->db_smartone = $this->load->database("onedev", true);
}
function _add_mou(&$companies) {
if (count($companies) == 0) {
return;
}
$company_list= "-1";
foreach($companies as $idx => $c) {
$company_list .= ", " . $c["M_PatientTypeID"];
if (! isset($companies[$idx]["mou"])) $companies[$idx]["mou"] = array();
}
$sql = "select *
from
m_moucompany
where M_MouCompanyM_PatientTypeID in ( $company_list )
and ( M_MouCompanyStartDate <= now() and M_MouCompanyEndDate >= now() )
and M_MouCompanyIsActive = 'Y'";
$query = $this->db_smartone->query($sql);
if ($query) {
$rows= $query->result_array();
foreach($rows as $r) {
$patientTypeID= $r["M_MouCompanyM_PatientTypeID"];
foreach($companies as $idx => $c) {
if($c["M_PatientTypeID"] == $patientTypeID) {
$companies[$idx]["mou"][] = $r;
}
}
}
} else {
$this->sys_error_db("m_moucompany mou",$this->db_smartone);
exit;
}
}
public function search()
{
$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
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()) AND M_MouIsReleased = 'Y'
where M_CompanyIsActive = 'Y'";
$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, 'M_MouNote', M_MouNote, 'M_MouIsBill', M_MouIsBill, 'M_MouEmail', M_MouEmail, 'M_MouIsDefault', M_MouIsDefault, 'M_MouEmailIsDefault', M_MouEmailIsDefault, 'delivery_email_code', `fn_fo_delivery_code`('MOU', 'EMAIL', '0')) ), ']'), '[]') as mou
from m_company
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()) AND M_MouIsReleased = 'Y'
where M_CompanyIsActive = 'Y'
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_default()
{
$prm = $this->sys_input;
$mouid = $prm['mouid'];
$sql = "SELECT M_MouID, M_MouM_CompanyID FROM m_mou
JOIN m_company ON M_CompanyID = M_MouM_CompanyID ANd M_CompanyIsActive = 'Y'
WHERE M_MouID = $mouid";
$query = $this->db_smartone->query($sql);
if ($query)
{
$rows = $query->row();
$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, 'M_MouNote', M_MouNote, 'M_MouIsBill', M_MouIsBill, 'M_MouEmail', M_MouEmail, 'M_MouIsDefault', 'Y', 'M_MouEmailIsDefault', M_MouEmailIsDefault, 'delivery_email_code', `fn_fo_delivery_code`('MOU', 'EMAIL', '0')) ), ']'), '[]') as mou
from m_company
join m_mou on M_MouM_CompanyID = M_CompanyID and M_MouID = $mouid
group by m_companyid";
$query = $this->db_smartone->query($sql, array($rows->M_MouM_CompanyID));
$rows2 = $query->result_array();
foreach ($rows2 as $k => $v)
$rows2[$k]['mou'] = json_decode($v['mou']);
$result = array("total" => 1, "records" => $rows2, "total_display" => sizeof($rows));
$this->sys_ok($result);
}
else
{
$this->sys_error_db("m_company rows", $this->db_smartone);
exit;
}
}
public function search_()
{
$prm = $this->sys_input;
$search = $prm["search"];
$sql_param = array("%$search%");
$sql = "select count(*) total
from
m_patienttype
where
M_PatientTypeName like ? and
M_PatientTypeIsActive = 'Y'";
$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("m_patienttype count", $this->db_smartone);
exit;
}
$sql = "select M_PatientTypeID, M_PatientTypeName
from
m_patienttype
where
M_PatientTypeName like ? and
M_PatientTypeIsActive = 'Y'
limit 0,10";
$query = $this->db_smartone->query($sql,$sql_param);
$rows = $query->result_array();
$this->_add_mou($rows);
$result = array("total" => $tot_count, "records" => $rows );
$this->sys_ok($result);
exit;
}
}