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

430 lines
12 KiB
PHP

<?php
class Favorite extends MY_Controller
{
var $db_onedev;
public function index()
{
echo "ITEM API";
}
public function __construct()
{
parent::__construct();
$this->db_onedev = $this->load->database("onedev", true);
}
public function lookupitem()
{
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 m_itemadditional
where
M_ItemAdditionalIsActive = 'Y'";
$total = $this->db_onedev->query($sql)->row()->total;
$sql = "select M_ItemAdditionalID as id, M_ItemAdditionalCode as code, M_ItemAdditionalName as name,M_ItemAdditionalMandatory as flagmandatory, IF(M_ItemAdditionalMandatory = 'N',CONCAT('[ ',M_ItemAdditionalCode,' ] ',M_ItemAdditionalName),CONCAT('[ ',M_ItemAdditionalCode,' ] ',M_ItemAdditionalName,'*')) as namex,M_ItemAdditionalPrice as price,'xxx' as itemtests
from m_itemadditional
where
( M_ItemAdditionalCode LIKE CONCAT('%',?,'%') OR M_ItemAdditionalName LIKE CONCAT('%',?,'%')) AND
M_ItemAdditionalIsActive = 'Y' $limit";
$sql_param = array($search,$search);
$query = $this->db_onedev->query($sql,$sql_param);
//echo $this->db_onedev->last_query();
if ($query) {
$rows = $query->result_array();
} else {
$this->sys_error_db("m_itemadditional 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 save()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$userid = $this->sys_user["M_UserID"];
//# ambil parameter input
$prm = $this->sys_input;
$code = $prm['code'];
$name = $prm['name'];
$price = $prm['price'];
$flagmandatory = $prm['flagmandatory'];
if($prm['act'] == 'new'){
$query = "SELECT COUNT(*) as exist FROM m_itemadditional WHERE M_ItemAdditionalIsActive = 'Y' AND M_ItemAdditionalCode = '{$code}'";
$exist_code = $this->db_onedev->query($query)->row()->exist;
$query = "SELECT COUNT(*) as exist FROM m_itemadditional WHERE M_ItemAdditionalIsActive = 'Y' AND M_ItemAdditionalName = '{$name}'";
$exist_name = $this->db_onedev->query($query)->row()->exist;
if($exist_code == 0 && $exist_name == 0){
$sql = "insert into m_itemadditional(
M_ItemAdditionalCode,
M_ItemAdditionalName,
M_ItemAdditionalMandatory,
M_ItemAdditionalPrice,
M_ItemAdditionalUserID,
M_ItemAdditionalCreatedDate,
M_ItemAdditionalLastUpdated
)
values( ?,?,?,?,?,now(),now())";
$query = $this->db_onedev->query($sql,
array(
$code,
$name,
$flagmandatory,
$price,
$userid
)
);
//echo $query;
if (!$query) {
$this->sys_error_db("m_itemadditional insert");
exit;
}
$result = array ("total" => 1, "records" => array("xid" => 0));
$this->sys_ok($result);
}else{
$errors = array();
if($exist_code > 0){
array_push($errors,"errorcode");
}
if($exist_name > 0){
array_push($errors,"errorname");
}
$result = array ("total" => -1,"errors" => $errors, "records" => 0);
$this->sys_ok($result);
}
}else{
$query = "SELECT COUNT(*) as exist FROM m_itemadditional WHERE M_ItemAdditionalIsActive = 'Y' AND M_ItemAdditionalCode = '{$code}' AND M_ItemAdditionalID <> {$prm['id']}";
$exist_code = $this->db_onedev->query($query)->row()->exist;
$query = "SELECT COUNT(*) as exist FROM m_itemadditional WHERE M_ItemAdditionalIsActive = 'Y' AND M_ItemAdditionalName = '{$name}' AND M_ItemAdditionalID <> {$prm['id']}";
$exist_name = $this->db_onedev->query($query)->row()->exist;
if($exist_code == 0 && $exist_name == 0){
$sql = "UPDATE m_itemadditional SET
M_ItemAdditionalCode = ?,
M_ItemAdditionalName = ?,
M_ItemAdditionalMandatory = ?,
M_ItemAdditionalPrice = ?,
M_ItemAdditionalLastUpdated = now()
WHERE
M_ItemAdditionalID = ?";
$query = $this->db_onedev->query($sql,
array(
$code,
$name,
$flagmandatory,
$price,
$prm['id']
)
);
//echo $query;
if (!$query) {
//echo $this->db_onedev->last_query();
$this->sys_error_db("m_itemadditional update");
exit;
}
$result = array ("total" => 1, "records" => array("xid" => $prm['id']));
$this->sys_ok($result);
}else{
$errors = array();
if($exist_code > 0){
array_push($errors,"errorcode");
}
if($exist_name > 0){
array_push($errors,"errorname");
}
$result = array ("total" => -1,"errors" => $errors, "records" => 0);
$this->sys_ok($result);
}
}
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
public function deleteitem()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$userid = $this->sys_user["M_UserID"];
//# ambil parameter input
$prm = $this->sys_input;
$sql = "update m_itemadditional SET
M_ItemAdditionalIsActive = 'N',
M_ItemAdditionalUserID = ?,
M_ItemAdditionalLastUpdated = now()
WHERE
M_ItemAdditionalID = ?
";
$query = $this->db_onedev->query($sql,
array(
$userid,
$prm['id']
)
);
// echo $query;
if (!$query) {
//echo $this->db_onedev->last_query();
$this->sys_error_db("m_item delete");
exit;
}
$sql = "update m_itemadditionaltest SET
M_ItemAdditionalTestIsActive = 'N',
M_ItemAdditionalTestLastUpdated = now()
WHERE
M_ItemAdditionalTestM_ItemAdditionalID = ?
";
$query = $this->db_onedev->query($sql,
array(
$prm['id']
)
);
// echo $query;
if (!$query) {
//echo $this->db_onedev->last_query();
$this->sys_error_db("m_itemadditionaltest delete");
exit;
}
$result = array ("total" => 1, "records" => array("xid" => 0));
$this->sys_ok($result);
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function lookupitemtests(){
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$sql = "
select
T_FavoriteID as id,
T_TestName as testname,
T_FavoriteT_TestID as testid,
T_FavoriteIsPacket as ispaket,
T_FavoriteT_PacketID as paketid,
T_FavoriteType as testtype
from t_favorite
JOIN t_test ON T_FavoriteT_TestID = T_TestID AND T_TestIsActive = 'Y'
where
T_FavoriteIsActive= 'Y' AND T_FavoriteIsPacket = 'N'
UNION
select
T_FavoriteID as id,
T_PacketName as testname,
T_FavoriteT_TestID as testid,
T_FavoriteIsPacket as ispaket,
T_FavoriteT_PacketID as paketid,
T_FavoriteType as testtype
from t_favorite
JOIN t_packet ON T_FavoriteT_PacketID = T_PacketID
where
T_FavoriteIsActive= 'Y' AND T_FavoriteIsPacket = 'Y'
";
$query = $this->db_onedev->query($sql);
if ($query) {
$rows = $query->result_array();
} else {
$this->sys_error_db("t_favorite select");
exit;
}
$result = array ("total" => count($rows), "records" => $rows);
$this->sys_ok($result);
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
public function lookuptests()
{
$prm = $this->sys_input;
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$search = $prm["search"];
$sql_param = array($search,$search);
$sql = " SELECT count(*) as total
FROM t_test
WHERE
(T_TestCode like CONCAT('%','{$search}','%') OR T_TestName like CONCAT('%','{$search}','%')) AND
T_TestIsActive = 'Y'
";
$query = $this->db_onedev->query($sql, $sql_param);
$tot_count = 0;
if ($query) {
$tot_count = $query->result_array()[0]["total"];
} else {
echo $this->db_onedev->last_query();
$this->sys_error_db("t_test count", $this->db_onedev);
exit;
}
$sql = "SELECT
T_TestID as testid,
T_TestSasCode as testcode,
T_TestName as testname,
'PX' as testtype,
'N' as ispaket
FROM t_test
WHERE
(T_TestCode like CONCAT('%','{$search}','%') OR T_TestName like CONCAT('%','{$search}','%')) AND
T_TestIsActive = 'Y' and T_TestIsPrice = 'Y'
union
select
T_PacketID as testid,
T_PacketSasCode as testcode,
T_PacketName as testname,
T_PacketType as testtype,
'Y' as ispaket
FROM t_packet
WHERE
(T_PacketSasCode like CONCAT('%','{$search}','%') OR T_PacketName like CONCAT('%','{$search}','%')) AND
T_PacketIsActive = 'Y'
ORDER BY testcode ASC";
$query = $this->db_onedev->query($sql, $sql_param);
//echo $this->db_onedev->last_query();
$rows = $query->result_array();
//$this->_add_address($rows);
$result = array("total" => $tot_count, "records" => $rows, "sql"=> $this->db_onedev->last_query());
$this->sys_ok($result);
exit;
}
function saveitemtest(){
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$userid = $this->sys_user["M_UserID"];
$prm = $this->sys_input;
$id = $prm['itemid'];
$itemtests = $prm['favorites'];
$deleteditemtests = $prm['deletedfavorite'];
foreach($itemtests as $k=>$v){
if($v['id'] == 0){
$testid = $v['testid'];
$packetid = 0;
if($v['testtype'] != 'PX'){
$packetid = $v['testid'];
$testid = 0;
}
$sql = "INSERT INTO t_favorite (
T_FavoriteType,
T_FavoriteIsPacket,
T_FavoriteT_TestID,
T_FavoriteT_PacketID,
T_FavoriteUserID
)
VALUES('{$v['testtype']}','{$v['ispaket']}','{$testid}','{$packetid}',{$userid})";
$query = $this->db_onedev->query($sql);
//echo $this->db_onedev->last_query();
}
}
if($deleteditemtests){
foreach($deleteditemtests as $k=>$v){
$sql = "UPDATE t_favorite SET
T_FavoriteIsActive = 'N',
T_FavoriteUserID = ?
WHERE
T_FavoriteID = ?
";
$sql_param = array($userid,$v['id']);
$query = $this->db_onedev->query($sql,$sql_param);
}
}
$result = array ("total" => 1, "records" => array());
$this->sys_ok($result);
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
}