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

221 lines
8.2 KiB
PHP

<?php
class Unitcompany extends MY_Controller
{
var $db_regional;
public function index()
{
echo "UNIT API";
}
public function __construct()
{
parent::__construct();
$this->db_regional = $this->load->database("regional", true);
}
function lookupunitbyname(){
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$unit = $prm['unit'];
$number_limit = 10;
$number_offset = ($prm['current_page'] - 1) * $number_limit ;
$sql = "select COUNT(*) as total
FROM(SELECT m_companytype.M_CompanyTypeID
from m_companytype
LEFT JOIN m_companytypegroup ON m_companytype.M_CompanyTypeM_CompanyTypeGroupID = m_companytypegroup.M_CompanyTypeGroupID
WHERE
(M_CompanyTypeName LIKE CONCAT('%','{$unit}','%') OR m_companytypegroup.M_CompanyTypeGroupName LIKE CONCAT('%','{$unit}','%')) AND
M_CompanyTypeIsActive = 'Y' GROUP BY M_CompanyTypeID) a";
$query = $this->db_regional->query($sql);
$tot_count = 0;
$tot_page = 0;
if ($query) {
$tot_count = $query->result_array()[0]["total"];
$tot_page = ceil($tot_count/$number_limit);
} else {
$this->sys_error_db("m_companytype count", $this->db_regional);
exit;
}
$sql = "select M_CompanyTypeID as id,
M_CompanyTypeName as name,
M_CompanyTypeName as Nat_UnitName,
m_companytype.*,
m_companytypegroup.M_CompanyTypeGroupName as GroupName
from m_companytype
LEFT JOIN m_companytypegroup ON m_companytype.M_CompanyTypeM_CompanyTypeGroupID = m_companytypegroup.M_CompanyTypeGroupID
WHERE
(M_CompanyTypeName LIKE CONCAT('%','{$unit}','%') OR m_companytypegroup.M_CompanyTypeGroupName LIKE CONCAT('%','{$unit}','%')) AND
M_CompanyTypeIsActive = 'Y'
GROUP BY M_CompanyTypeID
ORDER BY GroupName ASC, M_CompanyTypeName ASC
limit $number_limit offset $number_offset";
$query = $this->db_regional->query($sql);
if ($query) {
$rows = $query->result_array();
foreach($rows as $key => $val){
$rows[$key]['code'] = $number_offset + $key + 1;
//penomeran
$rows[$key]['Nat_UnitCode'] = $number_offset + $key + 1;
}
} else {
$this->sys_error_db("m_companytype select");
exit;
}
$result = array ("total" => $tot_page, "total_filter"=>count($rows),"records" => $rows);
$this->sys_ok($result);
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
public function addnewunit()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
//# ambil parameter input
$prm = $this->sys_input;
$name = $prm['M_CompanyTypeName'];
$group_id = $prm['M_CompanyTypeGroupID'];
$userid = $this->sys_user["M_UserID"];
if($prm['xid'] == 0){
$query = "SELECT COUNT(*) as exist FROM m_companytype WHERE M_CompanyTypeIsActive = 'Y' AND M_CompanyTypeName = '{$name}'";
$exist_code = $this->db_regional->query($query)->row()->exist;
if($exist_code == 0){
$sql = "insert into m_companytype(
M_CompanyTypeName,
M_CompanyTypeM_CompanyTypeGroupID,
M_CompanyTypeUserID,
M_CompanyTypeLastUpdated,
M_CompanyTypeCreated)
values(?,?,?,now(),now())";
$query = $this->db_regional->query($sql,
array(
$name,
$group_id,
$userid)
);
if (!$query) {
$this->sys_error_db("m_companytype insert",$this->db_regional);
exit;
}
$result = array ("total" => 1, "records" => array("xid" => 0));
$this->sys_ok($result);
}else{
$errors = array();
if($exist_code != 0){
array_push($errors,array('field'=>'Nat_UnitName','msg'=>'Nama sudah ada yang pakai dong'));
}
$result = array ("total" => -1,"errors" => $errors, "records" => 0);
$this->sys_ok($result);
}
}else{
$query = "SELECT COUNT(*) as exist FROM m_companytype WHERE M_CompanyTypeIsActive = 'Y' AND M_CompanyTypeName = '{$name}' AND M_CompanyTypeID <> {$prm['xid']}";
//echo $query;
$exist_code = $this->db_regional->query($query)->row()->exist;
// echo $exist_code;
if($exist_code == 0){
$sql = "UPDATE m_companytype SET
M_CompanyTypeName = '{$name}',
M_CompanyTypeM_CompanyTypeGroupID = '{$group_id}',
M_CompanyTypeUserID = '{$userid}',
M_CompanyTypeLastUpdated = now()
WHERE M_CompanyTypeID = '{$prm['xid']}'";
//echo $sql;
$query = $this->db_regional->query($sql);
$result = array ("total" => 1, "records" => array("xid" => 0));
$this->sys_ok($result);
}else{
$errors = array();
if($exist_code != 0){
array_push($errors,array('field'=>'Nat_UnitName','msg'=>'Nama sudah ada yang pakai dong'));
}
$result = array ("total" => -1,"errors" => $errors, "records" => 0);
$this->sys_ok($result);
}
}
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
public function getallgroups()
{
try {
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$sql = "SELECT M_CompanyTypeGroupID, M_CompanyTypeGroupName FROM m_companytypegroup WHERE M_CompanyTypeGroupIsActive = 'Y' ORDER BY M_CompanyTypeGroupName ASC";
$query = $this->db_regional->query($sql);
$rows = $query->result_array();
$this->sys_ok(array("records" => $rows));
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
public function deleteunit()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
//# ambil parameter input
$prm = $this->sys_input;
$userid = $this->sys_user["M_UserID"];
$sql = "update m_companytype SET
M_CompanyTypeIsActive = 'N',
M_CompanyTypeLastUpdated = now()
WHERE
M_CompanyTypeID = ?
";
$query = $this->db_regional->query($sql,
array(
$prm['id']
)
);
// echo $query;
if (!$query) {
$this->sys_error_db("m_companytype delete");
exit;
}
$result = array ("total" => 1, "records" => array("xid" => 0));
$this->sys_ok($result);
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
}