Files
2026-04-15 15:23:57 +07:00

514 lines
20 KiB
PHP

<?php
class Counter extends MY_Controller
{
var $db_antrione;
function __construct()
{
parent::__construct();
$this->db_antrione = $this->load->database("antrione", true);
}
function index()
{
echo "Api: Counter";
}
function search()
{
try {
if (!$this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$page = $prm["page"];
$ROW_PER_PAGE = 10;
$start_offset = 0;
$name = "%%";
if (isset($prm['name'])) {
$name = trim($prm["name"]);
$name = '%' . $prm['name'] . '%';
}
$order_by = "counterCode";
if (isset($prm['order_by'])) {
$order_by = trim($prm["order_by"]);
}
$order = "asc";
if (isset($prm['order'])) {
$order = trim($prm["order"]);
}
$sort = "order by " . $order_by . " " . $order;
if (isset($prm["page"])) {
if (
is_numeric($prm["page"]) && $prm["page"] > 0
) {
$start_offset = ($page - 1) * $ROW_PER_PAGE;
}
}
$sql = "SELECT
counterID AS id,
counterCode AS code,
counterIP AS IP,
counterIsDedicated AS isDedicated,
counterLocationID AS locationId,
locationName,
counterMaxQueue AS maxQueue,
counterIsActive AS isActive
FROM counter
LEFT JOIN location ON counterLocationID = locationID
AND locationIsActive = 'Y'
WHERE counterIsActive = 'Y'
AND (counterCode LIKE ? OR counterIP LIKE ? OR locationName LIKE ?)
$sort LIMIT 10 OFFSET ?";
$qry = $this->db_antrione->query($sql, [$name, $name, $name, $start_offset]);
$last_qry = $this->db_antrione->last_query();
if (!$qry) {
$error = array(
"message" => $this->db_antrione->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
exit;
}
$rows = $qry->result_array();
foreach ($rows as $key => $value) {
if ($value['isDedicated'] == 'Y') {
$sqlDetail = "SELECT
counterServiceID,
serviceID,
counterServiceCounterID as counterID,
serviceCode,
serviceName,
serviceDoctorName
FROM counter_service
JOIN service ON counterServiceServiceID = serviceID
AND serviceIsActive = 'Y'
WHERE counterServiceIsActive = 'Y'
AND counterServiceCounterID = ?";
$qryDetail = $this->db_antrione->query($sqlDetail, [intval($value['id'])]);
$last_qry = $this->db_antrione->last_query();
if (!$qryDetail) {
$error = array(
"message" => $this->db_antrione->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
exit;
}
$detail = $qryDetail->result_array();
$rows[$key]['details'] = $detail;
} else {
$rows[$key]['details'] = array();
}
}
$sqlCount = "SELECT
COUNT(counterID) AS total
FROM counter
LEFT JOIN location ON counterLocationID = locationID
AND locationIsActive = 'Y'
WHERE counterIsActive = 'Y'
AND (counterCode LIKE ? OR counterIP LIKE ? OR locationName LIKE ?)";
$qry_total_filter = $this->db_antrione->query($sqlCount, [$name, $name, $name]);
$last_qry_total_filter = $this->db_antrione->last_query();
if (!$qry_total_filter) {
$error = array(
"message" => $this->db_antrione->error()["message"],
"sql" => $last_qry_total_filter
);
$this->sys_error_db($error);
exit;
}
$total_filter = (int)$qry_total_filter->result_array()[0]["total"];
$total = ceil($total_filter / $ROW_PER_PAGE);
$result = array(
"total" => $total,
"total_filter" => $total_filter,
"records" => $rows,
"sql" => $last_qry,
"count" => $last_qry_total_filter
);
$this->sys_ok($result);
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function add()
{
try {
if (!$this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$code = '';
if (isset($prm['code'])) {
$code = trim($prm["code"]);
}
$ip = '';
if (isset($prm['ip'])) {
$ip = trim($prm["ip"]);
}
$isDedicated = '';
if (isset($prm['isDedicated'])) {
$isDedicated = trim($prm["isDedicated"]);
}
$locationId = 0;
if (isset($prm['locationId'])) {
$locationId = trim($prm["locationId"]);
$locationId = intval($locationId);
}
$maxQueue = 10;
if (isset($prm['maxQueue'])) {
$maxQueue = trim($prm["maxQueue"]);
$maxQueue = intval($maxQueue);
}
$service = array();
if (isset($prm['service'])) {
$service = $prm["service"];
}
if ($isDedicated == 'N') {
if ($code == "" || $ip == "" || $locationId == 0) {
$msg = 'Code, ip, location is mandatory';
$this->sys_error($msg);
exit;
}
} else {
if ($code == "" || $ip == "" || $locationId == 0 || empty($service)) {
$msg = 'Code, ip, location, service is mandatory';
$this->sys_error($msg);
exit;
}
}
$this->db_antrione->trans_begin();
$sql = "INSERT INTO counter(counterCode,
counterIP,
counterIsDedicated,
counterLocationID,
counterMaxQueue)
VALUES (?, ?, ?, ?, ?)";
$qry = $this->db_antrione->query($sql, [$code, $ip, $isDedicated, $locationId, $maxQueue]);
$last_qry = $this->db_antrione->last_query();
if (!$qry) {
$error = array(
"message" => $this->db_antrione->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
$this->db_antrione->trans_rollback();
exit;
}
if ($isDedicated == 'Y') {
$insertedId = $this->db_antrione->insert_id();
foreach ($service as $key => $value) {
$sql = "INSERT INTO counter_service
( counterServiceServiceID,
counterServiceCounterID)
VALUES (?, ?)";
$qry = $this->db_antrione->query($sql, [intval($value), intval($insertedId)]);
$last_qry = $this->db_antrione->last_query();
if (!$qry) {
$error = array(
"message" => $this->db_antrione->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
$this->db_antrione->trans_rollback();
exit;
}
}
}
$result = array(
"affected_rows" => $this->db_antrione->affected_rows(),
"inserted_id" => $this->db_antrione->insert_id(),
"sql" => $last_qry,
);
$this->db_antrione->trans_commit();
$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;
}
$prm = $this->sys_input;
$id = '';
if (isset($prm['id'])) {
$id = intval($prm["id"]);
}
$code = '';
if (isset($prm['code'])) {
$code = trim($prm["code"]);
}
$ip = '';
if (isset($prm['ip'])) {
$ip = trim($prm["ip"]);
}
$isDedicated = '';
if (isset($prm['isDedicated'])) {
$isDedicated = trim($prm["isDedicated"]);
}
$isDedicatedBefore = '';
if (isset($prm['isDedicatedBefore'])) {
$isDedicatedBefore = trim($prm["isDedicatedBefore"]);
}
$locationId = 0;
if (isset($prm['locationId'])) {
$locationId = trim($prm["locationId"]);
$locationId = intval($locationId);
}
$maxQueue = 10;
if (isset($prm['maxQueue'])) {
$maxQueue = trim($prm["maxQueue"]);
$maxQueue = intval($maxQueue);
}
$service = array();
if (isset($prm['service'])) {
$service = $prm["service"];
}
if ($code == "" || $ip == "" || $locationId == 0 || $id == "") {
$msg = 'Code, ip, location, id is mandatory';
$this->sys_error($msg);
exit;
}
$bagian = '';
$this->db_antrione->trans_begin();
$sql = "UPDATE counter SET
counterCode = ?,
counterIP = ?,
counterIsDedicated = ?,
counterLocationID = ?,
counterMaxQueue = ?
WHERE
counterID = ?";
$qry = $this->db_antrione->query($sql, [$code, $ip, $isDedicated, intval($locationId), $maxQueue, intval($id)]);
$last_qry = $this->db_antrione->last_query();
if (!$qry) {
$error = array(
"message" => $this->db_antrione->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
$this->db_antrione->trans_rollback();
exit;
}
$sql = "UPDATE counter_service
SET counterServiceIsActive = 'N'
WHERE counterServiceCounterID = ?";
$qry = $this->db_antrione->query($sql, [intval($id)]);
$last_qry = $this->db_antrione->last_query();
if (!$qry) {
$error = array(
"message" => $this->db_antrione->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
$this->db_antrione->trans_rollback();
exit;
}
$bagian = "N semua ";
if ($isDedicatedBefore == $isDedicated) {
if ($isDedicated == 'Y') {
foreach ($service as $key => $value) {
if (isset($value['counterServiceID'])) {
$sql = "UPDATE counter_service
SET counterServiceIsActive = 'Y'
WHERE counterServiceID = ? ";
$qry = $this->db_antrione->query($sql, [intval($value['counterServiceID'])]);
$last_qry = $this->db_antrione->last_query();
if (!$qry) {
$error = array(
"message" => $this->db_antrione->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
$this->db_antrione->trans_rollback();
exit;
}
$bagian = "before after sama, dedicated = Y counterservice id 0(tambah)";
} elseif (isset($value['counterServiceID']) == false) {
$sql = "INSERT INTO counter_service
( counterServiceServiceID,
counterServiceCounterID)
VALUES (?, ?)";
$qry = $this->db_antrione->query($sql, [intval($value['serviceID']), intval($id)]);
$last_qry = $this->db_antrione->last_query();
if (!$qry) {
$error = array(
"message" => $this->db_antrione->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
$this->db_antrione->trans_rollback();
exit;
}
$bagian = "before after sama, dedicated = Y update counter service ";
}
}
}
} elseif ($isDedicatedBefore != $isDedicated) {
if ($isDedicated == 'Y') {
foreach ($service as $key => $value) {
$sql = "INSERT INTO counter_service
( counterServiceServiceID,
counterServiceCounterID)
VALUES (?, ?)";
$qry = $this->db_antrione->query($sql, [intval($value['serviceID']), intval($id)]);
$last_qry = $this->db_antrione->last_query();
if (!$qry) {
$error = array(
"message" => $this->db_antrione->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
$this->db_antrione->trans_rollback();
exit;
}
}
$bagian = "before after beda, dedicated = Y insert counter service";
}
}
$result = array(
"sql" => $last_qry,
"bagian" => $bagian
);
$this->db_antrione->trans_commit();
$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;
}
$prm = $this->sys_input;
$id = '';
if (isset($prm['id'])) {
$id = intval($prm["id"]);
}
$isDedicated = '';
if (isset($prm['isDedicated'])) {
$isDedicated = trim($prm["isDedicated"]);
}
if ($id == "" || $isDedicated == "") {
$msg = 'id, isDedicated is mandatory';
$this->sys_error($msg);
exit;
}
$this->db_antrione->trans_begin();
$sql = "UPDATE counter
SET counterIsActive = 'N'
WHERE counterID = ?";
$qry = $this->db_antrione->query($sql, [intval($id)]);
$last_qry = $this->db_antrione->last_query();
if (!$qry) {
$error = array(
"message" => $this->db_antrione->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
$this->db_antrione->trans_rollback();
exit;
}
if ($isDedicated == "Y") {
$sql = "UPDATE counter_service
SET counterServiceIsActive = 'N'
WHERE counterServiceCounterID =? ";
$qry = $this->db_antrione->query($sql, [intval($id)]);
$last_qry = $this->db_antrione->last_query();
if (!$qry) {
$error = array(
"message" => $this->db_antrione->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
$this->db_antrione->trans_rollback();
exit;
}
}
$result = array(
"sql" => $last_qry,
);
$this->db_antrione->trans_commit();
$this->sys_ok($result);
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function getLocation()
{
try {
if (!$this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$sql = "SELECT
locationID,
locationName
FROM location WHERE locationIsActive = 'Y'
ORDER BY locationName ASC";
$qry = $this->db_antrione->query($sql);
$last_qry = $this->db_antrione->last_query();
if (!$qry) {
$error = array(
"message" => $this->db_antrione->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
exit;
}
$rows = $qry->result_array();
$result = array(
"records" => $rows,
"sql" => $last_qry,
);
$this->sys_ok($result);
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
}