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

316 lines
8.8 KiB
PHP

<?php
class Service extends MY_Controller
{
function __construct()
{
parent:: __construct();
}
function index()
{
$prm = $this->sys_input;
$sql_data = "SELECT DISTINCT
Q_ServiceID as id,
Q_ServiceCode as code,
Q_ServiceName as name,
Q_ServicePriority as priority
FROM q_service
WHERE Q_ServiceIsActive = 'Y'
ORDER BY Q_ServicePriority desc";
$qry = $this->db->query($sql_data);
if($qry) {
$rows = $qry->result_array();
}else{
$this->sys_error_db("service select error", $this->db);
exit;
}
$result = array(
"total" => count($rows),
"records" => $rows
);
$this->sys_ok($result);
exit;
}
function save()
{
try {
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$this->db->trans_begin();
$prm = $this->sys_input;
// print_r($prm);
$userid = $this->sys_user["M_UserID"];
// print_r($userid);
$code = "";
if (isset($prm['code'])) {
$code = trim($prm["code"]);
}
$name = "";
if (isset($prm['name'])) {
$name = trim($prm["name"]);
}
$priority = 0;
if (isset($prm['priority'])) {
if (is_numeric($prm["priority"])) {
$priority = trim($prm["priority"]);
}
}
$sql = "INSERT INTO q_service(
Q_ServiceCode,
Q_ServiceName,
Q_ServicePriority,
Q_ServiceUserID,
Q_ServiceCreated,
Q_ServiceLastUpdated)
VALUES (?, ?, ?, ?, NOW(), NOW())";
$qry = $this->db->query($sql, [
$code,
$name,
$priority,
$userid
]);
$last_qry = $this->db->last_query();
// echo $last_qry;
if(!$qry) {
$this->db->trans_rollback();
$error = array(
"message" => $this->db->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error, $this->db);
exit;
}
$this->db->trans_commit();
$result = array(
"affected_rows" => $this->db->affected_rows(),
"inserted_id" => $this->db->insert_id(),
);
$this->sys_ok($result);
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function search()
{
try {
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
// print_r($prm);
$search = "";
if(isset($prm["search"])) {
$search = trim($prm["search"]);
if ($search != ""){
$search = "%" . $prm["search"] . "%";
}else{
$search = "%";
}
}
$sortBy = $prm["sortBy"];
$sortStatus = $prm["sortStatus"];
if($sortBy){
$q_sort = "ORDER BY ".$sortBy." ".$sortStatus;
}
$number_offset = 0;
$number_limit = 10;
if($prm["current_page"] > 0) {
$number_offset = ($prm["current_page"] - 1) * $number_limit;
}
$sql_filter = "SELECT COUNT(DISTINCT
Q_ServiceID,
Q_ServiceCode,
Q_ServiceName,
Q_ServicePriority,
Q_ServiceIsFoOrder,
Q_ServiceIsActive) as total
FROM q_service
WHERE Q_ServiceName like ?
AND Q_ServiceIsActive = 'Y'";
$qry_filter = $this->db->query($sql_filter, [$search]);
// $last_qry = $this->db->last_query();
// print_r($last_qry);
$tot_count = 0;
$tot_page = 0;
if($qry_filter) {
$tot_count = $qry_filter->result_array()[0]["total"];
$tot_page = ceil($tot_count/$number_limit);
}else{
$this->sys_error_db("q_service select count", $this->db);
exit;
}
$sql_data = "SELECT DISTINCT
Q_ServiceID,
Q_ServiceCode,
Q_ServiceName,
Q_ServicePriority
FROM q_service
WHERE Q_ServiceName like ?
AND Q_ServiceIsActive = 'Y' $q_sort
limit ? offset ?";
$qry_data = $this->db->query($sql_data, [$search, $number_limit, $number_offset]);
if($qry_data) {
$rows = $qry_data->result_array();
}else{
$this->sys_error_db("q_service select", $this->db);
exit;
}
// $last_qry = $this->db->last_query();
// print_r($last_qry);
$result = array(
"total" => $tot_page,
"total_filter" => $tot_count,
"records" => $rows
);
$this->sys_ok($result);
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function edit()
{
try {
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$this->db->trans_begin();
$prm = $this->sys_input;
$userid = $this->sys_user['M_UserID'];
$code = "";
if (isset($prm['code'])) {
$code = trim($prm["code"]);
}
$name = "";
if (isset($prm['name'])) {
$name = trim($prm["name"]);
}
$priority = 0;
if (isset($prm['priority'])) {
if (is_numeric($prm["priority"])) {
$priority = trim($prm["priority"]);
}
}
$id = "";
if (isset($prm['id'])) {
if (is_numeric($prm["id"])) {
$id = trim($prm["id"]);
}
}
$sql = "UPDATE q_service
SET Q_ServiceCode = ?,
Q_ServiceName = ?,
Q_ServicePriority = ?,
Q_ServiceUserID = ?,
Q_ServiceLastUpdated = NOW()
WHERE Q_ServiceID = ?";
$qry = $this->db->query($sql, [$code, $name, $priority, $userid, $id]);
$last_qry = $this->db->last_query();
if(!$qry) {
$this->db->trans_rollback();
$error = array(
"message" => $this->db->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error, $this->db);
exit;
}
$this->db->trans_commit();
$result = array(
"total" => 1,
"affected_rows" => $this->db->affected_rows()
);
$this->sys_ok($result);
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function delete()
{
try {
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$this->db->trans_begin();
$prm = $this->sys_input;
$id = "";
if (isset($prm['id'])) {
if (is_numeric($prm["id"])) {
$id = trim($prm["id"]);
}
}
$sql = "UPDATE q_service
SET Q_ServiceIsActive = 'N',
Q_ServiceLastUpdated = NOW()
WHERE Q_ServiceID = ?";
$qry = $this->db->query($sql, [
$id
]);
$last_qry = $this->db->last_query();
if(!$qry){
$this->db->trans_rollback();
$error = array(
"message" => $this->db->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error, $this->db);
exit;
}
// print_r($last_qry);
$this->db->trans_commit();
$result = array(
"affected_rows" => $this->db->affected_rows()
);
$this->sys_ok($result);
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
}
?>