Files
BE_IBL/application/controllers/masternar/Narspecimen.php
2026-04-15 15:23:57 +07:00

357 lines
9.1 KiB
PHP

<?php
class Narspecimen
extends MY_Controller
{
var $db_onedev;
public function index()
{
echo "Samplingverify API";
}
public function __construct()
{
parent::__construct();
$this->db_onedev = $this->load->database("onedev", true);
$this->load->helper(array('form', 'url'));
}
public function search()
{
$prm = $this->sys_input;
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$sql = "SELECT Nar_SpecimenID as id,
Nar_SpecimenID,
Nar_SpecimenCode,
Nar_SpecimenDescription
FROM nar_specimen
WHERE
Nar_SpecimenIsActive = 'Y'
GROUP BY Nar_SpecimenID
";
//echo $sql;
$query = $this->db_onedev->query($sql);
$rows = $query->result_array();
$result = array("total" => count($rows), "records" => $rows, "sql"=> $this->db_onedev->last_query());
$this->sys_ok($result);
exit;
}
public function search_listing()
{
$prm = $this->sys_input;
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$sql = "SELECT IFNULL(Nar_SpecimenID,0) as id,
Nar_SpecimenID,
Nar_SpecimenCode,
Nar_SpecimenDescription
FROM nar_specimen
WHERE
Nar_SpecimenIsActive = 'Y'
";
//echo $sql;
$query = $this->db_onedev->query($sql);
$rows = $query->result_array();
$result = array("total" => count($rows), "records" => $rows, "sql"=> $this->db_onedev->last_query());
$this->sys_ok($result);
exit;
}
function selectcategory(){
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$rows = [];
$query ="SELECT *
FROM t_category
WHERE
T_CategoryIsActive = 'Y'
ORDER BY T_CategoryName asc";
//echo $query;
$rows['categorys'] = $this->db_onedev->query($query)->result_array();
$result = array(
"total" => count($rows) ,
"records" => $rows,
);
$this->sys_ok($result);
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function save(){
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
//# ambil parameter input
$prm = $this->sys_input;
$Nar_SpecimenName = $prm['name'];
$Nar_SpecimenIsPCR = $prm['ispcr'];
$Nar_SpecimenIsSchedule = $prm['isschedule'];
$Nar_SpecimenIsKuota = $prm['iskuota'];
$userid = $this->sys_user["M_UserID"];
if($prm['xid'] == 0){
$query = "SELECT COUNT(*) as exist FROM nar_specimen WHERE Nar_SpecimenIsActive = 'Y' AND Nar_SpecimenName = '{$Nar_SpecimenName}' AND Nar_SpecimenT_CategoryID = '{$Nar_SpecimenT_CategoryID}'";
$exist_code = $this->db_onedev->query($query)->row()->exist;
if($exist_code == 0){
$sql = "insert into nar_specimen(
Nar_SpecimenName,
Nar_SpecimenIsPCR,
Nar_SpecimenIsSchedule,
Nar_SpecimenIsKuota,
Nar_SpecimenUserID,
Nar_SpecimenLastUpdate,
Nar_SpecimenCreated)
values(?,?,?,?,?,now(),now())";
$query = $this->db_onedev->query($sql,
array(
$Nar_SpecimenName,
$Nar_SpecimenIsPCR,
$Nar_SpecimenIsSchedule,
$Nar_SpecimenIsKuota,
$userid)
);
if (!$query) {
$this->sys_error_db("nar_specimen 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{
$errors = array();
if($exist_code != 0){
array_push($errors,array('field'=>'code','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 nar_specimen WHERE Nar_SpecimenIsActive = 'Y' AND Nar_SpecimenName = '{$Nar_SpecimenName}' AND Nar_SpecimenT_CategoryID = '{$Nar_SpecimenT_CategoryID}' AND Nar_SpecimenID <> {$prm['xid']}";
//echo $query;
$exist_code = $this->db_onedev->query($query)->row()->exist;
// echo $exist_code;
if($exist_code == 0){
$sql = "UPDATE nar_specimen SET
Nar_SpecimenName = '{$Nar_SpecimenName}',
Nar_SpecimenIsPCR = '{$Nar_SpecimenIsPCR}',
Nar_SpecimenIsSchedule = '{$Nar_SpecimenIsSchedule}',
Nar_SpecimenIsKuota = '{$Nar_SpecimenIsKuota}',
Nar_SpecimenUserID = '{$userid}'
WHERE Nar_SpecimenID = '{$prm['xid']}'";
//echo $sql;
$query = $this->db_onedev->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'=>'code','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 delete()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
//# ambil parameter input
$prm = $this->sys_input;
$id = $prm['xid'];
$userid = $this->sys_user["M_UserID"];
$sql = "update nar_specimen SET
Nar_SpecimenIsActive = 'N',
Nar_SpecimenUserID = $userid,
Nar_SpecimenLastUpdate = now()
WHERE
Nar_SpecimenID = $id";
$query = $this->db_onedev->query($sql);
if (!$query) {
$this->sys_error_db("nar_specimen delete");
exit;
}
$sqltest = "update nar_specimen_test SET
T_SubcategoryTestIsActive = 'N',
T_SubcategoryTestUserID = $userid,
T_SubcategoryTestLastUpdated = now()
WHERE
T_SubcategoryTestT_SubcategoryID = $id";
$querytest = $this->db_onedev->query($sqltest);
// echo $query;
if (!$querytest) {
$this->sys_error_db("nar_specimen_test 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 savejpagroup(){
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$userid = $this->sys_user["M_UserID"];
$query = "insert into nar_specimen(
Nar_SpecimenName,
Nar_SpecimenUserID,
Nar_SpecimenCreated
)
VALUES(
'{$prm['value']}',
{$userid},
NOW()
)";
//echo $query;
$action = $this->db_onedev->query($query);
if($action){
$result = array(
"total" => 1 ,
"records" => array(),
);
$this->sys_ok($result);
exit;
}
else{
$this->sys_error_db($this->db_onedev->last_query(), $this->db_onedev);
exit;
}
}
function savelistingjpagroup(){
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$userid = $this->sys_user["M_UserID"];
$query = " UPDATE nar_specimen SET
Nar_SpecimenName = '{$prm['name']}',
Nar_SpecimenIsActive = '{$prm['status']}',
Nar_SpecimenUserID = {$userid}
WHERE
Nar_SpecimenID = {$prm['id']}
";
//echo $query;
$action = $this->db_onedev->query($query);
if($action){
$result = array(
"total" => 1 ,
"records" => array(),
);
$this->sys_ok($result);
exit;
}
else{
$this->sys_error_db($this->db_onedev->last_query(), $this->db_onedev);
exit;
}
}
function savealljpadetail(){
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$details = $prm['details'];
$userid = $this->sys_user["M_UserID"];
foreach($details as $k => $v){
$query = "insert into nar_specimen(
Nat_JPADetailNat_JPAID,
Nat_JPADetailNar_SpecimenID,
Nat_JPADetailDiscount,
Nat_JPADetailUserID,
Nat_JPADetailCreated
)
VALUES(
'{$v['Nat_JpaID']}',
'{$v['Nar_SpecimenID']}',
'{$v['Nat_JPADetailDiscount']}',
{$userid},
NOW()
) ON DUPLICATE KEY UPDATE
Nat_JPADetailDiscount = {$v['Nat_JPADetailDiscount']},
Nat_JPADetailUserID = {$userid}";
//echo $query;
$action = $this->db_onedev->query($query);
}
if($action){
$result = array(
"total" => 1 ,
"records" => array(),
);
$this->sys_ok($result);
exit;
}
else{
$this->sys_error_db($this->db_onedev->last_query(), $this->db_onedev);
exit;
}
}
}