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

304 lines
14 KiB
PHP

<?php
class Ticket extends MY_Controller
{
var $db_antrione;
var $load;
function __construct()
{
parent::__construct();
$this->db_antrione = $this->load->database("antrione", true);
$this->IP_SOCKET_IO = "localhost";
// $this->IP_SOCKET_IO = "devone.aplikasi.web.id";
}
function index()
{
echo ('API GET ANTRIAN NUMBER');
}
function getAntrian()
{
try {
$prm = $this->sys_input;
$serviceId = '';
if (isset($prm['service_id'])) {
$serviceId = trim($prm["service_id"]);
}
$boothId = '';
if (isset($prm['booth_id'])) {
$boothId = trim($prm["booth_id"]);
}
if ($serviceId == '' || $boothId == '') {
$this->sys_error_db("service id & booth id is mandatory");
exit;
}
$this->db_antrione->trans_begin();
$sqlCek = "SELECT * FROM service
WHERE serviceIsActive = 'Y'
AND serviceID = ?";
$qryCek = $this->db_antrione->query($sqlCek, [$serviceId]);
$last_qry = $this->db_antrione->last_query();
if (!$qryCek) {
$error = array(
"message" => $this->db_antrione->error()["message"],
"sql" => $last_qry
);
$this->db_antrione->trans_rollback();
$this->sys_error_db($error);
exit;
}
$serviceCek = $qryCek->result_array();
if (count($serviceCek) == 0) {
$error = array(
"message" => "service tidak ada ",
);
$this->sys_error_db($error);
$this->db_antrione->trans_rollback();
exit;
}
$sqlGetLocation = "SELECT *, SUM(fn_get_max_queue(counterID)) as maxQueue FROM counter
LEFT JOIN counter_service ON counterID = counterServiceCounterID
AND counterServiceIsActive = 'Y'
JOIN location ON counterLocationID = locationID
AND locationIsActive = 'Y'
WHERE (counterIsDedicated = 'N' OR counterServiceServiceID = ?)
AND counterIsActive = 'Y'
GROUP BY locationID
ORDER BY locationID";
// $sqlGetLocation = "SELECT *, SUM(counterMaxQueue) as maxQueue FROM
// counter_service
// JOIN counter ON counterServiceCounterID = counterID
// AND counterIsActive = 'Y'
// JOIN location ON counterLocationID = locationID
// AND locationIsActive = 'Y'
// WHERE counterServiceServiceID = ?
// AND counterServiceIsActive = 'Y'
// GROUP BY locationID
// ORDER BY locationID";
$qrygetLocation = $this->db_antrione->query($sqlGetLocation, [$serviceId]);
$last_qry = $this->db_antrione->last_query();
if (!$qryCek) {
$error = array(
"message" => $this->db_antrione->error()["message"],
"sql" => $last_qry
);
$this->db_antrione->trans_rollback();
$this->sys_error_db($error);
exit;
}
$location = $qrygetLocation->result_array();
// print_r($location);
// exit;
$ticketMsg = '';
$locationIdFinal = 0;
$countLocation = count($location);
$masukMana = "";
if ($countLocation == 0) {
$locationIdFinal = 1;
$ticketMsg = "Anda Akan Dilayan Di Front Office";
$masukMana = "TIdak ada counter yang melayani";
}
if ($countLocation == 1) {
$locationId = intval($location[0]['locationID']);
$sqlCek = "SELECT COUNT(queueID) as total FROM queue
WHERE
DATE_FORMAT(queueCreated, '%Y-%m-%d') = DATE_FORMAT(NOW(), '%Y-%m-%d')
AND queueIsActive = 'Y'
AND queueStatusID <> 4
AND queueLocationID = ?
AND queueServiceID = ?";
$qryCek = $this->db_antrione->query($sqlCek, [$locationId, $serviceId]);
$last_qry = $this->db_antrione->last_query();
if (!$qryCek) {
$error = array(
"message" => $this->db_antrione->error()["message"],
"sql" => $last_qry
);
$this->db_antrione->trans_rollback();
$this->sys_error_db($error);
exit;
}
$queue = $qryCek->row_array();
$queueNum = intval($queue['total']);
if ($queueNum < $location[0]["maxQueue"]) {
$locationIdFinal = $location[0]['locationID'];
$ticketMsg = "Anda Akan Dilayan Di Front Office " . $location[0]['locationName'];
$masukMana = "ada 1 counter yang melayani";
} else {
$locationIdFinal = 1;
$ticketMsg = "Anda Akan Dilayan Di Front Office Lantai 1";
$masukMana = "ada 1 counter yang melayani";
}
}
if ($countLocation > 1) {
$locationDedicated = array();
foreach ($location as $value) {
$locationId = intval($value["locationID"]);
$sqlCek = "SELECT COUNT(queueID) as total FROM queue
WHERE
DATE_FORMAT(queueCreated, '%Y-%m-%d') = DATE_FORMAT(NOW(), '%Y-%m-%d')
AND queueIsActive = 'Y'
AND queueStatusID <> 4
AND queueLocationID = ?
AND queueServiceID = ?";
$qryCek = $this->db_antrione->query($sqlCek, [$locationId, $serviceId]);
$last_qry = $this->db_antrione->last_query();
if (!$qryCek) {
$error = array(
"message" => $this->db_antrione->error()["message"],
"sql" => $last_qry
);
$this->db_antrione->trans_rollback();
$this->sys_error_db($error);
exit;
}
$queue = $qryCek->row_array();
$queueNum = intval($queue['total']);
if ($value['counterIsDedicated'] == 'Y' && $queueNum < $value["maxQueue"]) {
$locationDedicated = $value;
break;
}
}
// print_r($locationDedicated);
// exit;
if ($locationDedicated) {
$locationIdFinal = $locationDedicated['locationID'];
$ticketMsg = "Anda Akan Dilayan Di Front Office " . $locationDedicated['locationName'];
} else {
for ($i = 0; $i < $countLocation; $i++) {
$val = $location[$i];
$maxQueue = intval($val["maxQueue"]);
$locationId = intval($val["locationID"]);
$locationName = $val['locationName'];
$isDedicated = $val['counterIsDedicated'];
$sqlCek = "SELECT COUNT(queueID) as total FROM queue
WHERE
DATE_FORMAT(queueCreated, '%Y-%m-%d') = DATE_FORMAT(NOW(), '%Y-%m-%d')
AND queueIsActive = 'Y'
AND queueStatusID <> 4
AND queueLocationID = ?
AND queueServiceID = ?";
$qryCek = $this->db_antrione->query($sqlCek, [$locationId, $serviceId]);
$last_qry = $this->db_antrione->last_query();
if (!$qryCek) {
$error = array(
"message" => $this->db_antrione->error()["message"],
"sql" => $last_qry
);
$this->db_antrione->trans_rollback();
$this->sys_error_db($error);
exit;
}
$queue = $qryCek->row_array();
$queueNum = intval($queue['total']);
if ($countLocation == ($i + 1) && $queueNum >= $maxQueue) {
$ticketMsg = "Anda Akan Dilayan Di Front Office " . $location[0]['locationName'];
$locationIdFinal = $location[0]['locationID'];
$masukMana = "countLocation == key && queueNum >= maxQueue";
break;
}
if ($queueNum < $maxQueue) {
$ticketMsg = "Anda Akan Dilayan Di Front Office " . $locationName;
$masukMana = "queueNum < maxQueue";
$locationIdFinal = $locationId;
break;
}
if ($queueNum >= $maxQueue) {
$masukMana = "queueNum >= maxQueue";
continue;
}
}
}
}
// print_r([$masukMana, $queueNum, $location, $queue, $ticketMsg]);
// exit;
$sqlGetNumber = "SELECT fn_get_numbering(?) AS number";
$qryGetNumber = $this->db_antrione->query($sqlGetNumber, [$serviceId]);
$last_qry = $this->db_antrione->last_query();
if (!$qryCek) {
$error = array(
"message" => $this->db_antrione->error()["message"],
"sql" => $last_qry
);
$this->db_antrione->trans_rollback();
$this->sys_error_db($error);
exit;
}
$number = $qryGetNumber->row_array();
$numberQueue = $number['number'];
$sqlInsert = "INSERT INTO queue
(queueNumber,
queueStatusID,
queueServiceID,
queueLocationID,
queueTicketBoothID)
VALUES
(?, 1, ?, ?, ?)";
$qryInsert = $this->db_antrione->query($sqlInsert, [$numberQueue, $serviceId, $locationIdFinal, $boothId]);
$last_qry = $this->db_antrione->last_query();
if (!$qryInsert) {
$error = array(
"message" => $this->db_antrione->error()["message"],
"sql" => $last_qry
);
$this->db_antrione->trans_rollback();
$this->sys_error_db($error);
exit;
}
$queueId = $this->db_antrione->insert_id();
$sqlLog = "INSERT INTO queuelog
(queueLogDate,
queueLogStatusID,
queueLogCounterID)
VALUES(NOW(),1,0)";
$qryLog = $this->db_antrione->query($sqlLog);
$last_qry = $this->db_antrione->last_query();
if (!$qryLog) {
$error = array(
"message" => $this->db_antrione->error()["message"],
"sql" => $last_qry
);
$this->db_antrione->trans_rollback();
$this->sys_error_db($error);
exit;
}
$logId = $this->db_antrione->insert_id();
$sqlUpdate = "UPDATE queue SET
queueQueueLogID = ?
WHERE queueID = ?";
$qryUpdate = $this->db_antrione->query($sqlUpdate, [$logId, $queueId]);
$last_qry = $this->db_antrione->last_query();
if (!$qryUpdate) {
$error = array(
"message" => $this->db_antrione->error()["message"],
"sql" => $last_qry
);
$this->db_antrione->trans_rollback();
$this->sys_error_db($error);
exit;
}
$logId = $this->db_antrione->insert_id();
$this->db_antrione->trans_complete();
$result = array(
"number" => $numberQueue,
"location" => $ticketMsg,
"bagian" => $masukMana,
"maxQueuePerLantai" => $location
);
file_get_contents("http://" . $this->IP_SOCKET_IO . ":9099/broadcast/printed.fo.{$serviceId}");
$this->sys_ok($result);
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
}