Files
2026-04-27 10:31:17 +07:00

282 lines
7.9 KiB
PHP

<?php
class Tbahan extends MY_Controller
{
var $db_onedev;
public function index()
{
echo "SUB GROUP API";
}
public function __construct()
{
parent::__construct();
$this->db_onedev = $this->load->database("onedev", true);
}
public function lookup()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$search = $prm['search'];
$all = $prm['all'];
$limit = '';
if($all == 'N'){
$limit = ' LIMIT 10';
}
$sql = "select COUNT(*) as total
from t_bahan
where
T_BahanIsActive = 'Y'";
$sql_param = array($search);
$total = $this->db_onedev->query($sql,$sql_param)->row()->total;
$sql = "select T_BahanID as id,
T_BahanCode as code,
T_BahanName as name,
T_BahanLangName as namelang,
t_bahan.*
from t_bahan
where
( T_BahanName LIKE CONCAT('%','{$search}','%') OR
T_BahanCode LIKE CONCAT('%','{$search}','%')
)AND
T_BahanIsActive = 'Y'
GROUP BY T_BahanID
ORDER BY T_BahanID ASC $limit";
$sql_param = array($search);
$query = $this->db_onedev->query($sql);
//echo $this->db_onedev->last_query();
if ($query) {
$rows = $query->result_array();
} else {
$this->sys_error_db("t_bahan select");
exit;
}
$result = array ("total" => $total, "total_filter"=>count($rows),"records" => $rows);
$this->sys_ok($result);
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
public function addnewbahan()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
//# ambil parameter input
$prm = $this->sys_input;
$name = $prm['name'];
$namelang = $prm['namelang'];
$code = $prm['code'];
$query = "SELECT COUNT(*) as exist FROM t_bahan WHERE T_BahanIsActive = 'Y' AND T_BahanCode = '{$code}'";
$exist_code = $this->db_onedev->query($query)->row()->exist;
if($exist_code == 0){
$sql = "insert into t_bahan (
T_BahanCode,
T_BahanName,
T_BahanLangName,
T_BahanCreated,
T_BahanLastUpdated
)
values( ?, ?, ?, now(), now())";
$query = $this->db_onedev->query($sql,
array(
$code,
$name,
$namelang
)
);
//echo $query;
if (!$query) {
$this->sys_error_db("t_bahan insert");
exit;
}
$result = array ("total" => 1, "records" => array("xid" => 0));
$this->sys_ok($result);
$last_id = $this->db_onedev->insert_id();
}else{
$errors = array();
if($exist_code != 0){
// array_push($errors,array('field'=>'code','msg'=>'Kode sudah ada yang pakai dong'));
$sql = "insert into t_bahan(
T_BahanCode,
T_BahanName,
T_BahanLangName,
T_BahanCreated,
T_BahanLastUpdated
)
values( ?, ?, ?, now(), now())";
$query = $this->db_onedev->query($sql,
array(
$code,
$name,
$namelang
)
);
//echo $query;
if (!$query) {
$this->sys_error_db("t_bahan insert");
exit;
}
$result = array ("total" => 1, "records" => array("xid" => 0));
$this->sys_ok($result);
$last_id = $this->db_onedev->insert_id();
}
//$result = array ("total" => -1,"errors" => $errors, "records" => 0);
// $this->sys_ok($result);
}
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
public function editbahan()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
//# ambil parameter input
$prm = $this->sys_input;
$id = $prm['id'];
$name = $prm['name'];
$namelang = $prm['namelang'];
$code = $prm['code'];
$userid = $this->sys_user["M_UserID"];
$query = "SELECT COUNT(*) as exist FROM t_bahan WHERE T_BahanIsActive = 'Y' AND T_BahanCode = '{$code}' AND T_BahanID <> {$prm['id']}";
$exist_code = $this->db_onedev->query($query)->row()->exist;
if($exist_code == 0){
$sqlcompany = "update t_bahan SET
T_BahanCode = ?,
T_BahanName = ?,
T_BahanLangName = ?,
T_BahanLastUpdated = now()
where
T_BahanID = ?
";
$querycompany = $this->db_onedev->query($sqlcompany,
array(
$code,
$name,
$namelang,
$id
)
);
// echo $query;
if (!$querycompany) {
$this->sys_error_db("t_bahan update");
exit;
}
$result = array ("total" => 1, "records" => array("xid" => $id));
$this->sys_ok($result);
}else{
$errors = array();
if($exist_code != 0){
$sqlcompany = "update t_bahan SET
T_BahanCode = ?,
T_BahanName = ?,
T_BahanLangName = ?,
T_BahanLastUpdated = now()
where
T_BahanID = ?
";
$querycompany = $this->db_onedev->query($sqlcompany,
array(
$code,
$name,
$namelang,
$id
)
);
// echo $query;
if (!$querycompany) {
$this->sys_error_db("t_bahan update");
exit;
}
$result = array ("total" => 1, "records" => array("xid" => $id));
$this->sys_ok($result);
}
// $result = array ("total" => -1,"errors" => $errors, "records" => 0);
// $this->sys_ok($result);
}
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
public function deletebahan()
{
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 t_bahan SET
T_BahanIsActive = 'N',
T_BahanLastUpdated = now()
WHERE
T_BahanID = ?
";
$query = $this->db_onedev->query($sql,
array(
$prm['id']
)
);
// echo $query;
if (!$query) {
$this->sys_error_db("t_bahan delete");
exit;
}
$result = array ("total" => 1, "records" => array("xid" => 0));
$this->sys_ok($result);
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
}