ordersprinter/webapp/php/roomtables.php

334 lines
11 KiB
PHP
Raw Normal View History

2020-11-19 22:44:19 +01:00
<?php
// Datenbank-Verbindungsparameter
require_once ('dbutils.php');
require_once ('commonutils.php');
2020-11-19 22:47:44 +01:00
require_once ('queuecontent.php');
2020-11-19 22:44:19 +01:00
// require_once ('content.php');
class Roomtables {
var $dbutils;
function __construct() {
$this->dbutils = new DbUtils();
}
function handleCommand($command) {
2020-11-19 23:00:46 +01:00
if(session_id() == '') {
2020-11-19 22:44:19 +01:00
session_start();
2020-11-19 23:00:46 +01:00
if (!isset($_SESSION['angemeldet']) || !$_SESSION['angemeldet']) {
2020-11-19 22:47:44 +01:00
echo json_encode(array("status" => "ERROR", "code" => ERROR_NOT_AUTHOTRIZED, "msg" => ERROR_NOT_AUTHOTRIZED_MSG));
2020-11-19 23:00:46 +01:00
}
2020-11-19 22:44:19 +01:00
}
2020-11-19 23:00:46 +01:00
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
2020-11-19 22:44:19 +01:00
2020-11-19 23:00:46 +01:00
if($command == 'showAllRooms') {
2020-11-19 22:44:19 +01:00
$this->showAllRooms();
} else if ($command == 'getRooms') {
$this->getRooms(); // only rooms!
} else if ($command == 'showAllRoomsAndTablesWithUnpaidItems') {
$this->showAllRoomsAndTablesWithUnpaidItems();
} else if ($command == 'getUnpaidTables') {
2020-11-19 22:54:51 +01:00
$this->getUnpaidTables($_GET['roomid']);
2020-11-19 22:44:19 +01:00
} else if ($command == 'getRoomfield') {
$this->getRoomfield();
} else if ($command == 'setRoomInfo') {
if ($this->hasCurrentUserAdminRights()) {
$this->setRoomInfo($_POST['rooms']);
}
}
}
private function hasCurrentUserAdminRights() {
2020-11-19 23:00:46 +01:00
if(session_id() == '') {
session_start();
}
if (!isset($_SESSION['angemeldet']) || !$_SESSION['angemeldet']) {
return false;
} else {
return ($_SESSION['is_admin']);
}
2020-11-19 22:44:19 +01:00
}
function showAllRooms() {
2020-11-19 22:47:44 +01:00
$pdo = $this->dbutils->openDbAndReturnPdo();
2020-11-19 23:00:46 +01:00
$roomtables = $this->getAllTablesAndRooms($pdo);
2020-11-19 22:44:19 +01:00
echo json_encode($roomtables);
}
2020-11-19 22:47:44 +01:00
public static function getUnpaidTablesCore($pdo,$roomid) {
2020-11-19 22:44:19 +01:00
$tablesSql = "SELECT id,tableno FROM %resttables% WHERE %resttables%.roomid =? AND removed is null";
2020-11-19 23:00:46 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($tablesSql));
$stmt->execute(array($roomid));
$result = $stmt->fetchAll();
$tablesArr = array();
foreach($result as $row) {
$entry = array("id" => $row['id'], "name" => $row['tableno']);
$tablesArr[] = $entry;
2020-11-19 22:44:19 +01:00
}
$tableresult = array();
foreach($tablesArr as $aTable) {
$tableid = $aTable['id'];
2020-11-19 23:00:46 +01:00
$sql = "SELECT sum(%queue%.price) as sumprice,count(%queue%.price) as prodcount
FROM %queue%
INNER JOIN %products% ON %queue%.productid = %products%.id
INNER JOIN %pricelevel% ON %queue%.pricelevel = %pricelevel%.id
WHERE tablenr = ? AND paidtime is null AND toremove <> '1'
2020-11-19 22:50:09 +01:00
AND ordertime is not null AND isclosed is null";
2020-11-19 22:44:19 +01:00
2020-11-19 23:00:46 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
2020-11-19 22:44:19 +01:00
$stmt->execute(array($tableid));
2020-11-19 23:00:46 +01:00
$row = $stmt->fetchObject();
2020-11-19 22:44:19 +01:00
if ($row != null) {
2020-11-19 23:00:46 +01:00
$prodcount = $row->prodcount;
2020-11-19 22:44:19 +01:00
$sumprice = $row->sumprice;
if ($prodcount > 0) {
2020-11-19 22:47:44 +01:00
$aTableEntry = array("id" => $tableid,"name" => $aTable["name"], "pricesum" => $sumprice);
2020-11-19 22:44:19 +01:00
$tableresult[] = $aTableEntry;
}
}
}
2020-11-19 22:47:44 +01:00
return($tableresult);
}
function getUnpaidTables($roomid) {
$pdo = $this->dbutils->openDbAndReturnPdo();
$priceTakeAway = $this->getUnpaidSumOfTakeAway($pdo);
echo json_encode(array("tables" => self::getUnpaidTablesCore($pdo,$roomid), "takeawayprice" => $priceTakeAway));
2020-11-19 22:44:19 +01:00
}
function showAllRoomsAndTablesWithUnpaidItems() {
2020-11-19 22:47:44 +01:00
$pdo = $this->dbutils->openDbAndReturnPdo($pdo);
$roomtables = $this->getAllTablesAndRooms($pdo);
2020-11-19 22:44:19 +01:00
for ($i=0;$i<count($roomtables);$i++) {
$tablesArr = $roomtables[$i]["tables"];
$newtablesArr = array();
for ($j=0;$j<count($tablesArr);$j++) {
$tableentry = $tablesArr[$j];
$tableid = $tableentry["id"];
if ($this->hasTableUnpaidItems($tableid)) {
$newtablesArr[] = $tableentry;
}
}
$roomtables[$i]["tables"] = $newtablesArr;
}
echo json_encode($roomtables);
}
function hasTableUnpaidItems($tableid) {
2020-11-19 23:00:46 +01:00
$sql = "SELECT %queue%.id as id,longname,%queue%.price as price,%pricelevel%.name as pricelevelname,%products%.id as prodid
FROM %queue%
INNER JOIN %products% ON %queue%.productid = %products%.id
INNER JOIN %pricelevel% ON %queue%.pricelevel = %pricelevel%.id
WHERE tablenr = $tableid AND paidtime is null AND toremove <> '1'
AND ordertime is not null
2020-11-19 22:44:19 +01:00
ORDER BY ordertime;";
$pdo = $this->dbutils->openDbAndReturnPdo();
2020-11-19 23:00:46 +01:00
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
2020-11-19 22:44:19 +01:00
$stmt->execute();
$count = $stmt->rowCount();
if ($count > 0) {
return true;
} else {
return false;
}
}
/*
* get only the rooms (for paydesk, because tables are dynamic due to their pay status)
*/
function getRooms() {
$sql = "SELECT id,roomname FROM %room% WHERE removed is null";
2020-11-19 23:00:46 +01:00
$pdo = $this->dbutils->openDbAndReturnPdo();
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
2020-11-19 22:44:19 +01:00
$stmt->execute();
2020-11-19 23:00:46 +01:00
$result = $stmt->fetchAll();
$roomArr = array();
2020-11-19 22:44:19 +01:00
foreach($result as $row) {
2020-11-19 23:00:46 +01:00
$roomEntry = array("id" => $row['id'], "name" => $row['roomname']);
$roomArr[] = $roomEntry;
2020-11-19 22:44:19 +01:00
}
2020-11-19 22:47:44 +01:00
$priceTakeAway = $this->getUnpaidSumOfTakeAway($pdo);
echo json_encode(array("roomstables" => $roomArr, "takeawayprice" => $priceTakeAway));
}
private function getUnpaidSumOfTakeAway($pdo) {
$sql = "SELECT IFNULL(SUM(IF(%queue%.ordertime is not null AND %queue%.paidtime is null,%queue%.price,0.00)),0.00) as pricesum FROM %queue% ";
$sql .= " WHERE %queue%.tablenr is null";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute();
$row = $stmt->fetchObject();
return $row->pricesum;
2020-11-19 22:44:19 +01:00
}
2020-11-19 23:00:46 +01:00
private function getAllTablesAndRooms($pdo)
2020-11-19 22:47:44 +01:00
{
2020-11-19 23:00:46 +01:00
$queue = new QueueContent();
2020-11-19 22:47:44 +01:00
$sql = "SELECT id,roomname FROM %room% WHERE removed is null ORDER BY id";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
2020-11-19 23:00:46 +01:00
$stmt->execute();
2020-11-19 22:47:44 +01:00
$dbresult = $stmt->fetchAll(PDO::FETCH_ASSOC);
2020-11-19 23:00:46 +01:00
2020-11-19 22:47:44 +01:00
$arrayOfRooms = array();
foreach($dbresult as $zeile) {
2020-11-19 23:00:46 +01:00
$roomid = $zeile['id'];
2020-11-19 22:44:19 +01:00
$tablesArray = array();
2020-11-19 22:47:44 +01:00
2020-11-19 22:50:09 +01:00
$sql = "SELECT %resttables%.id as id,%resttables%.tableno as name,IFNULL(SUM(IF(%queue%.ordertime is not null AND %queue%.paidtime is null AND %queue%.isclosed is null,%queue%.price,0.00)),0.00) as pricesum FROM %resttables% ";
2020-11-19 22:47:44 +01:00
$sql .= " LEFT OUTER JOIN %queue% ON %queue%.tablenr=%resttables%.id WHERE %resttables%.removed is null AND ";
2020-11-19 23:03:26 +01:00
$sql .= " %resttables%.roomid=? GROUP BY %resttables%.id,name";
2020-11-19 22:47:44 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($roomid));
$tablesArray = $stmt->fetchAll(PDO::FETCH_OBJ);
foreach ($tablesArray as $tableEntry) {
$arrayOfProdsAndIdsOfATable = $queue->getAllPreparedProductsForTableidAsArray($pdo,$tableEntry->id);
$arrayOfProdsOfATable = $arrayOfProdsAndIdsOfATable['prods'];
$numberOfProductsTotalToServe = $queue->numberOfProductsForTableNotDelivered($pdo,$tableEntry->id);
$numberOfReadyProducts = count($arrayOfProdsOfATable);
$queueids = $this->getIdsFromProdList($arrayOfProdsOfATable);
$tableEntry->prodcount = $numberOfProductsTotalToServe;
$tableEntry->prodready = $numberOfReadyProducts;
$tableEntry->readyQueueIds = $queueids;
}
2020-11-19 22:44:19 +01:00
$aRoomEntry = array ("id" => $roomid, "name" => $zeile['roomname'], "tables" => $tablesArray);
2020-11-19 23:00:46 +01:00
$arrayOfRooms[] = $aRoomEntry;
2020-11-19 22:47:44 +01:00
}
$priceTakeAway = $this->getUnpaidSumOfTakeAway($pdo);
$arrayOfProdsAndIdsOfATable = $queue->getAllPreparedProductsForTableidAsArray($pdo,null);
$arrayOfProdsOfATable = $arrayOfProdsAndIdsOfATable['prods'];
$numberOfProductsTotalToServe = $queue->numberOfProductsForTableNotDelivered($pdo,null);
$numberOfReadyProducts = count($arrayOfProdsOfATable);
$queueids = $this->getIdsFromProdList($arrayOfProdsOfATable);
2020-11-19 23:00:46 +01:00
2020-11-19 22:47:44 +01:00
return array("roomstables" => $arrayOfRooms, "takeawayprice" => $priceTakeAway,
"takeawayprodcount" => $numberOfProductsTotalToServe,
"takeawayprodready" => $numberOfReadyProducts,
"takeawayReadyQueueIds" => $queueids
2020-11-19 23:00:46 +01:00
);
2020-11-19 22:44:19 +01:00
}
2020-11-19 22:47:44 +01:00
function getIdsFromProdList($arrayOfProdsOfATable) {
$idArr = array();
if (!is_null($arrayOfProdsOfATable) && (count($arrayOfProdsOfATable) > 0)) {
foreach($arrayOfProdsOfATable as $queueEntry) {
$idArr[] = $queueEntry["id"];
}
return $idArr;
} else {
return array();
}
}
2020-11-19 22:54:51 +01:00
2020-11-19 22:44:19 +01:00
function setRoomInfo($rooms) {
$pdo = $this->dbutils->openDbAndReturnPdo();
$pdo->beginTransaction();
$sql = "UPDATE %room% SET removed=?";
2020-11-19 23:00:46 +01:00
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
2020-11-19 22:44:19 +01:00
$stmt->execute(array('1'));
2020-11-19 23:00:46 +01:00
$sql = "UPDATE %resttables% SET removed=?";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
2020-11-19 22:44:19 +01:00
$stmt->execute(array('1'));
2020-11-19 22:47:44 +01:00
$sql = "DELETE FROM %tablepos%";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute();
$sql = "DELETE FROM %tablemaps%";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute();
2020-11-19 22:44:19 +01:00
$noOfRooms = count($rooms);
for ($roomindex = 0;$roomindex < $noOfRooms; $roomindex++) {
$aRoom = $rooms[$roomindex];
$aRoomName = $aRoom[0];
2020-11-19 22:55:09 +01:00
$aRoomAbbr = $aRoom[1];
if ($aRoomAbbr == "") {
$aRoomAbbr = null;
}
$roomPrinter = $aRoom[2];
2020-11-19 22:47:44 +01:00
if ($roomPrinter == 0) {
$roomPrinter = null;
}
2020-11-19 22:44:19 +01:00
2020-11-19 22:55:09 +01:00
$sql = "INSERT INTO `%room%` (`id`, `roomname`, `abbreviation`, `printer`) VALUES (NULL,?,?,?)";
2020-11-19 23:00:46 +01:00
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
2020-11-19 22:55:09 +01:00
$stmt->execute(array($aRoomName,$aRoomAbbr,$roomPrinter));
2020-11-19 22:44:19 +01:00
$roomId = $pdo->lastInsertId();
2020-11-19 22:55:09 +01:00
$tablesArr = $aRoom[3];
2020-11-19 22:44:19 +01:00
$noOfTables = count($tablesArr);
for ($tableindex = 0; $tableindex < $noOfTables; $tableindex++) {
$aTableName = $tablesArr[$tableindex];
$sql = "INSERT INTO `%resttables%` (`id` , `tableno`, `roomid`) VALUES (NULL ,?,?)";
2020-11-19 23:00:46 +01:00
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
2020-11-19 22:44:19 +01:00
$stmt->execute(array($aTableName,$roomId));
}
}
$pdo->commit();
echo json_encode("OK");
}
function getRoomfield() {
2020-11-19 23:00:46 +01:00
$pdo = $this->dbutils->openDbAndReturnPdo();
2020-11-19 22:44:19 +01:00
2020-11-19 22:55:09 +01:00
$sql = "SELECT id,roomname,IFNULL(abbreviation,'') as abbreviation,IFNULL(printer,0) as printer FROM %room% WHERE removed is null ORDER BY 'sorting'";
2020-11-19 23:00:46 +01:00
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
2020-11-19 22:44:19 +01:00
$stmt->execute();
$result = $stmt->fetchAll();
$numberOfRooms = $stmt->rowCount();
2020-11-19 23:00:46 +01:00
$roomIdNameArray = array();
2020-11-19 22:44:19 +01:00
$maxTables = 0;
2020-11-19 23:00:46 +01:00
$roomArr = array();
2020-11-19 22:44:19 +01:00
foreach($result as $row) {
$roomid = $row['id'];
$roomname = $row['roomname'];
2020-11-19 22:55:09 +01:00
$abbreviation = $row['abbreviation'];
2020-11-19 22:47:44 +01:00
$printer = $row['printer'];
2020-11-19 22:44:19 +01:00
$sql = "SELECT id,tableno FROM %resttables% WHERE roomid=? AND removed is null ORDER BY 'sorting'";
2020-11-19 23:00:46 +01:00
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
2020-11-19 22:44:19 +01:00
$stmt->execute(array($row['id']));
$numberOfTables = $stmt->rowCount();
2020-11-19 23:00:46 +01:00
$maxTables = ($maxTables < $numberOfTables ? $numberOfTables : $maxTables);
2020-11-19 22:44:19 +01:00
$tableresult = $stmt->fetchAll();
$tableArr = array();
foreach($tableresult as $aTable) {
$tableArr[] = array("id" => $aTable['id'], "tablename" => $aTable['tableno']);
}
2020-11-19 23:00:46 +01:00
$roomArr[] = array("roomid" => $roomid, "roomname" => $roomname, "abbreviation" => $abbreviation, "printer" => $printer, "tables" => $tableArr, "noOfTables" => $numberOfTables);
2020-11-19 22:44:19 +01:00
}
2020-11-19 22:47:44 +01:00
echo json_encode(array("status" => "OK", "noOfRooms" => $numberOfRooms, "maxTables" => $maxTables, "roomfield" => $roomArr));
2020-11-19 22:44:19 +01:00
}
}
?>