dbutils = new DbUtils(); } function handleCommand($command) { if(session_id() == '') { session_start(); if (!isset($_SESSION['angemeldet']) || !$_SESSION['angemeldet']) { echo json_encode(array("status" => "ERROR", "code" => ERROR_NOT_AUTHOTRIZED, "msg" => ERROR_NOT_AUTHOTRIZED_MSG)); } } 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 == 'getRoomfield') { $this->getRoomfield(); } else if ($command == 'getRoomfieldAlsoInactive') { $this->getRoomfieldAlsoInactive(); } else if ($command == 'setRoomInfo') { if ($this->hasCurrentUserAdminRights()) { $this->setRoomInfo($_POST['rooms'],$_POST['togoworkprinter']); } } } private function hasCurrentUserAdminRights() { if(session_id() == '') { session_start(); } if (!isset($_SESSION['angemeldet']) || !$_SESSION['angemeldet']) { return false; } else { return ($_SESSION['is_admin']); } } function showAllRooms() { $pdo = DbUtils::openDbAndReturnPdoStatic(); $roomtables = $this->getAllTablesAndRooms($pdo); echo json_encode($roomtables); } public static function getUnpaidTablesCore($pdo,$roomid) { $userarea = self::getUserArea($pdo); $areaWhere = " "; if (!is_null($userarea)) { $area = intval($userarea); $areaWhere = " AND R.area='$area' "; } $tablesSql = "SELECT id,tableno FROM %resttables% R WHERE R.roomid=? AND removed is null $areaWhere ORDER BY sorting"; $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; } $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 AND isclosed is null"; $stmt = $pdo->prepare(DbUtils::substTableAlias($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"], "pricesum" => $sumprice); $tableresult[] = $aTableEntry; } } } return($tableresult); } function getUnpaidTables($roomid) { $pdo = $this->dbutils->openDbAndReturnPdo(); $priceTakeAway = $this->getUnpaidSumOfTakeAway($pdo); echo json_encode(array("tables" => self::getUnpaidTablesCore($pdo,$roomid), "takeawayprice" => $priceTakeAway)); } function showAllRoomsAndTablesWithUnpaidItems() { $pdo = $this->dbutils->openDbAndReturnPdo($pdo); $roomtables = $this->getAllTablesAndRooms($pdo); for ($i=0;$ihasTableUnpaidItems($tableid)) { $newtablesArr[] = $tableentry; } } $roomtables[$i]["tables"] = $newtablesArr; } echo json_encode($roomtables); } function hasTableUnpaidItems($tableid) { $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() { $pdo = DbUtils::openDbAndReturnPdoStatic(); $userarea = self::getUserArea($pdo); $sql = "SELECT id,roomname FROM %room% WHERE removed is null ORDER BY sorting"; $result = CommonUtils::fetchSqlAll($pdo, $sql, null); $roomArr = array(); foreach($result as $row) { $tablesToLookAt = $this->hasUserResponsibleTablesInRoom($pdo, $row['id'], $userarea); if (!$tablesToLookAt) { continue; } $roomEntry = array("id" => $row['id'], "name" => $row['roomname']); $roomArr[] = $roomEntry; } $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 AND isclosed is null"; $stmt = $pdo->prepare(DbUtils::substTableAlias($sql)); $stmt->execute(); $row = $stmt->fetchObject(); return $row->pricesum; } private function hasUserResponsibleTablesInRoom($pdo,$roomid,$userarea) { if (is_null($userarea)) { return true; } $sql = "SELECT count(id) as countid FROM %resttables% R WHERE R.roomid=? AND R.area=?"; $row = CommonUtils::getRowSqlObject($pdo, $sql, array($roomid,$userarea)); $countid = $row->countid; if ($countid > 0) { return true; } else { return false; } } private static function getUserArea($pdo) { if(session_id() == '') { session_start(); } $userid = $_SESSION['userid']; $sql = "SELECT area FROM %user% WHERE id=?"; $row = CommonUtils::getRowSqlObject($pdo, $sql, array($userid)); return $row->area; } private function getAllTablesAndRooms($pdo) { $userarea = self::getUserArea($pdo); $queue = new QueueContent(); $sql = "SELECT id,roomname FROM %room% WHERE removed is null ORDER BY sorting"; $stmt = $pdo->prepare(DbUtils::substTableAlias($sql)); $stmt->execute(); $dbresult = $stmt->fetchAll(PDO::FETCH_ASSOC); $arrayOfRooms = array(); $showprepinwaiter = CommonUtils::getConfigValue($pdo, 'showprepinwaiter', 1); $workflowconfig = CommonUtils::getConfigValue($pdo, 'workflowconfig', 0); $queryprodForTableView = false; if (($showprepinwaiter == 1) && (($workflowconfig == 0) || ($workflowconfig == 1))) { $queryprodForTableView = true; } foreach($dbresult as $zeile) { $roomid = $zeile['id']; $tablesToLookAt = $this->hasUserResponsibleTablesInRoom($pdo, $roomid, $userarea); if (!$tablesToLookAt) { continue; } $tablesArray = array(); $areaWhere = " "; if (!is_null($userarea)) { $area = intval($userarea); $areaWhere = " AND R.area='$area' "; } $sql = "SELECT R.id as id,R.tableno as name,R.sorting as sorting,"; $sql .= " IFNULL(SUM(IF(Q.ordertime is not null AND Q.paidtime is null AND Q.isclosed is null,Q.price,0.00)),0.00) as pricesum FROM %resttables% R"; $sql .= " LEFT OUTER JOIN %queue% Q ON Q.tablenr=R.id WHERE R.removed is null AND active='1' AND "; $sql .= " R.roomid=? $areaWhere GROUP BY R.id,name "; $sql .= " ORDER BY R.sorting"; $stmt = $pdo->prepare(DbUtils::substTableAlias($sql)); $stmt->execute(array($roomid)); $tablesArray = $stmt->fetchAll(PDO::FETCH_OBJ); foreach ($tablesArray as $tableEntry) { $arrayOfProdsAndIdsOfATable = array("prods" => array(), "ids" => ''); if ($queryprodForTableView) { $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; } $aRoomEntry = array ("id" => $roomid, "name" => $zeile['roomname'], "tables" => $tablesArray); $arrayOfRooms[] = $aRoomEntry; } $priceTakeAway = $this->getUnpaidSumOfTakeAway($pdo); $arrayOfProdsAndIdsOfATable = array("prods" => array(), "ids" => ''); if ($showprepinwaiter == 1) { $arrayOfProdsAndIdsOfATable = $queue->getAllPreparedProductsForTableidAsArray($pdo,null); } $arrayOfProdsOfATable = $arrayOfProdsAndIdsOfATable['prods']; $numberOfProductsTotalToServe = $queue->numberOfProductsForTableNotDelivered($pdo,null); $numberOfReadyProducts = count($arrayOfProdsOfATable); $queueids = $this->getIdsFromProdList($arrayOfProdsOfATable); return array("roomstables" => $arrayOfRooms, "takeawayprice" => $priceTakeAway, "takeawayprodcount" => $numberOfProductsTotalToServe, "takeawayprodready" => $numberOfReadyProducts, "takeawayReadyQueueIds" => $queueids ); } function getIdsFromProdList($arrayOfProdsOfATable) { $idArr = array(); if (!is_null($arrayOfProdsOfATable) && (count($arrayOfProdsOfATable) > 0)) { foreach($arrayOfProdsOfATable as $queueEntry) { $idArr[] = $queueEntry["id"]; } return $idArr; } else { return array(); } } function setRoomInfo($roomsAsJson,$togoworkprinter) { $rooms = json_decode($roomsAsJson, true); $pdo = DbUtils::openDbAndReturnPdoStatic(); $pdo->beginTransaction(); try { $sql = "UPDATE %resttables% SET removed=1"; CommonUtils::execSql($pdo, $sql, null); $sql = "UPDATE %room% SET removed=1"; CommonUtils::execSql($pdo, $sql, null); foreach($rooms as $aRoom) { $roomid = $aRoom["roomid"]; $printer = $aRoom["printer"]; if ($printer == 0) { $printer = null; } $name = trim($aRoom["name"]); $sorting = trim($aRoom["sorting"]); $abbreviation = trim($aRoom["abbreviation"]); if (!is_numeric($roomid)) { $sql = "INSERT INTO %room% (roomname,abbreviation,printer,sorting) VALUES(?,?,?,?)"; CommonUtils::execSql($pdo, $sql, array($name,$abbreviation,$printer,$sorting)); $roomid = $pdo->lastInsertId(); } else { $sql = "UPDATE %room% SET removed=?,roomname=?,abbreviation=?,printer=?,sorting=? WHERE id=?"; CommonUtils::execSql($pdo, $sql, array(null,$name,$abbreviation,$printer,$sorting,$roomid)); } if (isset($aRoom["tables"])) { $tables = $aRoom["tables"]; foreach($tables as $t) { $tableid = $t["id"]; $tablename = $t["tablename"]; $name = $t["name"]; $code = ""; if (isset($t["code"])) { $code = $t["code"]; } $area = $t["area"]; if ($area == 0) { $area = null; } $sorting = $t["sorting"]; $active = $t["active"]; $allowoutorder = $t["allowoutorder"]; if (!is_numeric($tableid)) { $sql = "INSERT INTO %resttables% (tableno,roomid,code,name,area,active,allowoutorder,sorting) VALUES(?,?,?,?,?,?,?,?)"; CommonUtils::execSql($pdo, $sql, array($tablename,$roomid,$code,$name,$area,$active,$allowoutorder,$sorting)); } else { $sql = "UPDATE %resttables% SET removed=?,tableno=?,roomid=?,code=?,name=?,area=?,active=?,allowoutorder=?,sorting=? WHERE id=?"; CommonUtils::execSql($pdo, $sql, array(null,$tablename,$roomid,$code,$name,$area,$active,$allowoutorder,$sorting,$tableid)); } } } } $sql = "select %tablepos%.id as posid,%resttables%.removed FROM %tablepos%,%resttables% WHERE %resttables%.removed is not null AND %resttables%.id=%tablepos%.tableid"; $result = CommonUtils::fetchSqlAll($pdo, $sql, null); foreach($result as $r) { $sql = "DELETE FROM %tablepos% WHERE id=?"; CommonUtils::execSql($pdo, $sql, array($r["posid"])); }; $sql = "select %tablemaps%.id as posid,%room%.removed FROM %tablemaps%,%room% WHERE %room%.removed is not null AND %room%.id=%tablemaps%.roomid"; $result = CommonUtils::fetchSqlAll($pdo, $sql, null); foreach($result as $r) { $sql = "DELETE FROM %tablemaps% WHERE id=?"; CommonUtils::execSql($pdo, $sql, array($r["posid"])); } $hist = new HistFiller(); $hist->updateConfigInHist($pdo, "togoworkprinter", $togoworkprinter); $pdo->commit(); } catch (Exception $ex) { echo json_encode(array("status" => "ERROR","msg" => $ex->getMessage())); $pdo->rollBack(); return; } $this->getRoomfieldAlsoInactive($pdo); } function getRoomfieldAlsoInactive($pdo = null) { if (is_null($pdo)) { $pdo = $this->dbutils->openDbAndReturnPdo(); } $this->getRoomfieldCore($pdo, true); } function getRoomfield($pdo = null) { if (is_null($pdo)) { $pdo = DbUtils::openDbAndReturnPdoStatic(); } $this->getRoomfieldCore($pdo, false); } function getRoomfieldCore($pdo,$includeInActiveTables) { $sql = "SELECT id,roomname,IFNULL(abbreviation,'') as abbreviation,IFNULL(printer,0) as printer,sorting FROM %room% WHERE removed is null ORDER BY 'sorting'"; $stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql)); $stmt->execute(); $result = $stmt->fetchAll(); $numberOfRooms = count($result); $maxTables = 0; $roomArr = array(); $where = "removed is null AND active='1'"; if ($includeInActiveTables) { $where = "removed is null"; } foreach($result as $row) { $roomid = $row['id']; $roomname = $row['roomname']; $abbreviation = $row['abbreviation']; $printer = $row['printer']; $roomsorting = $row['sorting']; $sql = "SELECT id,tableno,IFNULL(code,'') as code,IFNULL(name,'') as name,IFNULL(allowoutorder,0) as allowoutorder,IFNULL(sorting,1) as sorting,IFNULL(active,1) as active,IFNULL(area,0) as area FROM %resttables% WHERE roomid=? AND $where 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'],"name" => $aTable['name'],"code" => $aTable['code'],"area" => $aTable['area'],"allowoutorder" => $aTable['allowoutorder'],"active" => $aTable['active'],"sorting" => $aTable['sorting']); } $roomArr[] = array("roomid" => $roomid, "roomname" => $roomname, "abbreviation" => $abbreviation, "printer" => $printer, "sorting" => $roomsorting, "tables" => $tableArr, "noOfTables" => $numberOfTables); } $togoworkprinter = CommonUtils::getConfigValue($pdo, "togoworkprinter", 0); echo json_encode(array("status" => "OK", "noOfRooms" => $numberOfRooms, "maxTables" => $maxTables, "roomfield" => $roomArr, "togoworkprinter" => $togoworkprinter)); } public static function getTablesForGuestsystem($pdo) { $sql = "SELECT id,name,code FROM %resttables% WHERE removed is null AND active=? AND allowoutorder=? AND code is not null"; $result = CommonUtils::fetchSqlAll($pdo, $sql, array(1,1)); return $result; } }