252 lines
8.5 KiB
PHP
252 lines
8.5 KiB
PHP
<?php
|
|
|
|
class Instrumentused extends MY_Controller
|
|
{
|
|
var $db_onedev;
|
|
public function index()
|
|
{
|
|
echo "INSTRUMENT USED API";
|
|
}
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->db_onedev = $this->load->database("onedev", true);
|
|
}
|
|
|
|
function lookupinstrumentbyname(){
|
|
try {
|
|
//# cek token valid
|
|
if (! $this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
|
|
$prm = $this->sys_input;
|
|
$instrument = $prm['instrument'];
|
|
$all = $prm['all'];
|
|
$limit = '';
|
|
if($all == 'N'){
|
|
$limit = ' LIMIT 10';
|
|
}
|
|
$number_limit = 10;
|
|
$number_offset = ($prm['current_page'] - 1) * $number_limit ;
|
|
$sql = "select COUNT(*) as total
|
|
FROM(SELECT *
|
|
from t_instrument_used
|
|
JOIN nat_instrument ON T_InstrumentUsedNat_InstrumentID = Nat_InstrumentID AND Nat_InstrumentIsActive = 'Y'
|
|
WHERE
|
|
(Nat_InstrumentName LIKE CONCAT('%','{$instrument}','%') OR
|
|
Nat_InstrumentCode LIKE CONCAT('%','{$instrument}','%')) AND
|
|
T_InstrumentUsedIsActive = 'Y' GROUP BY T_InstrumentUsedID) a";
|
|
// $total = $this->db_onedev->query($sql,$sql_param)->row()->total;
|
|
$query = $this->db_onedev->query($sql);
|
|
//echo $this->db_onedev->last_query();
|
|
$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("t_instrument_used count", $this->db_onedev);
|
|
exit;
|
|
}
|
|
|
|
$sql = "select T_InstrumentUsedID as id,
|
|
Nat_InstrumentName as name,
|
|
Nat_InstrumentCode as code,
|
|
Nat_InstrumentName,
|
|
Nat_InstrumentCode,
|
|
t_instrument_used.*
|
|
from t_instrument_used
|
|
JOIN nat_instrument ON T_InstrumentUsedNat_InstrumentID = Nat_InstrumentID AND Nat_InstrumentIsActive = 'Y'
|
|
WHERE
|
|
(Nat_InstrumentName LIKE CONCAT('%','{$instrument}','%') OR
|
|
Nat_InstrumentCode LIKE CONCAT('%','{$instrument}','%')) AND
|
|
T_InstrumentUsedIsActive = 'Y'
|
|
GROUP BY T_InstrumentUsedID
|
|
ORDER BY Nat_InstrumentCode ASC, Nat_InstrumentName ASC
|
|
limit $number_limit offset $number_offset";
|
|
$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_instrument_used 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 addnewinstrument()
|
|
{
|
|
try {
|
|
//# cek token valid
|
|
if (! $this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
|
|
//# ambil parameter input
|
|
$prm = $this->sys_input;
|
|
$instrumentid = $prm['instrumentid'];
|
|
$userid = $this->sys_user["M_UserID"];
|
|
|
|
if($instrumentid == 0 ){
|
|
$errors = array();
|
|
if($instrumentid == 0){
|
|
array_push($errors,array('field'=>'instrument','msg'=>'Instrument dipilih dulu dong'));
|
|
}
|
|
$result = array ("total" => -1,"errors" => $errors, "records" => 0);
|
|
$this->sys_ok($result);
|
|
}else{
|
|
if($prm['xid'] == 0){
|
|
$sql = "insert into t_instrument_used(
|
|
T_InstrumentUsedNat_InstrumentID,
|
|
T_InstrumentUsedUserID,
|
|
T_InstrumentUsedLastUpdated,
|
|
T_InstrumentUsedCreated)
|
|
values(?,?,now(),now())";
|
|
$query = $this->db_onedev->query($sql,
|
|
array(
|
|
$instrumentid,
|
|
$userid
|
|
)
|
|
);
|
|
if (!$query) {
|
|
$this->sys_error_db("t_instrument_used insert",$this->db_onedev);
|
|
exit;
|
|
}
|
|
$last_id = $this->db_onedev->insert_id();
|
|
$result = array ("total" => 1, "records" => array("xid" => 0));
|
|
$this->sys_ok($result);
|
|
}else{$sql = "UPDATE t_instrument_used SET T_InstrumentUsedNat_InstrumentID = '{$instrumentid}',
|
|
T_InstrumentUsedUserID = '{$userid}',
|
|
T_InstrumentUsedLastUpdated = now()
|
|
WHERE T_InstrumentUsedID = '{$prm['xid']}'";
|
|
//echo $sql;
|
|
$query = $this->db_onedev->query($sql);
|
|
$result = array ("total" => 1, "records" => array("xid" => 0));
|
|
$this->sys_ok($result);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
} catch(Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
|
|
public function deleteinstrument()
|
|
{
|
|
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_instrument_used SET
|
|
T_InstrumentUsedIsActive = 'N',
|
|
T_InstrumentUsedUserID = '{$userid}',
|
|
T_InstrumentUsedLastUpdated = now()
|
|
WHERE
|
|
T_InstrumentUsedID = ?
|
|
|
|
";
|
|
|
|
$query = $this->db_onedev->query($sql,
|
|
array(
|
|
$prm['id']
|
|
)
|
|
);
|
|
// echo $query;
|
|
if (!$query) {
|
|
$this->sys_error_db("t_instrument_used 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 searchinstrument(){
|
|
if (! $this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
$prm = $this->sys_input;
|
|
|
|
$max_rst = 12;
|
|
$tot_count =0;
|
|
|
|
$q = [
|
|
'search' => '%'
|
|
];
|
|
|
|
if ($prm['tes'] != '')
|
|
{
|
|
$q['search'] = "%{$prm['tes']}%";
|
|
}
|
|
|
|
// QUERY TOTAL
|
|
$sql = "SELECT count(*) as total
|
|
FROM nat_instrument
|
|
WHERE
|
|
Nat_InstrumentName like ?
|
|
AND Nat_InstrumentIsActive = 'Y'";
|
|
$query = $this->db_onedev->query($sql,$q['search']);
|
|
//echo $query;
|
|
if ($query) {
|
|
$tot_count = $query->result_array()[0]["total"];
|
|
}
|
|
else {
|
|
$this->sys_error_db("nat_instrument count",$this->db_onedev);
|
|
exit;
|
|
}
|
|
|
|
$sql = "SELECT * FROM
|
|
(SELECT Nat_InstrumentID, Nat_InstrumentName,T_InstrumentUsedID
|
|
FROM nat_instrument
|
|
LEFT JOIN t_instrument_used ON T_InstrumentUsedNat_InstrumentID = Nat_InstrumentID AND T_InstrumentUsedIsActive = 'Y'
|
|
WHERE
|
|
Nat_InstrumentName like ?
|
|
AND Nat_InstrumentIsActive = 'Y'
|
|
ORDER BY Nat_InstrumentName ASC) a
|
|
WHERE T_InstrumentUsedID IS NULL
|
|
";
|
|
$query = $this->db_onedev->query($sql, array($q['search']));
|
|
|
|
if ($query) {
|
|
$rows = $query->result_array();
|
|
//echo $this->db_onedev->last_query();
|
|
$result = array("total" => $tot_count, "records" => $rows, "total_display" => sizeof($rows));
|
|
$this->sys_ok($result);
|
|
}
|
|
else {
|
|
$this->sys_error_db("nat_instrument rows",$this->db_onedev);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
} |