ordersprinter/php/roomtables.php

306 lines
9.9 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');
// require_once ('content.php');
class Roomtables {
var $dbutils;
function __construct() {
$this->dbutils = new DbUtils();
}
function handleCommand($command) {
if(session_id() == '') {
session_start();
if (!isset($_SESSION['angemeldet']) || !$_SESSION['angemeldet']) {
// no user logged in
return false;
}
}
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");
if($command == 'showAllRooms') {
$this->showAllRooms();
} else if ($command == 'getRooms') {
$this->getRooms(); // only rooms!
} else if ($command == 'showAllRoomsAndTablesWithUnpaidItems') {
$this->showAllRoomsAndTablesWithUnpaidItems();
} else if ($command == 'getUnpaidTables') {
$this->getUnpaidTables($_GET['roomid']);
} else if ($command == 'showAllTablesOfRoom') {
$this->showAllTablesOfRoom();
} else if ($command == 'getRoomfield') {
$this->getRoomfield();
} else if ($command == 'setRoomInfo') {
if ($this->hasCurrentUserAdminRights()) {
$this->setRoomInfo($_POST['rooms']);
}
}
}
private function hasCurrentUserAdminRights() {
if(session_id() == '') {
session_start();
}
if (!isset($_SESSION['angemeldet']) || !$_SESSION['angemeldet']) {
// no user logged in
return false;
} else {
return ($_SESSION['is_admin']);
}
}
function showAllRooms() {
$roomtables = $this->getAllTablesAndRooms();
echo json_encode($roomtables);
}
function getUnpaidTables($roomid) {
// first get all tables of that rooms
$tablesSql = "SELECT id,tableno FROM %resttables% WHERE %resttables%.roomid =? AND removed is null";
$pdo = $this->dbutils->openDbAndReturnPdo();
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($tablesSql));
$stmt->execute(array($roomid));
$result = $stmt->fetchAll();
$tablesArr = array();
foreach($result as $row) {
$entry = array("id" => $row['id'], "name" => $row['tableno']);
$tablesArr[] = $entry;
}
$tableresult = array();
foreach($tablesArr as $aTable) {
$tableid = $aTable['id'];
$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'
AND ordertime is not null";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($tableid));
$row = $stmt->fetchObject();
if ($row != null) {
$prodcount = $row->prodcount;
$sumprice = $row->sumprice;
if ($prodcount > 0) {
$aTableEntry = array("id" => $tableid,"name" => $aTable["name"], "sum" => $sumprice);
$tableresult[] = $aTableEntry;
}
}
}
echo json_encode($tableresult);
}
function showAllRoomsAndTablesWithUnpaidItems() {
$roomtables = $this->getAllTablesAndRooms();
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) {
// sql copied from getJsonProductsOfTableToPay
$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
ORDER BY ordertime;";
$pdo = $this->dbutils->openDbAndReturnPdo();
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$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";
$pdo = $this->dbutils->openDbAndReturnPdo();
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute();
$result = $stmt->fetchAll();
$roomArr = array();
foreach($result as $row) {
$roomEntry = array("id" => $row['id'], "name" => $row['roomname']);
$roomArr[] = $roomEntry;
}
echo json_encode($roomArr);
}
// Part of chain to choose a product , here we start with chosing a room
private function getAllTablesAndRooms()
{
$sql = "SELECT id,roomname FROM %room% WHERE removed is null";
$dbresult = $this->dbutils->performSqlCommand($sql);
$arrayOfRooms = array();
while ($zeile = mysqli_fetch_array( $dbresult, MYSQL_ASSOC)) {
$roomid = $zeile['id'];
// now find the tables that are in that room
$tablesArray = array();
$tablesSql = "SELECT id,tableno FROM %resttables% WHERE %resttables%.roomid ='$roomid' AND removed is null";
$dbresulttables = $this->dbutils->performSqlCommand($tablesSql);
while ($table_entry = mysqli_fetch_array( $dbresulttables, MYSQL_ASSOC)) {
$tableArrayEntry = array("id" => $table_entry['id'], "name" => $table_entry['tableno']);
$tablesArray[] = $tableArrayEntry;
}
mysqli_free_result($dbresulttables);
$aRoomEntry = array ("id" => $roomid, "name" => $zeile['roomname'], "tables" => $tablesArray);
$arrayOfRooms[] = $aRoomEntry;
}
mysqli_free_result( $dbresult );
return $arrayOfRooms;
}
// Part of chain to choose a product, here we know the room and show all tables there
function showAllTablesOfRoom()
{
// Parameter for this method
$roomid = $_GET['roomid'];
// Find name of room to display in table header
$sql = "SELECT id,roomname FROM %room% WHERE id='$roomid' AND removed is null";
$dbresult = $this->dbutils->performSqlCommand($sql);
$zeile = mysqli_fetch_array( $dbresult, MYSQL_ASSOC);
$roomid = $zeile['id'];
$roomname = $zeile['roomname'];
mysqli_free_result( $dbresult );
// Navigation table - home - back
echo '<table class=navitable>';
echo '<tr>';
echo '<td><input type="image" src="img/home.png" onclick="showAllRooms();" />';
echo '<td>' . $roomname;
echo '</tr></table>';
$nextFunction = $_GET['nextfunction'];
$sql = "SELECT id,tableno FROM " . DB_RESTTABLES_TABLE . " WHERE roomid=" . $roomid;
$dbresult = $this->dbutils->performSqlCommand($sql);
// create a table that is optimal (sqrt-like size)
$numberOfIcons = mysqli_num_rows($dbresult);
$arrayOfButtons = array();
while ($zeile = mysqli_fetch_array( $dbresult, MYSQL_ASSOC)) {
$roomid = $zeile['id'];
$onClickMethod = $nextFunction . '(' . $zeile['id'] . ',' . $roomid . ');';
$aButtonEntry = array ("textOfButton" => $zeile['tableno'],
"onClickMethod" => $onClickMethod);
$arrayOfButtons[] = $aButtonEntry;
}
mysqli_free_result( $dbresult );
$commonUtils = new CommonUtils();
$commonUtils->createGridTableWithSqrtSizeOfButtons($arrayOfButtons);
}
function setRoomInfo($rooms) {
$pdo = $this->dbutils->openDbAndReturnPdo();
$pdo->beginTransaction();
// clear room table
$sql = "UPDATE %room% SET removed=?";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array('1'));
// clear resttables table
$sql = "UPDATE %resttables% SET removed=?";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array('1'));
$noOfRooms = count($rooms);
for ($roomindex = 0;$roomindex < $noOfRooms; $roomindex++) {
$aRoom = $rooms[$roomindex];
$aRoomName = $aRoom[0];
$sql = "INSERT INTO `%room%` (`id`, `roomname`) VALUES (NULL,?)";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($aRoomName));
$roomId = $pdo->lastInsertId();
$tablesArr = $aRoom[1];
$noOfTables = count($tablesArr);
for ($tableindex = 0; $tableindex < $noOfTables; $tableindex++) {
$aTableName = $tablesArr[$tableindex];
$sql = "INSERT INTO `%resttables%` (`id` , `tableno`, `roomid`) VALUES (NULL ,?,?)";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($aTableName,$roomId));
}
}
$pdo->commit();
echo json_encode("OK");
}
function getRoomfield() {
$pdo = $this->dbutils->openDbAndReturnPdo();
$sql = "SELECT id,roomname FROM %room% WHERE removed is null ORDER BY 'sorting'";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute();
$result = $stmt->fetchAll();
$numberOfRooms = $stmt->rowCount();
$roomIdNameArray = array();
$maxTables = 0;
$roomArr = array();
foreach($result as $row) {
$roomid = $row['id'];
$roomname = $row['roomname'];
// now get the tables of this room
$sql = "SELECT id,tableno FROM %resttables% WHERE roomid=? AND removed is null ORDER BY 'sorting'";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($row['id']));
$numberOfTables = $stmt->rowCount();
$maxTables = ($maxTables < $numberOfTables ? $numberOfTables : $maxTables);
$tableresult = $stmt->fetchAll();
$tableArr = array();
foreach($tableresult as $aTable) {
$tableArr[] = array("id" => $aTable['id'], "tablename" => $aTable['tableno']);
}
$roomArr[] = array("roomid" => $roomid, "roomname" => $roomname, "tables" => $tableArr, "noOfTables" => $numberOfTables);
}
echo json_encode(array("noOfRooms" => $numberOfRooms, "maxTables" => $maxTables, "roomfield" => $roomArr));
}
}
?>