ordersprinter/webapp/php/queuecontent.php

2547 lines
92 KiB
PHP
Raw Permalink Normal View History

2020-11-19 22:47:44 +01:00
<?php
require_once ('dbutils.php');
require_once ('products.php');
require_once ('commonutils.php');
require_once ('printqueue.php');
require_once ('utilities/userrights.php');
class QueueContent {
var $dbutils;
var $commonUtils;
var $userrights;
2020-11-19 23:14:02 +01:00
public static $INTERNAL_CALL_NO = false;
public static $INTERNAL_CALL_YES = true;
2020-11-19 22:47:44 +01:00
public static $lastSettingOfDisplayMode = 'all';
function __construct() {
$this->dbutils = new DbUtils();
$this->commonUtils = new CommonUtils();
$this->userrights = new Userrights();
}
function handleCommand($command) {
2020-11-19 22:59:47 +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:47:44 +01:00
if ($command == "getJsonTableNameFromId") {
$this->getJsonTableNameFromId($_GET['tableid']);
return;
}
2020-11-19 23:12:18 +01:00
if ($command == "addAndCashProdByRestCall") {
$pdo = DbUtils::openDbAndReturnPdoStatic();
$this->addAndCashProdByRestCall($pdo, $_POST);
return;
}
2020-11-19 22:47:44 +01:00
// these command are only allowed for user with supply rights
2020-11-19 22:59:47 +01:00
$cmdArray = array('getJsonAllPreparedProducts', 'getJsonLastDeliveredProducts', 'declareProductBeDelivered', 'declareMultipleProductsDelivered','declareProductNotBeDelivered');
2020-11-19 22:47:44 +01:00
if (in_array($command, $cmdArray)) {
if (!($this->userrights->hasCurrentUserRight('right_supply'))) {
2020-11-19 23:11:42 +01:00
echo json_encode(array("status" => "ERROR","msg" => "Fehlende Benutzerrechte"));
2020-11-19 22:47:44 +01:00
return false;
}
}
// these command are only allowed for user with kitchen or bar rights
$cmdArray = array('declareProductBeCookingOrCooked', 'declareProductNOTBeCooked');
2020-11-19 22:59:47 +01:00
if (in_array($command, $cmdArray)) {
if (!($this->userrights->hasCurrentUserRight('right_kitchen')) && !($this->userrights->hasCurrentUserRight('right_bar'))) {
echo "Benutzerrechte nicht ausreichend!";
return false;
}
2020-11-19 22:47:44 +01:00
}
// these command are only allowed for user with waiter rights
2020-11-19 22:59:47 +01:00
$cmdArray = array('addProductListToQueue', 'removeProductFromQueue', 'changeTable','getProdsForTableChange');
if (in_array($command, $cmdArray)) {
if (!($this->userrights->hasCurrentUserRight('right_waiter'))) {
echo "Benutzerrechte nicht ausreichend!";
return false;
}
2020-11-19 22:47:44 +01:00
}
2020-11-19 22:59:47 +01:00
// these command are only allowed for user with paydesk rights
$cmdArray = array('getJsonProductsOfTableToPay', 'declarePaidCreateBillReturnBillId');
if (in_array($command, $cmdArray)) {
2020-11-19 22:47:44 +01:00
if (!($this->userrights->hasCurrentUserRight('right_paydesk'))) {
2020-11-19 22:59:47 +01:00
echo json_encode(array("status" => "ERROR", "code" => ERROR_PAYDESK_NOT_AUTHOTRIZED, "msg" => ERROR_PAYDESK_NOT_AUTHOTRIZED_MSG));
return false;
}
2020-11-19 22:47:44 +01:00
}
if ($command == 'addProductListToQueue') {
$this->addProductListToQueue($_POST["tableid"],$_POST["prods"],$_POST["print"],$_POST["payprinttype"]);
2020-11-19 23:10:26 +01:00
} else if ($command == 'getRecords') {
2020-11-19 23:12:13 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
$this->getRecords($pdo,$_GET["tableid"]);
} else if ($command == 'getAllTableRecords') {
$this->getAllTableRecords();
2020-11-19 23:12:30 +01:00
} else if ($command == 'getAllOrders') {
$this->getAllOrders();
2020-11-19 22:47:44 +01:00
} else if ($command == 'kitchenToCook') {
$this->kitchenToCook();
} else if ($command == 'declareProductBeCookingOrCooked') {
$this->declareProductBeCookingOrCooked($_POST['queueid'],$_POST['action']);
} else if ($command == 'declareProductNotBeCooked') {
$this->declareProductNotBeCooked($_POST['queueid']);
} else if ($command == 'showProductsOfTableToPay') {
$this->showProductsOfTableToPay($_GET['tableid']);
} else if ($command == 'getJsonAllPreparedProducts') {
$this->getJsonAllPreparedProducts();
} else if ($command == 'declareProductBeDelivered') {
$this->declareProductBeDelivered($_POST['queueid']);
} else if ($command == 'declareMultipleProductsDelivered') {
$this->declareMultipleProductsDelivered($_POST['queueids']);
} else if ($command == 'declareProductNotBeDelivered') {
$this->declareProductNotBeDelivered($_POST['queueid']);
} else if ($command == 'getJsonLongNamesOfProdsForTableNotDelivered') {
$this->getJsonLongNamesOfProdsForTableNotDelivered($_GET["tableid"]);
2020-11-19 22:55:43 +01:00
} else if ($command == 'getUnpaidTables') {
$this->getUnpaidTables();
2020-11-19 22:50:09 +01:00
} else if ($command == 'getProdsForTableChange') {
$this->getProdsForTableChange($_GET['tableId']);
2020-11-19 22:47:44 +01:00
} else if ($command == 'changeTable') {
2020-11-19 22:50:09 +01:00
$this->changeTable($_POST['fromTableId'],$_POST['toTableId'],$_POST['queueids']);
2020-11-19 22:47:44 +01:00
} else if ($command == 'removeProductFromQueue') {
$this->removeProductFromQueue($_POST["queueid"],$_POST["isPaid"],$_POST["isCooking"],$_POST["isReady"]);
} else if ($command == 'getJsonAllQueueItemsToMake') {
$this->getJsonAllQueueItemsToMake(intval($_GET["kind"]));
} else if ($command == 'getJsonLastMadeItems') {
$this->getJsonLastMadeItems(intval($_GET["kind"]));
} else if ($command == 'getJsonLastDeliveredProducts') {
$this->getJsonLastDeliveredProducts();
} else if ($command == 'getJsonProductsOfTableToPay') {
$this->getJsonProductsOfTableToPay($_GET['tableid']);
} else if ($command == 'declarePaidCreateBillReturnBillId') {
2020-11-19 23:00:55 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
2020-11-19 23:14:02 +01:00
$this->declarePaidCreateBillReturnBillId($pdo,$_POST['ids'],$_POST['tableid'],$_POST['paymentid'],$_POST['declareready'],$_POST['host'],self::$INTERNAL_CALL_NO,$_POST['reservationid'],$_POST['guestinfo'],$_POST['intguestid'],null,null);
2020-11-19 22:47:44 +01:00
} else {
echo "Command not supported.";
2020-11-19 22:59:47 +01:00
}
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:00:58 +01:00
2020-11-19 23:00:55 +01:00
private static function setNewProductsToServe($pdo,$val) {
2020-11-19 23:10:48 +01:00
self::setWorkItemFlag($pdo, "newproductstoserve", $val);
}
private static function getNewProductsToServe($pdo) {
return self::getWorkItemFlag($pdo, "newproductstoserve");
}
private static function setNewFoodToCookFlag($pdo,$val) {
self::setWorkItemFlag($pdo, "newfoodtocook", $val);
}
private static function getNewFoodToCookFlag($pdo) {
return self::getWorkItemFlag($pdo, "newfoodtocook");
}
private static function setNewDrinkToCookFlag($pdo,$val) {
self::setWorkItemFlag($pdo, "newdrinktocook", $val);
}
private static function getNewDrinkToCookFlag($pdo) {
return self::getWorkItemFlag($pdo, "newdrinktocook");
}
private static function setWorkItemFlag($pdo,$item,$val) {
2020-11-19 23:00:55 +01:00
$sql = "SELECT count(id) as countid FROM %work% WHERE item=?";
2020-11-19 23:10:48 +01:00
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($item));
2020-11-19 23:00:55 +01:00
if ($row->countid == 0) {
$sql = "INSERT INTO %work% (item,value,signature) VALUES (?,?,?)";
2020-11-19 23:10:48 +01:00
CommonUtils::execSql($pdo, $sql, array($item,$val,null));
2020-11-19 23:00:55 +01:00
} else {
$sql = "UPDATE %work% SET value=? WHERE item=?";
2020-11-19 23:10:48 +01:00
CommonUtils::execSql($pdo, $sql, array($val,$item));
2020-11-19 23:00:55 +01:00
}
}
2020-11-19 23:10:48 +01:00
private static function getWorkItemFlag($pdo,$item) {
2020-11-19 23:00:55 +01:00
$sql = "SELECT count(id) as countid FROM %work% WHERE item=?";
2020-11-19 23:10:48 +01:00
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($item));
2020-11-19 23:00:55 +01:00
if ($row->countid == 0) {
return 0;
} else {
$sql = "SELECT value FROM %work% WHERE item=?";
2020-11-19 23:10:48 +01:00
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($item));
2020-11-19 23:00:55 +01:00
return $row->value;
}
}
2020-11-19 22:47:44 +01:00
function getJsonTableNameFromId($tableid) {
2020-11-19 22:54:51 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
$commonUtils = new CommonUtils();
echo json_encode($commonUtils->getTableNameFromId($pdo,$tableid));
2020-11-19 22:47:44 +01:00
}
function getUserName($userid) {
2020-11-19 22:59:47 +01:00
$pdo = $this->dbutils->openDbAndReturnPdo();
2020-11-19 23:10:06 +01:00
if (is_null($userid)) {
return "";
}
2020-11-19 22:59:47 +01:00
$sql = "SELECT username FROM %user% WHERE id=?";
2020-11-19 23:00:55 +01:00
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($userid));
2020-11-19 22:59:47 +01:00
if ($row != null) {
2020-11-19 22:47:44 +01:00
return($row->username);
} else {
return "";
}
}
private function areBillExisting($pdo) {
$sql = "SELECT count(id) as countid FROM %bill%";
2020-11-19 23:00:55 +01:00
$row = CommonUtils::getRowSqlObject($pdo, $sql, null);
2020-11-19 22:47:44 +01:00
$count = intval($row->countid);
if ($count > 0) {
return true;
} else {
return false;
}
}
/*
* Get the queue items for the kitchen view that have to be still be cooked
* as a json element array
*
* 1. It is sorted for ordertime
* 2. From this ordertime search for the distinct tables
* 3. Sort it that way that tables are grouped together
*
* $kind=0 -> return only food elements, =1 -> return drinks
*/
private function getJsonAllQueueItemsToMake($kind) {
2020-11-19 22:59:47 +01:00
date_default_timezone_set(DbUtils::getTimeZone());
2020-11-19 22:47:44 +01:00
$currentTime = date('Y-m-d H:i:s');
2020-11-19 23:10:48 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
2020-11-19 22:47:44 +01:00
2020-11-19 23:10:06 +01:00
Guestsync::sync($pdo);
2020-11-19 22:47:44 +01:00
2020-11-19 23:12:35 +01:00
$unit = CommonUtils::caseOfSqlUnitSelection($pdo);
2020-11-19 22:47:44 +01:00
if ($this->areBillExisting($pdo)) {
2020-11-19 23:15:07 +01:00
$sql = "SELECT DISTINCT Q.id as id,IF(Q.orderuser IS NULL,'1','0') as isguestorder,%resttables%.id as tableid,tablenr,Q.unit,CONCAT($unit,Q.productname) as longname,anoption,tableno,date_format(ordertime,'%Y-%m-%d %H:%i:00') as ordertime,cooking,TIMESTAMPDIFF(MINUTE,ordertime,?) AS waittime FROM %queue% Q,%products%,%prodtype%,%resttables%,%bill% ";
2020-11-19 22:47:44 +01:00
} else {
2020-11-19 23:15:07 +01:00
$sql = "SELECT DISTINCT Q.id as id,IF(Q.orderuser IS NULL,'1','0') as isguestorder,%resttables%.id as tableid,tablenr,Q.unit,CONCAT($unit,Q.productname) as longname,anoption,tableno,date_format(ordertime,'%Y-%m-%d %H:%i:00') as ordertime,cooking,TIMESTAMPDIFF(MINUTE,ordertime,?) AS waittime FROM %queue% Q,%products%,%prodtype%,%resttables% ";
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:00:46 +01:00
$sql .= "WHERE (readytime IS NULL AND ";
2020-11-19 23:12:35 +01:00
$sql .= "Q.toremove='0' AND ";
$sql .= "Q.productid=%products%.id AND ";
$sql .= "Q.tablenr = %resttables%.id AND ";
2020-11-19 22:47:44 +01:00
$sql .= "%products%.category=%prodtype%.id AND ";
$sql .= "%prodtype%.kind=? AND ";
2020-11-19 23:12:35 +01:00
$sql .= "Q.isclosed is null AND ";
$sql .= "Q.workprinted='0') ";
2020-11-19 22:47:44 +01:00
if ($this->areBillExisting($pdo)) {
2020-11-19 23:12:35 +01:00
$sql .= "AND (Q.billid is null OR (";
$sql .= "Q.billid=%bill%.id AND %bill%.closingid is null)) ";
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:12:35 +01:00
$sql .= "ORDER BY ordertime,longname,anoption,cooking DESC,Q.id";
2020-11-19 22:47:44 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($currentTime,$kind));
$result1 = $stmt->fetchAll();
2020-11-19 23:15:07 +01:00
$sql = "SELECT DISTINCT Q.id as id,IF(Q.orderuser IS NULL,'1','0') as isguestorder,'-' as tableid,'-' as tablenr,Q.unit,CONCAT($unit,Q.productname) as longname,anoption,'-' as tableno,date_format(ordertime,'%Y-%m-%d %H:%i:00') as ordertime,cooking,TIMESTAMPDIFF(MINUTE,ordertime,?) AS waittime FROM %products%,%prodtype%,%queue% Q LEFT OUTER JOIN %bill% b ";
2020-11-19 23:12:35 +01:00
$sql .= " ON Q.billid=b.id ";
2020-11-19 23:00:46 +01:00
$sql .= "WHERE (readytime IS NULL AND ";
2020-11-19 23:12:35 +01:00
$sql .= "Q.toremove='0' AND ";
$sql .= "Q.productid=%products%.id AND ";
$sql .= "Q.tablenr is null AND ";
2020-11-19 22:47:44 +01:00
$sql .= "%products%.category=%prodtype%.id AND ";
$sql .= "%prodtype%.kind=? AND ";
2020-11-19 23:12:35 +01:00
$sql .= "Q.isclosed is null AND ";
$sql .= "Q.workprinted='0') ";
$sql .= "AND (Q.billid is null OR ( ";
2020-11-19 23:00:05 +01:00
$sql .= "b.closingid is null)) ";
2020-11-19 22:47:44 +01:00
$sql .= "ORDER BY ordertime,longname,anoption,cooking DESC";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($currentTime,$kind));
$result2 = $stmt->fetchAll();
$result = array_merge($result1,$result2);
$resultarray = array();
foreach($result as $zeile) {
$waitTime = $zeile['waittime'];
$cook = $zeile['cooking'];
if (is_null($cook)) {
$cook = 0;
}
$arr = array("id" => $zeile['id'],
2020-11-19 23:12:50 +01:00
"isguestorder" => $zeile['isguestorder'],
2020-11-19 22:47:44 +01:00
"tableid" => $zeile['tableid'],
"tablenr" => $zeile['tableno'],
"longname" => $zeile['longname'],
"option" => $zeile['anoption'],
"cooking" => $cook,
"waittime" => $waitTime
);
$resultarray[] = $arr;
}
$tablearray = array();
$insertedTables = array();
$table = (-1);
if (count($resultarray) <> 0) {
for ($queue_index=0;$queue_index < count($resultarray);$queue_index++) {
$aTable = $resultarray[$queue_index]['tablenr'];
if (($table <> $aTable) && !in_array($aTable,$insertedTables)) {
$table = $aTable;
$tableid = $resultarray[$queue_index]['tableid'];
$maxWaitTime = $resultarray[$queue_index]['waittime'];
$tableArr = array();
for ($i=0;$i<count($resultarray);$i++) {
if ($resultarray[$i]['tablenr'] == $table) {
$foundItem = $resultarray[$i];
$waittimeofentry = intval($foundItem['waittime']);
$waitIconMinStep = 1;
if ($waittimeofentry <= 1) {
$waitIconMinStep = 1;
} else if ($waittimeofentry <= 20) {
$waitIconMinStep = strval($waittimeofentry);
} else if ($waittimeofentry <= 25) {
$waitIconMinStep = 25;
} else if ($waittimeofentry <= 30) {
$waitIconMinStep = 30;
} else if ($waittimeofentry <= 40) {
$waitIconMinStep = 40;
} else if ($waittimeofentry <= 50) {
$waitIconMinStep = 50;
} else {
$waitIconMinStep = 60;
}
$waitIconMinStep = $waitIconMinStep . 'min.png';
$extras = $this->getExtrasOfQueueItem($pdo,$foundItem['id']);
$anEntryForThisTable = array("id" => $foundItem['id'],
"longname" => $foundItem['longname'],
2020-11-19 23:12:50 +01:00
"isguestorder" => $foundItem['isguestorder'],
2020-11-19 22:47:44 +01:00
"option" => $foundItem['option'],
"extras" => $extras,
"cooking" => $this->getUserName($foundItem['cooking']),
"waiticon" => $waitIconMinStep,
"waittime" => $waittimeofentry
);
$tableArr[] = $anEntryForThisTable;
}
}
if (($maxWaitTime > 20) && ($maxWaitTime < 60)) {
if ($maxWaitTime >= 50) {
$maxWaitTime = "> 50";
} else if ($maxWaitTime >= 40) {
$maxWaitTime = "> 40";
} else if ($maxWaitTime >= 30) {
$maxWaitTime = "> 30";
} else if ($maxWaitTime >= 25) {
$maxWaitTime = "> 25";
} else {
$maxWaitTime = "> 20";
}
} else if ($maxWaitTime <= 1) {
$maxWaitTime = "1";
}
$tablearray[] = array("table" => $table, "tableid" => $tableid,"count" => count($tableArr), "queueitems" => $tableArr, "maxwaittime" => $maxWaitTime);
$insertedTables[] = $aTable;
}
}
}
2020-11-19 23:10:48 +01:00
if ($kind == 0) {
$newProductsToMake = self::getNewFoodToCookFlag($pdo);
self::setNewFoodToCookFlag($pdo, 0);
} else {
$newProductsToMake = self::getNewDrinkToCookFlag($pdo);
self::setNewDrinkToCookFlag($pdo, 0);
}
$ret = array("status" => "OK","msg" => array("newproducts" => $newProductsToMake,"tocook" => $tablearray));
echo json_encode($ret);
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:14:48 +01:00
private function getExtrasOfQueueItemInTicketFormat($pdo,$queueid) {
$pureExtrasStrings = $this->getExtrasOfQueueItem($pdo, $queueid);
$extras = array();
foreach($pureExtrasStrings as $extraText) {
$extras[] = array("name" => $extraText);
}
return $extras;
}
2020-11-19 22:47:44 +01:00
private function getExtrasOfQueueItem($pdo,$queueid) {
if (is_null($pdo)) {
2020-11-19 23:14:48 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:12:39 +01:00
$sql = "SELECT CONCAT(amount,'x ',name) as name FROM %queueextras% WHERE queueid=?";
2020-11-19 23:14:48 +01:00
$extras = CommonUtils::fetchSqlAll($pdo, $sql, array($queueid));
2020-11-19 22:47:44 +01:00
$extrasarr = array();
foreach($extras as $anExtra) {
$extrasarr[] = $anExtra["name"];
}
return $extrasarr;
}
2020-11-19 22:58:27 +01:00
private function getExtrasWithIdsOfQueueItem($pdo,$queueid) {
if (is_null($pdo)) {
$pdo = $this->dbutils->openDbAndReturnPdo();
}
2020-11-19 23:12:39 +01:00
$sql = "SELECT name,extraid,amount FROM %queueextras% WHERE queueid=?";
2020-11-19 22:58:27 +01:00
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($queueid));
$extras = $stmt->fetchAll();
$extrasnames = array();
$extrasids = array();
2020-11-19 23:12:48 +01:00
$extrasamounts = array();
2020-11-19 22:58:27 +01:00
foreach($extras as $anExtra) {
$extrasnames[] = $anExtra["name"];
$extrasids[] = $anExtra["extraid"];
2020-11-19 23:12:39 +01:00
$extrasamounts[] = $anExtra["amount"];
2020-11-19 22:58:27 +01:00
}
2020-11-19 23:12:39 +01:00
return array("extrasnames" => $extrasnames,"extrasids" => $extrasids,"extrasamounts" => $extrasamounts);
2020-11-19 22:58:27 +01:00
}
2020-11-19 22:47:44 +01:00
private function getJobsToPrint($pdo,$kind,$printer,$queueIds) {
if (is_null($queueIds) || (count($queueIds) == 0)) {
return array();
}
$queueStr = implode(',',$queueIds);
2020-11-19 23:02:21 +01:00
$decpoint = CommonUtils::getConfigValue($pdo, 'decpoint', '.');
2020-11-19 23:03:26 +01:00
$groupWorkItems = 'groupworkitemsf';
if ($kind == 1) {
$groupWorkItems = 'groupworkitemsd';
}
2020-11-19 22:53:50 +01:00
$sql = "SELECT setting FROM %config% where name=?";
2020-11-19 23:03:26 +01:00
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($groupWorkItems));
2020-11-19 22:53:50 +01:00
$groupworkitems = $row->setting;
if (is_null($groupworkitems)) {
$groupworkitems = 1;
}
2020-11-19 23:12:35 +01:00
$unit = CommonUtils::caseOfSqlUnitSelection($pdo);
$sql = "SELECT Q.id as id,Q.tablenr as tableid, Q.productid,CONCAT($unit,Q.productname) as longname,Q.price as price,Q.togo as togo,";
$sql .= "anoption,%prodtype%.kind as kind,%prodtype%.printer as printer,fixbind FROM %queue% Q,%products%,%prodtype% ";
$sql .= "WHERE %prodtype%.kind=? AND Q.id IN ($queueStr) AND productid=%products%.id AND %products%.category=%prodtype%.id ORDER BY longname";
2020-11-19 23:12:05 +01:00
$queueItems = CommonUtils::fetchSqlAll($pdo, $sql, array($kind));
2020-11-19 22:47:44 +01:00
$jobs = array();
foreach($queueItems as $aQueueItem) {
$thePrinter = $aQueueItem["printer"];
2020-11-19 23:12:05 +01:00
$fixbind = $aQueueItem["fixbind"];
2020-11-19 22:47:44 +01:00
$queueid = $aQueueItem["id"];
$tableid = $aQueueItem["tableid"];
2020-11-19 23:12:05 +01:00
if ($fixbind == 0) {
if (!is_null($tableid) && ($tableid != 0)) {
$sql = "SELECT printer FROM %resttables% T,%room% R WHERE T.id=? AND T.roomid=R.id";
$r = CommonUtils::fetchSqlAll($pdo, $sql, array($tableid));
if (count($r) > 0) {
$roomPrinter = $r[0]["printer"];
if (!is_null($roomPrinter)) {
$thePrinter = $roomPrinter;
}
}
} else {
$roomPrinter = CommonUtils::getConfigValue($pdo, "togoworkprinter", 0);
if ($roomPrinter != 0) {
$thePrinter = $roomPrinter;
}
2020-11-19 23:11:57 +01:00
}
2020-11-19 22:47:44 +01:00
}
if ($thePrinter == $printer) {
$extras = $this->getExtrasOfQueueItem($pdo,$queueid);
2020-11-19 23:02:21 +01:00
$formattedPrice = number_format($aQueueItem["price"], 2, $decpoint, '');
2020-11-19 23:02:27 +01:00
2020-11-19 22:47:44 +01:00
$theEntry = array(
"id" => $queueid,
"productid" => $aQueueItem["productid"],
"longname" => $aQueueItem["longname"],
2020-11-19 23:10:35 +01:00
"singleprod" => $aQueueItem["longname"],
2020-11-19 23:02:21 +01:00
"price" => $formattedPrice,
2020-11-19 23:02:27 +01:00
"togo" => $aQueueItem["togo"],
2020-11-19 22:47:44 +01:00
"anoption" => $aQueueItem["anoption"],
"kind" => $aQueueItem["kind"],
"printer" => $aQueueItem["printer"],
"extras" => $extras
);
2020-11-19 23:12:05 +01:00
if ($groupworkitems == 1) {
2020-11-19 22:53:50 +01:00
$this->grouping($jobs, $theEntry);
} else {
$jobs[] = $theEntry;
}
}
}
2020-11-19 23:10:37 +01:00
if ($groupworkitems == 1) {
2020-11-19 23:12:05 +01:00
$jobidsOfThisJob = array();
2020-11-19 22:53:50 +01:00
foreach($jobs as &$aJob) {
2020-11-19 23:10:35 +01:00
$aJob["singleprod"] = $aJob["longname"];
2020-11-19 22:53:50 +01:00
$cnt = $aJob["count"];
2020-11-19 23:00:55 +01:00
$aJob["longname"] = $cnt . "x " . $aJob["longname"];
2020-11-19 23:12:05 +01:00
$aJob["allqueueids"] = $aJob["queueids"];
2020-11-19 22:47:44 +01:00
}
}
return $jobs;
}
2020-11-19 22:53:50 +01:00
private function grouping(&$collection,$entry) {
$extrasTxt = join(",",$entry["extras"]);
$found = false;
foreach($collection as &$anEntry) {
2020-11-19 23:02:27 +01:00
if (($anEntry["longname"] == $entry["longname"]) && ($anEntry["price"] == $entry["price"]) && ($anEntry["togo"] == $entry["togo"]) && ($anEntry["anoption"] == $entry["anoption"]) && (join(",",$anEntry["extras"]) == $extrasTxt)) {
2020-11-19 22:53:50 +01:00
$found = true;
$anEntry["count"] = $anEntry["count"] + 1;
2020-11-19 23:12:05 +01:00
$anEntry["queueids"][] = $entry["id"];
2020-11-19 22:53:50 +01:00
}
}
if (!$found) {
$collection[] = array("id" => $entry["id"],
"productid" => $entry["productid"],
"longname" => $entry["longname"],
2020-11-19 23:10:35 +01:00
"singleprod" => $entry["singleprod"],
2020-11-19 22:53:50 +01:00
"printer" => $entry["printer"],
"anoption" => $entry["anoption"],
2020-11-19 23:02:21 +01:00
"price" => $entry["price"],
2020-11-19 23:02:27 +01:00
"togo" => $entry["togo"],
2020-11-19 22:53:50 +01:00
"kind" => $entry["kind"],
"extras" => $entry["extras"],
2020-11-19 23:12:05 +01:00
"count" => 1,
"queueids" => array($entry["id"])
2020-11-19 22:53:50 +01:00
);
}
}
2020-11-19 23:03:26 +01:00
private function filterKindQueueIds($pdo,$idArr,$kind) {
$retArr = array();
if (is_null($idArr) || (count($idArr) == 0)) {
return $retArr;
}
2020-11-19 23:15:07 +01:00
$sql = "SELECT Q.id as id,kind from %prodtype% T,%products% P,%queue% Q where Q.id=? AND productid=P.id AND category=T.id";
2020-11-19 23:03:26 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
foreach($idArr as $id) {
$stmt->execute(array($id));
$row = $stmt->fetchObject();
if ($row->kind == $kind) {
$retArr[] = $id;
}
}
return $retArr;
}
2020-11-19 23:14:48 +01:00
private function doWorkPrint($pdo,$theTableid,$insertedQueueIds,$username,$userid,$payPrintType,$lang,$declareReadyDelivered = true) {
2020-11-19 23:03:26 +01:00
$oneProdForEachWorkRecf = CommonUtils::getConfigValue($pdo, 'oneprodworkrecf', 0);
$oneProdForEachWorkRecd = CommonUtils::getConfigValue($pdo, 'oneprodworkrecd', 0);
$foodIds = $this->filterKindQueueIds($pdo, $insertedQueueIds, 0);
$drinkIds = $this->filterKindQueueIds($pdo, $insertedQueueIds, 1);
if (($payPrintType != "s") || ($oneProdForEachWorkRecf == 0)) {
2020-11-19 23:14:48 +01:00
$this->doWorkPrintCore($pdo,$theTableid,$foodIds,$username,$userid,$payPrintType,$lang,$declareReadyDelivered);
2020-11-19 23:03:26 +01:00
}
if (($payPrintType != "s") || ($oneProdForEachWorkRecd == 0)) {
2020-11-19 23:14:48 +01:00
$this->doWorkPrintCore($pdo,$theTableid,$drinkIds,$username,$userid,$payPrintType,$lang,$declareReadyDelivered);
2020-11-19 23:03:26 +01:00
}
if ($payPrintType != 's') {
return;
}
if ($oneProdForEachWorkRecf == 1) {
foreach($foodIds as $aQueueId) {
2020-11-19 23:14:48 +01:00
$this->doWorkPrintCore($pdo,$theTableid,array($aQueueId),$username,$userid,$payPrintType,$lang,$declareReadyDelivered);
2020-11-19 23:03:26 +01:00
}
2020-11-19 23:00:05 +01:00
}
2020-11-19 23:03:26 +01:00
if ($oneProdForEachWorkRecd == 1) {
foreach($drinkIds as $aQueueId) {
2020-11-19 23:14:48 +01:00
$this->doWorkPrintCore($pdo,$theTableid,array($aQueueId),$username,$userid,$payPrintType,$lang,$declareReadyDelivered);
2020-11-19 23:00:05 +01:00
}
}
}
2020-11-19 22:53:50 +01:00
2020-11-19 23:14:48 +01:00
private function doWorkPrintCore($pdo,$theTableid,$insertedQueueIds,$username,$userid,$payPrintType,$lang,$declareReadyDelivered = true) {
2020-11-19 22:56:12 +01:00
2020-11-19 22:47:44 +01:00
$foodJobsPrinter1 = $this->getJobsToPrint($pdo, 0, 1, $insertedQueueIds);
$foodJobsPrinter2 = $this->getJobsToPrint($pdo, 0, 2, $insertedQueueIds);
2020-11-19 23:12:05 +01:00
$foodJobsPrinter3 = $this->getJobsToPrint($pdo, 0, 3, $insertedQueueIds);
$foodJobsPrinter4 = $this->getJobsToPrint($pdo, 0, 4, $insertedQueueIds);
2020-11-19 22:47:44 +01:00
$drinkJobsPrinter1 = $this->getJobsToPrint($pdo, 1, 1, $insertedQueueIds);
$drinkJobsPrinter2 = $this->getJobsToPrint($pdo, 1, 2, $insertedQueueIds);
2020-11-19 23:12:05 +01:00
$drinkJobsPrinter3 = $this->getJobsToPrint($pdo, 1, 3, $insertedQueueIds);
$drinkJobsPrinter4 = $this->getJobsToPrint($pdo, 1, 4, $insertedQueueIds);
2020-11-19 22:47:44 +01:00
2020-11-19 23:00:05 +01:00
if ($payPrintType == "s") {
2020-11-19 23:14:48 +01:00
$this->createAWorkReceiptAndQueueWorkPrint($pdo,$foodJobsPrinter1,$theTableid,0,1,$username,$userid,$lang);
$this->createAWorkReceiptAndQueueWorkPrint($pdo,$foodJobsPrinter2,$theTableid,0,2,$username,$userid,$lang);
$this->createAWorkReceiptAndQueueWorkPrint($pdo,$foodJobsPrinter3,$theTableid,0,3,$username,$userid,$lang);
$this->createAWorkReceiptAndQueueWorkPrint($pdo,$foodJobsPrinter4,$theTableid,0,4,$username,$userid,$lang);
$this->createAWorkReceiptAndQueueWorkPrint($pdo,$drinkJobsPrinter1,$theTableid,1,1,$username,$userid,$lang);
$this->createAWorkReceiptAndQueueWorkPrint($pdo,$drinkJobsPrinter2,$theTableid,1,2,$username,$userid,$lang);
$this->createAWorkReceiptAndQueueWorkPrint($pdo,$drinkJobsPrinter3,$theTableid,1,3,$username,$userid,$lang);
$this->createAWorkReceiptAndQueueWorkPrint($pdo,$drinkJobsPrinter4,$theTableid,1,4,$username,$userid,$lang);
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:00:05 +01:00
if ($declareReadyDelivered) {
2020-11-19 23:00:55 +01:00
$printAndQueueJobs = CommonUtils::getConfigValue($pdo, "printandqueuejobs", 0);
if ($printAndQueueJobs == 0) {
$this->declareReadyAndDelivered($pdo, $insertedQueueIds);
}
2020-11-19 23:00:05 +01:00
}
2020-11-19 22:56:12 +01:00
2020-11-19 22:47:44 +01:00
$result = array_merge($foodJobsPrinter1,$foodJobsPrinter2,$drinkJobsPrinter1,$drinkJobsPrinter2);
2020-11-19 23:12:05 +01:00
return $result;
2020-11-19 22:47:44 +01:00
}
2020-11-19 22:56:12 +01:00
private function declareReadyAndDelivered($pdo,$queueids) {
date_default_timezone_set(DbUtils::getTimeZone());
$now = date('Y-m-d H:i:s');
$germanTime = date('H:i');
$sql = "UPDATE %queue% SET workprinted='1',readytime=?,delivertime=? WHERE id=?";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
foreach($queueids as $anId) {
$stmt->execute(array($now,$now,$anId));
}
}
2020-11-19 23:14:48 +01:00
private function createAWorkReceiptAndQueueWorkPrint($pdo,$jobs,$theTableid,$kind,$printer,$user,$userid,$lang) {
2020-11-19 22:47:44 +01:00
if (count($jobs) < 1) {
return;
}
date_default_timezone_set(DbUtils::getTimeZone());
$germanTime = date('H:i');
$resultarray = array();
foreach($jobs as $aJob) {
$queueId = $aJob["id"];
2020-11-19 23:14:48 +01:00
$extras = $this->getExtrasOfQueueItemInTicketFormat($pdo,$queueId);
2020-11-19 22:47:44 +01:00
2020-11-19 23:02:27 +01:00
$togo = "";
if ($aJob['togo'] == 1) {
$togo = "To-Go";
}
2020-11-19 23:11:52 +01:00
2020-11-19 22:47:44 +01:00
$arr = array("id" => $queueId,
"longname" => $aJob['longname'],
2020-11-19 23:10:35 +01:00
"singleprod" => $aJob['singleprod'],
2020-11-19 23:02:21 +01:00
"price" => $aJob['price'],
2020-11-19 23:02:27 +01:00
"togo" => $togo,
2020-11-19 22:47:44 +01:00
"option" => $aJob['anoption'],
"extras" => $extras,
"ordertime" => $germanTime,
"kind" => $kind,
2020-11-19 23:11:52 +01:00
"printer" => $printer
2020-11-19 22:47:44 +01:00
);
2020-11-19 23:11:52 +01:00
if (isset($aJob["allqueueids"])) {
$arr["allqueueids"] = $aJob["allqueueids"];
}
2020-11-19 22:47:44 +01:00
$resultarray[] = $arr;
}
if (is_null($theTableid) || ($theTableid == 0)) {
$takeAwayStr = array("Zum Mitnehmen","Take away","Para llevar");
$tablename = $takeAwayStr[$lang];
} else {
2020-11-19 22:55:09 +01:00
$sql = "SELECT tableno,%room%.abbreviation FROM %resttables%,%room% WHERE %resttables%.id=? AND %resttables%.roomid=%room%.id";
2020-11-19 23:00:55 +01:00
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($theTableid));
2020-11-19 22:47:44 +01:00
2020-11-19 23:10:06 +01:00
if (is_null($row->abbreviation) || ($row->abbreviation == '')) {
2020-11-19 22:55:09 +01:00
$tablename = $row->tableno;
} else {
$tablename = $row->abbreviation . "-" . $row->tableno;
}
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:11:54 +01:00
$isTogo = false;
if (is_null($theTableid)) {
$isTogo = true;
}
2020-11-19 23:14:48 +01:00
PrintQueue::queueWorkPrintJob($pdo, $tablename, $germanTime, $resultarray, $kind, $printer, $user,$userid,$isTogo);
2020-11-19 22:47:44 +01:00
}
private function getJsonLastMadeItems($kind) {
2020-11-19 22:59:47 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
2020-11-19 23:12:35 +01:00
$unit = CommonUtils::caseOfSqlUnitSelection($pdo);
2020-11-19 22:59:47 +01:00
if ($this->areBillExisting($pdo)) {
2020-11-19 23:15:07 +01:00
$sql = "SELECT DISTINCT Q.id as id,tablenr,Q.unit,CONCAT($unit,Q.productname) as longname,anoption,tableno,readytime,%products%.id as prodid FROM %queue% Q,%products%,%prodtype%,%resttables%,%bill% ";
2020-11-19 22:47:44 +01:00
} else {
2020-11-19 23:15:07 +01:00
$sql = "SELECT DISTINCT Q.id as id,tablenr,Q.unit,CONCAT($unit,Q.productname) as longname,anoption,tableno,readytime,%products%.id as prodid FROM %queue% Q,%products%,%prodtype%,%resttables% ";
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:00:46 +01:00
$sql .= "WHERE (readytime IS NOT NULL AND ";
$sql .= "delivertime IS NULL AND ";
2020-11-19 23:12:35 +01:00
$sql .= "Q.toremove = '0' AND ";
$sql .= "Q.productid=%products%.id AND ";
$sql .= "Q.tablenr = %resttables%.id AND ";
2020-11-19 22:47:44 +01:00
$sql .= "%products%.category=%prodtype%.id AND ";
$sql .= "%prodtype%.kind=? AND ";
2020-11-19 23:12:35 +01:00
$sql .= "Q.isclosed is null AND ";
$sql .= "Q.workprinted='0') ";
2020-11-19 22:47:44 +01:00
if ($this->areBillExisting($pdo)) {
2020-11-19 23:12:35 +01:00
$sql .= "AND (Q.billid is null OR (";
$sql .= "Q.billid=%bill%.id AND %bill%.closingid is null)) ";
2020-11-19 22:59:47 +01:00
}
2020-11-19 22:47:44 +01:00
$sql .= "ORDER BY readytime DESC LIMIT 10;";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
2020-11-19 22:59:47 +01:00
$stmt->execute(array($kind));
2020-11-19 22:47:44 +01:00
$result1 = $stmt->fetchAll();
if ($this->areBillExisting($pdo)) {
2020-11-19 23:12:35 +01:00
$sql = "SELECT DISTINCT Q.id as id,0 as tablenr,CONCAT($unit,Q.productname) as longname,anoption,'-' as tableno,readytime,%products%.id as prodid FROM %queue% Q,%products%,%prodtype%,%bill% ";
2020-11-19 22:47:44 +01:00
} else {
2020-11-19 23:12:35 +01:00
$sql = "SELECT DISTINCT Q.id as id,0 as tablenr,CONCAT($unit,Q.productname) as longname,anoption,'-' as tableno,readytime,%products%.id as prodid FROM %queue% Q,%products%,%prodtype% ";
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:00:46 +01:00
$sql .= "WHERE (readytime IS NOT NULL AND ";
$sql .= "delivertime IS NULL AND ";
2020-11-19 23:12:35 +01:00
$sql .= "Q.toremove = '0' AND ";
$sql .= "Q.productid=%products%.id AND ";
$sql .= "Q.tablenr is null AND ";
2020-11-19 22:47:44 +01:00
$sql .= "%products%.category=%prodtype%.id AND ";
$sql .= "%prodtype%.kind=? AND ";
2020-11-19 23:12:35 +01:00
$sql .= "Q.isclosed is null AND ";
$sql .= "Q.workprinted='0') ";
2020-11-19 22:47:44 +01:00
if ($this->areBillExisting($pdo)) {
2020-11-19 23:12:35 +01:00
$sql .= "AND (Q.billid is null OR (";
$sql .= "Q.billid=%bill%.id AND %bill%.closingid is null)) ";
2020-11-19 22:47:44 +01:00
}
$sql .= "ORDER BY readytime DESC LIMIT 10;";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($kind));
$result2 = $stmt->fetchAll();
$result = array_merge($result1,$result2);
2020-11-19 22:59:47 +01:00
2020-11-19 22:47:44 +01:00
$resultarray = array();
foreach($result as $zeile) {
$extras = $this->getExtrasOfQueueItem($pdo,$zeile['id']);
$productid = $zeile['prodid'];
2020-11-19 22:54:51 +01:00
$useConditions = $this->getUseKitchenAndSupplyForProd($pdo,$productid);
2020-11-19 22:47:44 +01:00
if ($useConditions["usekitchen"] == 1) {
2020-11-19 22:59:47 +01:00
$arr = array("id" => $zeile['id'],
"tablename" => $zeile['tableno'],
"longname" => $zeile['longname'],
2020-11-19 22:47:44 +01:00
"option" => $zeile['anoption'],
2020-11-19 22:59:47 +01:00
"extras" => $extras,
"readytime" => $zeile['readytime']
);
2020-11-19 22:47:44 +01:00
$resultarray[] = $arr;
2020-11-19 22:59:47 +01:00
}
2020-11-19 22:47:44 +01:00
}
$resultarray = $this->appendProdsForBarKitchenAndAutoDelivery($pdo,$kind, $resultarray);
echo json_encode($resultarray);
}
2020-11-19 22:59:47 +01:00
private function appendProdsForBarKitchenAndAutoDelivery($pdo,$kind,$resultarray) {
2020-11-19 23:12:35 +01:00
$unit = CommonUtils::caseOfSqlUnitSelection($pdo);
2020-11-19 23:15:07 +01:00
$sql = "SELECT DISTINCT Q.id as id,tableno as tablename,Q.unit,CONCAT($unit,Q.productname) as longname,delivertime,anoption,p.id as prodid ";
2020-11-19 23:12:35 +01:00
$sql .= "FROM %queue% Q ";
$sql .= "LEFT JOIN %bill% b ON Q.billid=b.id ";
2020-11-19 23:10:13 +01:00
2020-11-19 23:12:35 +01:00
$sql .= "INNER JOIN %products% p ON Q.productid=p.id ";
2020-11-19 23:10:13 +01:00
$sql .= "INNER JOIN %prodtype% t ON p.category=t.id AND t.kind=? AND t.usesupplydesk='0' AND t.usekitchen='1' ";
2020-11-19 23:12:35 +01:00
$sql .= "LEFT JOIN %resttables% r ON Q.tablenr=r.id ";
$sql .= "WHERE Q.workprinted='0' AND toremove <> '1' AND Q.readytime IS NOT NULL AND Q.toremove='0' ";
$sql .= " AND (Q.billid is null OR (Q.billid=b.id AND b.closingid is null)) ";
$sql .= "ORDER BY Q.delivertime DESC LIMIT 50";
2020-11-19 23:10:15 +01:00
2020-11-19 22:54:51 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($kind));
$result = $stmt->fetchAll();
2020-11-19 22:59:47 +01:00
foreach ($result as $zeile) {
$extras = $this->getExtrasOfQueueItem($pdo,$zeile['id']);
$deliveredProd = array(
2020-11-19 22:47:44 +01:00
"id" => $zeile['id'],
2020-11-19 23:10:15 +01:00
"tablename" => $zeile['tablename'],
2020-11-19 22:59:47 +01:00
"longname" => $zeile['longname'],
2020-11-19 22:47:44 +01:00
"option" => $zeile['anoption'],
2020-11-19 22:59:47 +01:00
"extras" => $extras,
"readytime" => $zeile['delivertime']
);
$resultarray[] = $deliveredProd;
}
return($resultarray);
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:00:05 +01:00
function getTableIdOfQueue($pdo,$queueid) {
$sql = "SELECT tablenr as tableid FROM %queue% WHERE id=?";
2020-11-19 23:00:55 +01:00
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($queueid));
2020-11-19 23:00:05 +01:00
return $row->tableid;
}
2020-11-19 22:47:44 +01:00
/*
* Kitchen can delare a product as being cooked
*/
function declareProductBeCookingOrCooked($queueid,$action) {
if (is_numeric($queueid)) {
2020-11-19 23:12:00 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
2020-11-19 22:47:44 +01:00
$pdo->beginTransaction();
$sql = "SELECT cooking,productid FROM %queue% WHERE id=?";
2020-11-19 23:00:55 +01:00
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($queueid));
2020-11-19 22:59:47 +01:00
if ($row != null) {
2020-11-19 22:47:44 +01:00
$cooking = $row->cooking;
2020-11-19 22:59:47 +01:00
$productid = $row->productid;
2020-11-19 22:47:44 +01:00
if ($action == 'r') {
2020-11-19 23:12:46 +01:00
self::reallyDeclareAsCooked($pdo,$queueid);
2020-11-19 23:12:00 +01:00
$useConditions = $this->getUseKitchenAndSupplyForProd($pdo,$productid);
if ($useConditions["usesupply"] == 0) {
2020-11-19 23:12:46 +01:00
self::declareProductBeDeliveredWithGivenPdo($pdo,$queueid);
2020-11-19 22:47:44 +01:00
} else {
2020-11-19 23:12:00 +01:00
self::setNewProductsToServe($pdo, 1);
}
$payprinttype = CommonUtils::getConfigValue($pdo, 'payprinttype', "l");
$digiprintwork = CommonUtils::getConfigValue($pdo, 'digiprintwork', 1);
if (($payprinttype === 's') && ($digiprintwork == 1)) {
$theTableid = $this->getTableIdOfQueue($pdo, $queueid);
if (is_null($theTableid)) {
$theTableid = 0;
}
2020-11-19 23:14:48 +01:00
$this->doWorkPrint($pdo,$theTableid,array($queueid),$_SESSION['currentuser'],$_SESSION['userid'],$payprinttype, $_SESSION['language'],false);
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:12:00 +01:00
$pdo->commit();
echo json_encode(array("status" => "OK"));
2020-11-19 22:47:44 +01:00
} else if ($action == 'c') {
if (!is_null($cooking)) {
2020-11-19 22:59:47 +01:00
$pdo->rollBack();
2020-11-19 22:47:44 +01:00
echo json_encode(array("status" => "ERROR", "code" => ERROR_DB_PAR_ACCESS, "msg" => ERROR_DB_PAR_ACCESS_MSG));
} else {
$userid = $this->getUserId();
$updSql = "UPDATE %queue% SET cooking=? WHERE id=?";
2020-11-19 22:59:47 +01:00
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($updSql));
2020-11-19 22:47:44 +01:00
$stmt->execute(array($userid,$queueid));
$pdo->commit();
echo json_encode(array("status" => "OK"));
}
2020-11-19 22:59:47 +01:00
}
2020-11-19 22:47:44 +01:00
} else {
$pdo->rollBack();
}
} else {
echo json_encode(array("status" => "ERROR", "code" => ERROR_GENERAL_ID_TYPE, "msg" => ERROR_GENERAL_ID_TYPE_MSG));
2020-11-19 22:59:47 +01:00
}
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:12:46 +01:00
private static function reallyDeclareAsCooked($pdo,$queueid) {
2020-11-19 22:59:47 +01:00
date_default_timezone_set(DbUtils::getTimeZone());
$readytime = date('Y-m-d H:i:s');
2020-11-19 22:47:44 +01:00
$insertSql = "UPDATE %queue% SET readytime=? WHERE id=?";
2020-11-19 23:12:46 +01:00
CommonUtils::execSql($pdo, $insertSql, array($readytime,$queueid));
2020-11-19 22:47:44 +01:00
}
/*
* Product is not cooked (undo of kitchen)
*/
function declareProductNotBeCooked($queueid) {
if (is_numeric($queueid)) {
2020-11-19 22:59:47 +01:00
$pdo = $this->dbutils->openDbAndReturnPdo();
2020-11-19 22:47:44 +01:00
$pdo->beginTransaction();
2020-11-19 23:00:46 +01:00
$sql = "SELECT id FROM %queue% WHERE id=? AND readytime IS NOT NULL";
2020-11-19 23:00:55 +01:00
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($queueid));
2020-11-19 22:59:47 +01:00
if ($row != null) {
2020-11-19 22:47:44 +01:00
$foundid = $row->id;
if ($foundid == $queueid) {
2020-11-19 23:00:46 +01:00
$sql = "UPDATE %queue% SET readytime=?, delivertime=?, cooking=NULL WHERE id=?";
2020-11-19 22:47:44 +01:00
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
2020-11-19 23:00:46 +01:00
$stmt->execute(array(null,null,$queueid));
2020-11-19 22:47:44 +01:00
$pdo->commit();
echo json_encode(array("status" => "OK"));
} else {
echo json_encode(array("status" => "ERROR", "code" => ERROR_DB_PAR_ACCESS, "msg" => ERROR_DB_PAR_ACCESS_MSG));
$pdo->rollBack();
}
} else {
$pdo->rollBack();
echo json_encode(array("status" => "ERROR", "code" => ERROR_DB_PAR_ACCESS, "msg" => ERROR_DB_PAR_ACCESS_MSG));
2020-11-19 22:59:47 +01:00
}
2020-11-19 22:47:44 +01:00
} else {
echo json_encode(array("status" => "ERROR", "code" => ERROR_GENERAL_ID_TYPE, "msg" => ERROR_GENERAL_ID_TYPE_MSG));
}
}
2020-11-19 22:54:51 +01:00
private function findCategoryOfProd($pdo,$prodid) {
$sql = "SELECT category FROM %products% WHERE id=?";
2020-11-19 23:00:55 +01:00
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($prodid));
2020-11-19 22:54:51 +01:00
return $row->category;
2020-11-19 22:47:44 +01:00
}
2020-11-19 22:54:51 +01:00
private function getUseKitchenAndSupplyForProdInCat($pdo,$catid) {
$sql = "SELECT usekitchen, usesupplydesk FROM %prodtype% WHERE id=?";
2020-11-19 23:00:55 +01:00
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($catid));
2020-11-19 22:54:51 +01:00
return array("usekitchen" => $row->usekitchen, "usesupply" => $row->usesupplydesk);
2020-11-19 22:47:44 +01:00
}
2020-11-19 22:54:51 +01:00
private function getUseKitchenAndSupplyForProd($pdo,$prodid) {
$catid = $this->findCategoryOfProd($pdo,$prodid);
return $this->getUseKitchenAndSupplyForProdInCat($pdo,$catid);
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:12:46 +01:00
private static function getUseKitchenAndSupplyForProdWithPdo($pdo,$prodid) {
2020-11-19 22:59:47 +01:00
$sql = "SELECT usekitchen, usesupplydesk FROM %prodtype%,%products% WHERE %products%.category=%prodtype%.id AND %products%.id=?";
2020-11-19 23:00:55 +01:00
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($prodid));
2020-11-19 22:47:44 +01:00
if ($row != null) {
return array("usekitchen" => $row->usekitchen, "usesupply" => $row->usesupplydesk);
} else {
return array("usekitchen" => "1", "usesupply" => "1");
}
}
2020-11-19 23:12:48 +01:00
private function getAllOrders() {
2020-11-19 23:12:30 +01:00
if (!($this->userrights->hasCurrentUserRight('right_statistics')) ) {
echo json_encode(array("status" => "ERROR","msg" => "Benutzerrechte nicht ausreichend"));
return false;
}
$pdo = DbUtils::openDbAndReturnPdoStatic();
2020-11-19 23:14:48 +01:00
$sql = "SELECT count(Q.id) as countid,productid,price,productname,ordertime,username,COALESCE(toremove,0) as toremove,IF(billid is null,0,1) as ispaid,COALESCE(tableno,'To-Go') as tablename ";
2020-11-19 23:12:48 +01:00
$sql .= " FROM %queue% Q LEFT JOIN %resttables% R ON Q.tablenr=R.id LEFT JOIN %user% U ON Q.orderuser=U.id ";
$sql .= " WHERE isclosed is null ";
2020-11-19 23:12:30 +01:00
$sql .= " group by ordertime,productname,productid,price,toremove,ispaid,tablename,username ORDER BY ordertime,tablename,ispaid";
$allorders = CommonUtils::fetchSqlAll($pdo, $sql);
2020-11-19 23:14:48 +01:00
$sql = "SELECT COALESCE(SUM(price),'0.00') as sumprice FROM %queue% Q WHERE isclosed is null AND toremove=?";
2020-11-19 23:12:30 +01:00
$sumAllRes = CommonUtils::fetchSqlAll($pdo, $sql,array(0));
$sumAll = $sumAllRes[0]["sumprice"];
2020-11-19 23:14:48 +01:00
$sql = "SELECT COALESCE(SUM(price),'0.00') as sumprice FROM %queue% Q WHERE isclosed is null AND toremove=? AND billid is null";
2020-11-19 23:12:30 +01:00
$sumUnpaidRes = CommonUtils::fetchSqlAll($pdo, $sql,array(0));
$sumUnpaid = $sumUnpaidRes[0]["sumprice"];
2020-11-19 23:14:48 +01:00
$sql = "SELECT COALESCE(SUM(price),'0.00') as sumprice FROM %queue% Q WHERE isclosed is null AND toremove=? AND billid is not null";
2020-11-19 23:12:30 +01:00
$sumPaidRes = CommonUtils::fetchSqlAll($pdo, $sql,array(0));
$sumPaid = $sumPaidRes[0]["sumprice"];
2020-11-19 23:14:48 +01:00
$sql = "SELECT COALESCE(SUM(price),'0.00') as sumprice FROM %queue% Q WHERE isclosed is null AND toremove=? AND billid is null";
2020-11-19 23:12:30 +01:00
$sumCancelledRes = CommonUtils::fetchSqlAll($pdo, $sql,array(1));
$sumCancelled = $sumCancelledRes[0]["sumprice"];
$result = array("list" => $allorders,
"sumall" => $sumAll,
"sumunpaid" => $sumUnpaid,
"sumpaid" => $sumPaid,
"sumcancelled" => $sumCancelled);
echo json_encode(array("status" => "OK","msg" => $result));
}
2020-11-19 23:12:13 +01:00
function getAllTableRecords() {
if (!($this->userrights->hasCurrentUserRight('right_statistics')) ) {
2020-11-19 23:10:26 +01:00
echo json_encode(array("status" => "ERROR","msg" => "Benutzerrechte nicht ausreichend"));
return false;
}
$pdo = DbUtils::openDbAndReturnPdoStatic();
2020-11-19 23:12:13 +01:00
$sql = "SELECT id,roomname FROM %room% WHERE removed is null";
$allrooms = CommonUtils::fetchSqlAll($pdo, $sql);
$roomsOut = array();
$sql = "SELECT id,tableno FROM %resttables% WHERE roomid=? AND removed is null";
foreach($allrooms as $aRoom) {
$tablesOfRoom = CommonUtils::fetchSqlAll($pdo, $sql, array($aRoom["id"]));
$tablesOut = array();
foreach($tablesOfRoom as $aTable) {
$recordsOfThisTable = $this->getRecordsCore($pdo, $aTable["id"]);
$tablesOut[] = array("tablename" => $aTable["tableno"],"records" => $recordsOfThisTable);
}
$roomsOut[] = array("roomname" => $aRoom["roomname"],"tables" => $tablesOut);
}
echo json_encode(array("status" => "OK","msg" => $roomsOut));
}
function getRecords($pdo,$tableid) {
if (!($this->userrights->hasCurrentUserRight('right_waiter')) && !($this->userrights->hasCurrentUserRight('right_paydesk'))) {
return array("status" => "ERROR","msg" => "Benutzerrechte nicht ausreichend");
}
$out = $this->getRecordsCore($pdo, $tableid);
echo json_encode($out);
}
function getRecordsCore($pdo,$tableid) {
2020-11-19 23:10:26 +01:00
if ($tableid != 0) {
$sql = "SELECT id,TIME(date) as time,(IF(userid is null,'-',(SELECT username FROM %user% WHERE %user%.id=userid))) as username,action,tableid FROM %records% WHERE tableid=? ORDER BY time DESC";
$entries = CommonUtils::fetchSqlAll($pdo, $sql, array($tableid));
} else {
$sql = "SELECT id,TIME(date) as time,(IF(userid is null,'-',(SELECT username FROM %user% WHERE %user%.id=userid))) as username,action,tableid FROM %records% WHERE tableid is null ORDER BY time DESC";
$entries = CommonUtils::fetchSqlAll($pdo, $sql, null);
}
$records = array();
foreach($entries as $anEntry) {
$sql = "SELECT queueid FROM %recordsqueue% WHERE recordid=?";
$queueids = CommonUtils::fetchSqlAll($pdo, $sql, array($anEntry["id"]));
$prods = array();
foreach($queueids as $queueid) {
$sql = "SELECT productid,longname FROM %products%,%queue% WHERE %queue%.id=? AND %queue%.productid=%products%.id";
$prodInfo = CommonUtils::fetchSqlAll($pdo, $sql, array($queueid["queueid"]));
if (count($prodInfo) == 0) {
break;
}
2020-11-19 23:12:39 +01:00
$sql = "SELECT extraid,CONCAT(amount,'x ',name) as name FROM %queueextras% WHERE queueid=?";
2020-11-19 23:10:26 +01:00
$extras = CommonUtils::fetchSqlAll($pdo, $sql, array($queueid["queueid"]));
$extrasArr = array();
foreach($extras as $e) {
$extrasArr[] = $e["name"];
}
$extrasStr = implode(',', $extrasArr);
$prods[] = array("name" => $prodInfo[0]["longname"],"extras" => $extrasStr);
}
$records[] = array("id" => $anEntry["id"],"time" => $anEntry["time"],"username" => $anEntry["username"],"action" => $anEntry["action"],"prods" => $prods);
}
2020-11-19 23:12:13 +01:00
return array("status" => "OK","msg" => $records);
2020-11-19 23:10:26 +01:00
}
2020-11-19 23:12:18 +01:00
function addAndCashProdByRestCall($pdo,$data) {
if (!isset($data["remotecode"])) {
echo json_encode(array("status" => "ERROR","msg" => "Remote code not sent in POST data"));
return;
}
if (!isset($data["price"])) {
echo json_encode(array("status" => "ERROR","msg" => "Price not sent in POST data"));
return;
}
if (!isset($data["prodid"])) {
echo json_encode(array("status" => "ERROR","msg" => "Product id not sent in POST data"));
return;
}
if (!isset($data["userid"])) {
echo json_encode(array("status" => "ERROR","msg" => "User id not sent in POST data"));
return;
}
$printer = -1;
if (isset($data["printer"])) {
$printer = $data["printer"];
}
$remotecode = $data["remotecode"];
$prodid = $data["prodid"];
$price = $data["price"];
$userid = $data["userid"];
$remote = CommonUtils::getConfigValue($pdo, 'remoteaccesscode', '');
if (is_null($remote) || ($remote == '')) {
echo json_encode(array("status" => "ERROR","msg" => "No remote access code set in configuration. No booking allowed."));
return;
}
if (md5($remotecode) != $remote) {
echo json_encode(array("status" => "ERROR","msg" => "Wrong access code given!"));
return;
}
if (!is_numeric($price)) {
echo json_encode(array("status" => "ERROR","msg" => "Price not numerical!"));
return;
}
$pdo->beginTransaction();
date_default_timezone_set(DbUtils::getTimeZone());
$ordertime = date('Y-m-d H:i:s');
$sql = "INSERT INTO %records% (date,userid,tableid,action) VALUES(?,?,?,?)";
CommonUtils::execSql($pdo, $sql, array($ordertime,$userid,null,T_ORDER));
$recordid = $pdo->lastInsertId();
$sql = "SELECT longname,amount,tax FROM %products% where id=?";
$prodInfo = CommonUtils::fetchSqlAll($pdo, $sql, array($prodid));
if (count($prodInfo) != 1) {
echo json_encode(array("status" => "ERROR","msg" => "Product not identifyable by id"));
return;
};
$tax = $prodInfo[0]["tax"];
if (is_null($tax)) {
$tax = CommonUtils::getConfigValue($pdo, 'togotax', "7.0");
}
$productname = $prodInfo[0]["longname"];
$amount = $prodInfo[0]["amount"];
$insertSql = "INSERT INTO `%queue%` (
2020-11-19 23:14:48 +01:00
`id` , `tablenr`,`productid`,`pricelevel`,`price`,`pricetype`,`tax`,`taxaustria`,`productname`,`ordertime`,`orderuser`,`anoption`,`pricechanged`,`togo`,`readytime`,`delivertime`,`paidtime`,`billid`,`toremove`,`cooking`,`workprinted`)
2020-11-19 23:12:18 +01:00
VALUES (
2020-11-19 23:14:48 +01:00
NULL , ?,?,?,?,?,?,?,?,?,?,?,?,?, null, null, NULL,NULL,'0',NULL,'0');";
2020-11-19 23:12:18 +01:00
try {
$stmt = $pdo->prepare(DbUtils::substTableAlias($insertSql));
2020-11-19 23:14:48 +01:00
$stmt->execute(array(null,$prodid,1,$price,0,$tax,null,$productname,$ordertime,$userid,'',1,0));
2020-11-19 23:12:18 +01:00
$queueid = $pdo->lastInsertId();
} catch (Exception $ex) {
echo json_encode(array("status" => "ERROR","msg" => "Error happened when queuing the product: " . $ex->getMessage()));
$pdo->rollBack();
return;
}
$sql = "INSERT INTO %recordsqueue% (recordid,queueid) VALUES(?,?)";
CommonUtils::execSql($pdo, $sql, array($recordid,$queueid));
if (!is_null($amount)) {
$amount = max(($amount -1),0);
$sql = "UPDATE %products% SET amount=? WHERE id=?";
CommonUtils::execSql($pdo, $sql, array($amount,$prodid));
if ($amount < 11) {
Tasks::createTaskForEmptyInventory($pdo, $prodid);
}
}
$pdo->commit();
2020-11-19 23:14:02 +01:00
$billid = $this->declarePaidCreateBillReturnBillId($pdo, $queueid, 0, 1, 1, 0, self::$INTERNAL_CALL_YES, '', '', '',$userid);
2020-11-19 23:12:18 +01:00
if ($printer >= 0) {
PrintQueue::internalQueueReceiptPrintjob($pdo, $billid, $printer);
}
echo json_encode(array("status" => "OK"));
}
2020-11-19 23:12:46 +01:00
function addProductListToQueue($theTableid,$prods,$doPrint,$payprinttype) {
date_default_timezone_set(DbUtils::getTimeZone());
$ordertime = date('Y-m-d H:i:s');
if(session_id() == '') {
session_start();
}
2020-11-19 23:13:59 +01:00
$quickcash = $_SESSION['quickcash'];
2020-11-19 23:12:46 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
2020-11-19 23:13:59 +01:00
$ret = $this->addProductListToQueueCore($pdo, $ordertime, $theTableid, $prods, $doPrint, $payprinttype,$_SESSION['userid'],$quickcash);
2020-11-19 23:12:46 +01:00
echo json_encode($ret);
}
2020-11-19 23:14:48 +01:00
2020-11-19 22:59:47 +01:00
/*
* Add a product list to the queue as if it was ordered by the waiter.
* The ordertime is set by the time that this method is invoked.
*
* If product shall not be run over kitchen or supplydesk this is
* managed here as well
2020-11-19 22:47:44 +01:00
*/
2020-11-19 23:13:59 +01:00
public function addProductListToQueueCore($pdo,$ordertime,$theTableid,$prods,$doPrint,$payprinttype,$userid,$quickcash) {
2020-11-19 23:00:55 +01:00
2020-11-19 22:47:44 +01:00
if (intval($theTableid) == 0) {
$theTableid = null; // togo room
}
2020-11-19 23:13:59 +01:00
2020-11-19 23:00:55 +01:00
$printAndQueueJobs = CommonUtils::getConfigValue($pdo, "printandqueuejobs", 0);
if ($printAndQueueJobs == 1) {
$doPrint = 1;
}
2020-11-19 23:13:59 +01:00
2020-11-19 22:47:44 +01:00
$pdo->beginTransaction();
2020-11-19 23:00:31 +01:00
$workflowconfig = CommonUtils::getExistingConfigValue($pdo, 'workflowconfig');
$austria = CommonUtils::getExistingConfigValue($pdo, 'austria');
2020-11-19 23:12:46 +01:00
$commUtils = new CommonUtils();
$currentPriceLevel = $commUtils->getCurrentPriceLevel($pdo);
2020-11-19 22:59:47 +01:00
$currentPriceLevelId = $currentPriceLevel["id"];
2020-11-19 22:47:44 +01:00
2020-11-19 23:13:59 +01:00
$insertedQueueIdsForPrint = array();
$insertedQueueIdsTotal = array();
2020-11-19 22:47:44 +01:00
2020-11-19 23:10:26 +01:00
$sql = "INSERT INTO %records% (date,userid,tableid,action) VALUES(?,?,?,?)";
2020-11-19 23:12:46 +01:00
CommonUtils::execSql($pdo, $sql, array($ordertime,$userid,$theTableid,T_ORDER));
2020-11-19 23:10:26 +01:00
$recordid = $pdo->lastInsertId();
2020-11-19 23:14:48 +01:00
$tseEntries = array();
$queueitemids = array();
2020-11-19 23:10:26 +01:00
2020-11-19 22:47:44 +01:00
for ($i=0;$i<count($prods);$i++) {
$aProd = $prods[$i];
$productid = intval($aProd["prodid"]);
$theOption = $aProd["option"];
$theChangedPrice = $aProd["changedPrice"];
$theChangedPrice = str_replace(',','.',$theChangedPrice);
2020-11-19 23:12:35 +01:00
$unitamount = $aProd["unitamount"];
2020-11-19 22:59:47 +01:00
2020-11-19 23:14:48 +01:00
$getPriceSql = "SELECT priceA,priceB,priceC,longname,tax as prodtaxkey,togotax as prodtogotaxkey,taxaustria,amount,COALESCE(unit,0) as unit FROM %products% where id=?";
2020-11-19 23:00:55 +01:00
$row = CommonUtils::getRowSqlObject($pdo, $getPriceSql, array($productid));
2020-11-19 22:59:47 +01:00
if ($row == null) {
echo "Fehler: Preise nicht vorhanden"; // error
return;
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:00:31 +01:00
if (!is_null($row->amount)) {
2020-11-19 23:14:48 +01:00
if (is_null($row->unit) || ($row->unit == 0)) {
$reduceAmount = 1;
} else {
$reduceAmount = round($unitamount);
}
$amount = max(($row->amount - $reduceAmount), 0);
$sql = "UPDATE %products% SET amount=? WHERE id=?";
CommonUtils::execSql($pdo, $sql, array($amount, $productid));
if ($amount < 11) {
Tasks::createTaskForEmptyInventory($pdo, $productid);
}
2020-11-19 23:00:31 +01:00
}
2020-11-19 22:59:47 +01:00
2020-11-19 22:47:44 +01:00
$productname = $row->longname;
2020-11-19 23:12:35 +01:00
$unit = intval($row->unit);
2020-11-19 23:14:48 +01:00
$price_for_level_A = $row->priceA;
$price_for_level_B = $row->priceB;
$price_for_level_C = $row->priceC;
$prodtaxkey = $row->prodtaxkey;
$prodtogotaxkey = $row->prodtogotaxkey;
$basePrice = $price_for_level_A;
if ($currentPriceLevelId == 2) {
$basePrice = $price_for_level_B;
} else if ($currentPriceLevelId == 3) {
$basePrice = $price_for_level_C;
}
$priceType = DbUtils::$PRICE_TYPE_BASE;
2020-11-19 22:59:47 +01:00
if (($theChangedPrice == "NO") || (!is_numeric($theChangedPrice))) {
2020-11-19 23:14:48 +01:00
$price = $basePrice;
2020-11-19 22:47:44 +01:00
} else {
2020-11-19 23:14:48 +01:00
if ($theChangedPrice < $basePrice) {
$priceType = DBUtils::$PRICE_TYPE_DICOUNT;
} else if ($theChangedPrice > $basePrice) {
$priceType = DbUtils::$PRICE_TYPE_EXTRA_AMOUNT;
}
2020-11-19 22:47:44 +01:00
$price = $theChangedPrice;
}
2020-11-19 22:55:20 +01:00
$togo = $aProd["togo"];
2020-11-19 23:14:48 +01:00
$taxkeytouse = $prodtaxkey;
if (is_null($theTableid) || ($togo == 1)) {
$taxkeytouse = $prodtogotaxkey;
2020-11-19 22:55:20 +01:00
}
2020-11-19 23:14:48 +01:00
$taxToUse = CommonUtils::getTaxFromKey($pdo,$taxkeytouse);
2020-11-19 23:00:31 +01:00
if ($austria == 1) {
$taxaustrianumber = $row->taxaustria;
$configItem = "";
switch($taxaustrianumber) {
case 1:
$configItem = "taxaustrianormal";
break;
case 2:
$configItem = "taxaustriaerm1";
break;
case 3:
$configItem = "taxaustriaerm2";
break;
case 4:
$configItem = "taxaustriaspecial";
break;
default:
$configItem = "taxaustrianormal";
}
} else {
$taxaustrianumber = null;
2020-11-19 22:47:44 +01:00
}
$extras = null;
if (array_key_exists("extras",$aProd)) {
$extras = $aProd["extras"];
}
2020-11-19 23:02:24 +01:00
if (($theChangedPrice == "NO") || (!is_numeric($theChangedPrice))) {
if (!is_null($extras) && ($extras != "")) {
for ($j=0;$j<count($extras);$j++) {
$anExtra = $extras[$j];
$extraid = $anExtra["id"];
$extraname = $anExtra["name"];
2020-11-19 23:12:39 +01:00
$extraamount = $anExtra["amount"];
2020-11-19 23:02:24 +01:00
$sql = "SELECT price FROM %extras% WHERE id=?";
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($extraid));
2020-11-19 23:12:39 +01:00
$price += floatval($row->price * $extraamount);
2020-11-19 23:02:24 +01:00
}
2020-11-19 22:47:44 +01:00
}
}
2020-11-19 22:59:47 +01:00
2020-11-19 23:14:48 +01:00
if (is_numeric($unit) && ($unit > 1) && ($unit < 8)) {
2020-11-19 23:12:35 +01:00
if (!is_numeric($unitamount)) {
$pdo->rollBack();
2020-11-19 23:12:46 +01:00
return(array("status" => "ERROR", "msg" => "Mengenangabe nicht numerisch"));
2020-11-19 23:12:35 +01:00
} else {
$price = $price * $unitamount;
}
}
2020-11-19 22:59:47 +01:00
if (is_null($theTableid) || (is_numeric($theTableid) && is_numeric($productid))) {
2020-11-19 23:12:46 +01:00
$useConditions = self::getUseKitchenAndSupplyForProdWithPdo($pdo,$productid);
2020-11-19 22:59:47 +01:00
2020-11-19 23:14:48 +01:00
if ($unit < 8) {
$ordertype = DbUtils::$ORDERTYPE_PRODUCT;
$voucherid = null;
} else if ($unit == 8) {
$ordertype = DbUtils::$ORDERTYPE_1ZweckKauf;
$voucherid = vouchers::createEinZweckVoucher($pdo, $productname, $userid);
if (is_null($voucherid)) {
$pdo->rollBack();
return(array("status" => "ERROR","msg" => "Gutschein konnte nicht angelegt werden"));
}
$productname = "$voucherid - $productname";
} else if ($unit == 9) {
$ordertype = DbUtils::$ORDERTYPE_1ZweckEinl;
$voucherid = $theOption;
$redeem = vouchers::redeemVoucher($pdo, $theOption, $userid);
if (!$redeem) {
$pdo->rollBack();
return(array("status" => "ERROR","msg" => "Gutschein mit Nummer " . $theOption . " kann nicht eingelöst werden."));
} else {
$taxkeytouse = 5;
$taxToUse = 0.00;
$productname = "$theOption - $productname";
}
}
2020-11-19 22:59:47 +01:00
$insertSql = "INSERT INTO `%queue%` (
2020-11-19 23:14:48 +01:00
`id` , `tablenr`,`productid`,`pricelevel`, `price`,`pricetype`,`unit`,`unitamount`, `ordertype`, `voucherid`,`tax`,`taxkey`,`taxaustria`,`productname`,`ordertime`,`orderuser`,`anoption`,`pricechanged`,`togo`,`readytime`,`delivertime`,`paidtime`,`billid`,`toremove`,`cooking`,`workprinted`)
2020-11-19 22:59:47 +01:00
VALUES (
2020-11-19 23:14:48 +01:00
NULL , ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, null, null, NULL,NULL,'0',NULL,'0')";
2020-11-19 23:12:46 +01:00
2020-11-19 23:14:48 +01:00
CommonUtils::execSql($pdo, $insertSql, array($theTableid,$productid,$currentPriceLevelId,$price,$priceType,$unit,$unitamount,$ordertype,$voucherid,$taxToUse,$taxkeytouse,$taxaustrianumber,$productname,$ordertime,$userid,$theOption,($theChangedPrice == "NO" ? 0 : 1),$togo));
2020-11-19 23:12:46 +01:00
2020-11-19 22:47:44 +01:00
$queueid = $pdo->lastInsertId();
2020-11-19 22:55:20 +01:00
2020-11-19 23:10:26 +01:00
$sql = "INSERT INTO %recordsqueue% (recordid,queueid) VALUES(?,?)";
CommonUtils::execSql($pdo, $sql, array($recordid,$queueid));
2020-11-19 23:14:48 +01:00
if ($unit == 0) {
$orderAmount = 1;
} else {
$amountAsDouble = doubleval($unitamount);
$orderAmount = strval($amountAsDouble);
}
$quotedProdname = self::quoteTseName($productname);
$tseEntries[] = $orderAmount . ";\"" . $quotedProdname . "\";" . $price;
$queueitemids[] = $queueid;
2020-11-19 22:47:44 +01:00
if (!is_null($extras) && ($extras != "")) {
for ($j=0;$j<count($extras);$j++) {
$anExtra = $extras[$j];
$extraid = $anExtra["id"];
$extraname = $anExtra["name"];
2020-11-19 23:12:39 +01:00
$extraamount = $anExtra["amount"];
$sql = "INSERT INTO %queueextras% (`queueid`,`extraid`,`amount`,`name`) VALUES(?,?,?,?)";
2020-11-19 23:12:46 +01:00
CommonUtils::execSql($pdo, $sql, array($queueid,$extraid,$extraamount,$extraname));
2020-11-19 22:59:47 +01:00
}
2020-11-19 22:47:44 +01:00
}
2020-11-19 22:55:30 +01:00
2020-11-19 23:13:59 +01:00
if (($workflowconfig == DbUtils::$WORKFLOW_WORK_WITH_SERVER) && ($doPrint == DbUtils::$WORKFLOW_DIGITAL_AND_WORK)) {
2020-11-19 23:12:46 +01:00
self::reallyDeclareAsCooked($pdo,$queueid);
self::declareProductBeDeliveredWithGivenPdo($pdo,$queueid);
2020-11-19 22:47:44 +01:00
} else {
2020-11-19 23:13:59 +01:00
$insertedQueueIdsTotal[] = $queueid;
2020-11-19 22:59:47 +01:00
if ($useConditions["usekitchen"] == 0) {
2020-11-19 23:12:46 +01:00
self::reallyDeclareAsCooked($pdo,$queueid);
2020-11-19 22:59:47 +01:00
if ($useConditions["usesupply"] == 0) {
2020-11-19 23:12:46 +01:00
self::declareProductBeDeliveredWithGivenPdo($pdo,$queueid);
2020-11-19 22:59:47 +01:00
}
2020-11-19 22:55:30 +01:00
} else {
2020-11-19 23:13:59 +01:00
$insertedQueueIdsForPrint[] = $queueid;
2020-11-19 23:10:48 +01:00
if (($printAndQueueJobs == 0) && ($doPrint == 0)) {
2020-11-19 23:12:53 +01:00
self::setFlagForCooking($pdo,$productid);
2020-11-19 23:10:48 +01:00
}
2020-11-19 22:55:30 +01:00
}
2020-11-19 22:59:47 +01:00
}
2020-11-19 22:47:44 +01:00
}
}
2020-11-19 23:14:48 +01:00
$tseEntriesLine = implode("\n", $tseEntries);
$signStatus = self::signAtTSE($pdo, $tseEntriesLine, $queueitemids,false);
if ($signStatus["status"] != "OK") {
$pdo->rollBack();
return(array("status" => "ERROR","msg" => $signStatus["msg"]));
}
2020-11-19 23:13:59 +01:00
$generalQuickcash = CommonUtils::getConfigValue($pdo, "cashenabled", 1);
if (($generalQuickcash == 1) || ($quickcash == 1)) {
$idStr = join(',',$insertedQueueIdsTotal);
2020-11-19 23:14:02 +01:00
$billid = $this->declarePaidCreateBillReturnBillId($pdo,$idStr, $theTableid, 1, 0, 0, self::$INTERNAL_CALL_YES,'','','',null);
$forceprint = CommonUtils::getConfigValue($pdo, 'forceprint', 0);
if ($forceprint == 1) {
if(session_id() == '') {
session_start();
}
$recprinter = $_SESSION['receiptprinter'];
PrintQueue::internalQueueReceiptPrintjob($pdo, $billid, $recprinter);
}
2020-11-19 23:00:55 +01:00
}
2020-11-19 22:47:44 +01:00
if ($doPrint == 1) {
if ($payprinttype == "s") {
2020-11-19 23:14:48 +01:00
$this->doWorkPrint($pdo,$theTableid,$insertedQueueIdsForPrint,$_SESSION['currentuser'],$userid,$payprinttype, $_SESSION['language']);
2020-11-19 23:12:48 +01:00
$pdo->commit();
2020-11-19 23:12:46 +01:00
return(array("status" => "OK"));
2020-11-19 22:47:44 +01:00
} else {
2020-11-19 23:14:48 +01:00
$result = $this->doWorkPrint($pdo,$theTableid,$insertedQueueIdsForPrint,$_SESSION['currentuser'],$userid,$payprinttype, $_SESSION['language']);
2020-11-19 23:12:48 +01:00
$pdo->commit();
2020-11-19 23:12:46 +01:00
return(array("status" => "OK", "msg" => $result));
2020-11-19 22:47:44 +01:00
}
}
$pdo->commit();
2020-11-19 23:13:59 +01:00
return(array("status" => "OK","queueids" => $insertedQueueIdsTotal));
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:14:48 +01:00
private static function quoteTseName($txt) {
return str_replace("\"", "\"\"", $txt);
}
private static function signAtTSE($pdo,$signTxtValue,$queueitemids,$isCancel) {
$sentToTseResult = Tse::sendOrdersToTSE($pdo,$signTxtValue);
if ($sentToTseResult["status"] != "OK") {
return(array("status" => "ERROR","msg" => "TSE-Signierung fehlgeschlagen. Vorgang konnte nicht ausgeführt werden."));
} else {
$logtime = 0;
$trans = 0;
$tseSignature = '';
$pubkeyRef = null;
$sigalgRef = null;
$sigcounter = 0;
$signtxt = $signTxtValue;
$certificateRef = null;
$serialNoRef = null;
if ($sentToTseResult["usetse"] == DbUtils::$TSE_OK) {
2020-11-19 23:15:07 +01:00
$logtime = $sentToTseResult["logtime"];
$trans = $sentToTseResult["trans"];
$sigcounter = $sentToTseResult["sigcounter"];
$tseSignature = $sentToTseResult["signature"];
$sigalgRef = CommonUtils::referenceValueInTseValuesTable($pdo, $sentToTseResult["sigalg"]);
$pubkeyRef = CommonUtils::referenceValueInTseValuesTable($pdo, $sentToTseResult["publickey"]);
$serialNoRef = CommonUtils::referenceValueInTseValuesTable($pdo, $sentToTseResult["serialno"]);
$certificateRef = CommonUtils::referenceValueInTseValuesTable($pdo, $sentToTseResult["certificate"]);
2020-11-19 23:14:48 +01:00
}
$opid = Operations::createOperation(
$pdo,
DbUtils::$PROCESSTYPE_VORGANG,
DbUtils::$OPERATION_IN_QUEUE_TABLE,
$logtime,
$trans,
$signtxt,
$tseSignature,
$pubkeyRef,
$sigalgRef,
$serialNoRef,
$certificateRef,
$sigcounter,
$sentToTseResult["usetse"]
);
if (!$isCancel) {
$sql = "UPDATE %queue% SET opidok=?,logtime=?,poszeile=? WHERE id=?";
$poszeile = 1;
foreach($queueitemids as $aQueueItemId) {
CommonUtils::execSql($pdo, $sql, array($opid,$logtime,$poszeile,$aQueueItemId));
$poszeile++;
}
} else {
$sql = "UPDATE %queue% SET opidcancel=?,cancellogtime=? WHERE id=?";
foreach($queueitemids as $aQueueItemId) {
CommonUtils::execSql($pdo, $sql, array($opid,$logtime,$aQueueItemId));
}
}
}
if (($sentToTseResult["usetse"] == DbUtils::$TSE_OK) || ($sentToTseResult["usetse"] == DbUtils::$TSE_KNOWN_ERROR) || ($sentToTseResult["usetse"] == DbUtils::$NO_TSE)) {
return array("status" => "OK");
} else {
return array("status" => "ERROR","msg" => "Error TSE sign process");
}
}
2020-11-19 23:12:53 +01:00
public static function setFlagForCooking($pdo,$productid) {
$beepordered = CommonUtils::getConfigValue($pdo, "beepordered", 0);
if ($beepordered == 1) {
$sql = "SELECT kind FROM %prodtype% T,%products% P where P.id=? AND P.category=T.id";
$result = CommonUtils::fetchSqlAll($pdo, $sql, array($productid));
if (count($result)>0) {
$kindOfNewProd = $result[0]['kind'];
if ($kindOfNewProd == 0) {
self::setNewFoodToCookFlag($pdo, 1);
} else {
self::setNewDrinkToCookFlag($pdo, 1);
}
}
}
}
2020-11-19 23:10:06 +01:00
function addProductListToQueueForGuest($pdo,$ordertime,$theTableid,$prodid,$doPrint) {
if(session_id() == '') {
session_start();
}
date_default_timezone_set(DbUtils::getTimeZone());
$pdo->beginTransaction();
2020-11-19 23:10:26 +01:00
2020-11-19 23:10:06 +01:00
$workflowconfig = CommonUtils::getExistingConfigValue($pdo, 'workflowconfig');
2020-11-19 23:15:07 +01:00
$commUtils = new CommonUtils();
$currentPriceLevel = $commUtils->getCurrentPriceLevel($pdo);
2020-11-19 23:10:06 +01:00
$currentPriceLevelId = $currentPriceLevel["id"];
$insertedQueueIds = array();
2020-11-19 23:15:07 +01:00
$sql = "INSERT INTO %records% (date,userid,tableid,action) VALUES(?,?,?,?)";
CommonUtils::execSql($pdo, $sql, array($ordertime,null,$theTableid,T_ORDER));
$recordid = $pdo->lastInsertId();
2020-11-19 23:14:48 +01:00
2020-11-19 23:10:06 +01:00
$productid = $prodid;
$theOption = '';
$theChangedPrice = "NO";
2020-11-19 23:14:48 +01:00
$getPriceSql = "SELECT priceA,priceB,priceC,longname,tax as prodtaxkey,taxaustria,amount FROM %products% where id=?";
2020-11-19 23:10:06 +01:00
$row = CommonUtils::getRowSqlObject($pdo, $getPriceSql, array($productid));
if ($row == null) {
echo "Fehler: Preise nicht vorhanden"; // error
2020-11-19 23:15:07 +01:00
return false;
2020-11-19 23:10:06 +01:00
}
if (!is_null($row->amount)) {
$amount = max(($row->amount -1),0);
$sql = "UPDATE %products% SET amount=? WHERE id=?";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($amount,$productid));
}
$productname = $row->longname;
$price_for_level_A = $row->priceA;
$price_for_level_B = $row->priceB;
$price_for_level_C = $row->priceC;
$price = $price_for_level_A; // default - levl 1
if ($currentPriceLevelId == 2) {
$price = $price_for_level_B;
} else if ($currentPriceLevelId == 3) {
$price = $price_for_level_C;
} // else: use default price A
2020-11-19 23:14:48 +01:00
$taxkeytouse = $row->prodtaxkey;
$taxToUse = CommonUtils::getTaxFromKey($pdo,$taxkeytouse);
2020-11-19 23:10:06 +01:00
$taxaustrianumber = null;
if (is_null($theTableid) || (is_numeric($theTableid) && is_numeric($productid))) {
2020-11-19 23:12:46 +01:00
$useConditions = self::getUseKitchenAndSupplyForProdWithPdo($pdo,$productid);
2020-11-19 23:10:06 +01:00
$insertSql = "INSERT INTO `%queue%` (
2020-11-19 23:14:48 +01:00
`tablenr`,`productid`,`pricelevel`,`price`,`pricetype`,`unit`,`unitamount`,`ordertype`, `voucherid`,`tax`,`taxkey`,`taxaustria`,`productname`,`ordertime`,`orderuser`,`anoption`,`pricechanged`,`togo`,`readytime`,`delivertime`,`paidtime`,`billid`,`toremove`,`cooking`,`workprinted`)
VALUES ( ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, null, null, NULL,NULL,'0',NULL,'0')";
2020-11-19 23:10:06 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($insertSql));
2020-11-19 23:14:48 +01:00
$stmt->execute(array($theTableid,$productid,$currentPriceLevelId,$price,0,0,1.00,DbUtils::$ORDERTYPE_PRODUCT,null,$taxToUse,$taxkeytouse,$taxaustrianumber,$productname,$ordertime,null,$theOption,($theChangedPrice == "NO" ? 0 : 1),0));
$queueid = $pdo->lastInsertId();
2020-11-19 23:10:26 +01:00
$sql = "INSERT INTO %recordsqueue% (recordid,queueid) VALUES(?,?)";
CommonUtils::execSql($pdo, $sql, array($recordid,$queueid));
2020-11-19 23:10:06 +01:00
2020-11-19 23:14:48 +01:00
$quotedProdname = self::quoteTseName($productname);
$tseEntries = "1;\"" . $quotedProdname . "\";" . $price;
2020-11-19 23:13:59 +01:00
if (($workflowconfig == DbUtils::$WORKFLOW_DIGITAL_AND_WORK) && ($doPrint == DbUtils::$WORKFLOW_WORK_WITH_SERVER)) {
2020-11-19 23:12:46 +01:00
self::reallyDeclareAsCooked($pdo,$queueid);
self::declareProductBeDeliveredWithGivenPdo($pdo,$queueid);
2020-11-19 23:10:06 +01:00
} else {
if ($useConditions["usekitchen"] == 0) {
2020-11-19 23:12:46 +01:00
self::reallyDeclareAsCooked($pdo,$queueid);
2020-11-19 23:10:06 +01:00
if ($useConditions["usesupply"] == 0) {
2020-11-19 23:12:46 +01:00
self::declareProductBeDeliveredWithGivenPdo($pdo,$queueid);
2020-11-19 23:10:06 +01:00
}
} else {
$insertedQueueIds[] = $queueid;
}
}
2020-11-19 23:14:48 +01:00
$signStatus = self::signAtTSE($pdo, $tseEntries, array($queueid),false);
2020-11-19 23:15:07 +01:00
if ($signStatus["status"] != "OK") {
error_log("Signing guest order at TSE impossible. Ignore guest order");
2020-11-19 23:14:48 +01:00
$pdo->rollBack();
2020-11-19 23:15:07 +01:00
return false;
2020-11-19 23:14:48 +01:00
}
2020-11-19 23:10:06 +01:00
}
if ($doPrint == 1) {
2020-11-19 23:14:48 +01:00
$this->doWorkPrint($pdo,$theTableid,$insertedQueueIds,'Gastbestellung',0,'s', $_SESSION['language']);
2020-11-19 23:10:06 +01:00
}
$pdo->commit();
2020-11-19 23:15:07 +01:00
return true;
2020-11-19 23:10:06 +01:00
}
2020-11-19 22:47:44 +01:00
/*
* Do as if the product would have been removed from queue - but don't do it exactly,
* because then it would not appear in the reports any more. Instead declare the
2020-11-19 23:12:27 +01:00
* toremove = 1 (was never ordered...)
2020-11-19 22:47:44 +01:00
*/
function removeProductFromQueue($queueid,$isPaid,$isCooking,$isReady) {
if (is_numeric($queueid)) {
2020-11-19 23:10:26 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
2020-11-19 23:14:48 +01:00
$pdo->beginTransaction();
2020-11-19 23:10:26 +01:00
date_default_timezone_set(DbUtils::getTimeZone());
$currentTime = date('Y-m-d H:i:s');
$userid = $this->getUserId();
$sql = "SELECT tablenr FROM %queue% WHERE id=?";
$result = CommonUtils::fetchSqlAll($pdo, $sql, array($queueid));
if (count($result) == 0) {
echo json_encode(array("status" => "OK"));
return;
}
$tableid = $result[0]["tablenr"];
2020-11-19 22:47:44 +01:00
$sql = "SELECT count(id) as countid FROM %bill%";
2020-11-19 23:00:55 +01:00
$row = CommonUtils::getRowSqlObject($pdo, $sql, null);
2020-11-19 22:47:44 +01:00
$hasBills = ($row->countid > 0 ? true : false);
2020-11-19 22:59:47 +01:00
if ($hasBills) {
2020-11-19 22:47:44 +01:00
$sql = "UPDATE %queue%,%bill% ";
} else {
$sql = "UPDATE %queue% ";
}
2020-11-19 23:12:27 +01:00
$sql .= "SET %queue%.toremove='1' WHERE %queue%.id=? AND %queue%.toremove='0' ";
2020-11-19 22:47:44 +01:00
if ($isPaid == '1') {
2020-11-19 23:00:46 +01:00
$sql .= " AND paidtime IS NOT NULL ";
2020-11-19 22:47:44 +01:00
} else {
2020-11-19 23:00:46 +01:00
$sql .= " AND (paidtime IS NULL or paidtime is null) ";
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:14:48 +01:00
Vouchers::handleRemoveQueueItem($pdo,$queueid);
2020-11-19 22:47:44 +01:00
if ($isCooking == '1') {
$sql .= " AND cooking is not null ";
} else {
$sql .= " AND cooking is null ";
}
if ($isReady == '1') {
2020-11-19 23:00:46 +01:00
$sql .= " AND readytime IS NOT NULL ";
2020-11-19 22:47:44 +01:00
} else {
2020-11-19 23:00:46 +01:00
$sql .= " AND (readytime IS NULL or readytime is null) ";
2020-11-19 22:47:44 +01:00
}
if ($hasBills) {
$sql .= " AND (billid is null OR (";
$sql .= " billid = %bill%.id AND %bill%.closingid is null)) ";
}
2020-11-19 23:10:26 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
2020-11-19 22:59:47 +01:00
2020-11-19 22:47:44 +01:00
$stmt->execute(array($queueid));
2020-11-19 22:59:47 +01:00
$rowsAffected = $stmt->rowCount();
2020-11-19 22:47:44 +01:00
if ($rowsAffected == 1) {
2020-11-19 23:14:48 +01:00
$signRemoval = self::signRemovalOfQueueItem($pdo, $queueid);
if ($signRemoval["status"] != "OK") {
$pdo->rollBack();
return(array("status" => "ERROR","msg" => "TSE-Signierung fehlgeschlagen: " . $signRemoval["msg"]));
}
2020-11-19 23:03:48 +01:00
Workreceipts::createCancelWorkReceipt($pdo, $queueid);
2020-11-19 23:10:26 +01:00
$sql = "INSERT INTO %records% (date,userid,tableid,action) VALUES(?,?,?,?)";
CommonUtils::execSql($pdo, $sql, array($currentTime,$userid,$tableid,T_REMOVE));
$recordid = $pdo->lastInsertId();
$sql = "INSERT INTO %recordsqueue% (recordid,queueid) VALUES(?,?)";
CommonUtils::execSql($pdo, $sql, array($recordid,$queueid));
2020-11-19 23:14:48 +01:00
$pdo->commit();
2020-11-19 22:47:44 +01:00
echo json_encode(array("status" => "OK"));
} else {
2020-11-19 23:14:48 +01:00
$pdo->rollBack();
2020-11-19 22:47:44 +01:00
echo json_encode(array("status" => "Failed", "msg" => "Affected rows: $rowsAffected"));
}
}
}
2020-11-19 23:14:48 +01:00
public static function signRemovalOfQueueItem($pdo,$queueid) {
2020-11-19 23:15:01 +01:00
$sql = "SELECT (IF(unit=0,ROUND(0-unitamount),0-unitamount)) as unitamount,unit,productname,price FROM %queue% WHERE id=?";
2020-11-19 23:14:48 +01:00
$result = CommonUtils::fetchSqlAll($pdo, $sql, array($queueid));
$amount = $result[0]["unitamount"];
$unit = $result[0]["unitamount"];
if ($unit == 0) {
$orderAmount = 1;
} else {
$amountAsDouble = doubleval($amount);
$orderAmount = strval($amountAsDouble);
}
$prodname = self::quoteTseName($result[0]["productname"]);
$price = $result[0]["price"];
$signTxt = $orderAmount . ";\"" . $prodname . "\";" . $price;
$signStatus = self::signAtTSE($pdo, $signTxt, array($queueid),true);
if ($signStatus["status"] != "OK") {
return array("status" => "ERROR","msg" => "TSE-Signierung fehlgeschlagen: " . $signStatus["msg"]);
} else {
return array("status" => "OK");
}
}
2020-11-19 22:55:43 +01:00
function getUnpaidTables() {
$pdo = DbUtils::openDbAndReturnPdoStatic();
2020-11-19 23:12:27 +01:00
$sql = "SELECT DISTINCT %resttables%.tableno as tablename FROM %queue%,%resttables% WHERE paidtime is null AND %queue%.toremove='0' and tablenr=%resttables%.id AND isclosed is null";
2020-11-19 23:11:44 +01:00
$result = CommonUtils::fetchSqlAll($pdo, $sql, null);
2020-11-19 22:55:43 +01:00
$unpaidTables = array();
foreach ($result as $anUnpaidTable) {
$unpaidTables[] = $anUnpaidTable["tablename"];
}
2020-11-19 23:12:27 +01:00
$sql = "SELECT COUNT(id) as takeawayitems FROM %queue% WHERE tablenr is null AND paidtime is null AND %queue%.toremove='0' AND isclosed is null";
2020-11-19 23:11:44 +01:00
$result = CommonUtils::fetchSqlAll($pdo, $sql, null);
$cnt = $result[0]["takeawayitems"];
$takeawayHasOpenBills = 0;
if ($cnt != 0) {
$takeawayHasOpenBills = 1;
}
echo json_encode(array("status" => "OK","msg" => join(",",$unpaidTables),"takeawayhasopenbills" => $takeawayHasOpenBills));
2020-11-19 22:55:43 +01:00
}
2020-11-19 22:47:44 +01:00
/*
* Return as JSON structure all products that are assigned to a specified table, with the
* specification that they are not delivered yet.
*
2020-11-19 23:12:27 +01:00
* toremove must not be 1, because = 1 means that is is paid but was cancelled later
2020-11-19 22:47:44 +01:00
* by the waiter! (in a previous version such entries were deleted from queue, but then
* they won't appear in reports any more)
*
* Return is: [
* {"queueid":"2","longname":"EL Greco 1 Person", "isReady":"1"},
* {"queueid":"5","longname":"Souvlaki","isReady":"0"}]
* (a sample)
*
*/
function getJsonLongNamesOfProdsForTableNotDelivered($tableid) {
if (is_numeric($tableid)) {
2020-11-19 22:59:47 +01:00
$prods = array();
2020-11-19 22:47:44 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
$sql = "SELECT count(id) as countid FROM %bill%";
2020-11-19 23:00:55 +01:00
$row = CommonUtils::getRowSqlObject($pdo, $sql, null);
2020-11-19 22:47:44 +01:00
if ($row->countid == 0) {
2020-11-19 23:12:27 +01:00
$sql = "SELECT DISTINCT %queue%.id as quid, ordertime FROM %queue% WHERE %queue%.toremove='0' AND isclosed is null AND ";
2020-11-19 22:47:44 +01:00
} else {
2020-11-19 23:12:27 +01:00
$sql = "SELECT DISTINCT %queue%.id as quid, ordertime FROM %queue%,%bill% WHERE %queue%.toremove='0' AND isclosed is null AND ((%queue%.billid is null AND %queue%.paidtime is null) OR (%queue%.billid=%bill%.id AND %bill%.closingid is null)) AND ";
2020-11-19 22:47:44 +01:00
}
if ($tableid == 0) {
2020-11-19 23:00:14 +01:00
$sql .= "%queue%.tablenr is null ORDER BY ordertime, quid";
2020-11-19 22:47:44 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute();
} else {
2020-11-19 23:00:14 +01:00
$sql .= "%queue%.tablenr=? ORDER BY ordertime, quid";
2020-11-19 22:47:44 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($tableid));
}
$allNotClosedQueueIds = $stmt->fetchAll();
$resultids = array();
2020-11-19 23:00:46 +01:00
$sql = "SELECT count(id) as countid FROM %queue% WHERE %queue%.id=? AND (%queue%.delivertime IS NULL ";
$sql .= " OR %queue%.readytime IS NULL ";
2020-11-19 22:47:44 +01:00
$sql .= " OR (%queue%.billid is null AND %queue%.paidtime is null)) ";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
foreach($allNotClosedQueueIds as $aQueueId) {#
$aQId = $aQueueId["quid"];
$stmt->execute(array($aQId));
$row = $stmt->fetchObject();
if ($row->countid > 0) {
$resultids[] = $aQId;
}
}
$prods = array();
2020-11-19 23:12:35 +01:00
$sql = "SELECT productid,readytime,paidtime,cooking,productname,anoption,productname,togo,pricechanged,price,unit,unitamount FROM %queue% WHERE id=? ORDER BY productid ";
2020-11-19 22:47:44 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
foreach ($resultids as $anId) {
$stmt->execute(array($anId));
$row = $stmt->fetchObject();
$isReady = "1";
$isDelivered = "1";
$isPaid = "1";
$isCooking = '1';
2020-11-19 23:00:46 +01:00
if ($row->readytime == null) {
2020-11-19 22:47:44 +01:00
$isReady = "0"; // not yet prepared by the kitchen
}
if ($row->paidtime == null) {
$isPaid = "0"; // not yet paid
}
if (is_null($row->cooking)) {
$isCooking = '0';
}
2020-11-19 22:58:27 +01:00
$extras = $this->getExtrasWithIdsOfQueueItem($pdo,$anId);
2020-11-19 22:47:44 +01:00
$prodEntry = array(
2020-11-19 22:55:09 +01:00
"id" =>$anId,
2020-11-19 22:58:27 +01:00
"prodid" => $row->productid,
2020-11-19 22:47:44 +01:00
"longname" => $row->productname,
"option" => $row->anoption,
2020-11-19 22:55:20 +01:00
"pricechanged" => $row->pricechanged,
"togo" => $row->togo,
"price" => $row->price,
2020-11-19 23:12:35 +01:00
"unit" => $row->unit,
"unitamount" => $row->unitamount,
2020-11-19 22:58:27 +01:00
"extras" => $extras["extrasnames"],
"extrasids" => $extras["extrasids"],
2020-11-19 23:12:39 +01:00
"extrasamounts" => $extras["extrasamounts"],
2020-11-19 22:47:44 +01:00
"isready" => $isReady,
"isPaid" => $isPaid,
"isCooking" => $isCooking);
$prods[] = $prodEntry;
}
echo json_encode($prods);
2020-11-19 22:59:47 +01:00
}
2020-11-19 22:47:44 +01:00
}
2020-11-19 22:50:09 +01:00
function getProdsForTableChange($tableid) {
$pdo = DbUtils::openDbAndReturnPdoStatic();
2020-11-19 22:47:44 +01:00
2020-11-19 22:50:09 +01:00
if ($tableid == 0) {
$tableid = null;
2020-11-19 22:47:44 +01:00
}
2020-11-19 22:59:47 +01:00
$sql = "SELECT %queue%.id as queueid,productname FROM ";
2020-11-19 22:50:09 +01:00
$sql .= "%queue% WHERE ";
2020-11-19 23:12:27 +01:00
$sql .= "(tablenr=? OR (tablenr IS NULL AND ? IS NULL)) AND %queue%.toremove='0' AND isclosed is null AND billid is null ";
2020-11-19 22:50:09 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($tableid,$tableid));
2020-11-19 22:59:47 +01:00
$unpaidresultungrouped = $stmt->fetchAll();
$sql = "SELECT %queue%.id as queueid,productname FROM ";
2020-11-19 22:50:09 +01:00
$sql .= "%queue% LEFT OUTER JOIN %bill% ON %queue%.billid=%bill%.id WHERE ";
2020-11-19 23:12:27 +01:00
$sql .= "(tablenr=? OR (tablenr IS NULL AND ? IS NULL)) AND %queue%.toremove='0' AND isclosed is null AND billid is null AND (";
2020-11-19 23:00:46 +01:00
$sql .= "%queue%.delivertime IS NULL OR ";
$sql .= "(%queue%.delivertime IS NOT NULL AND workprinted='1')) ";
2020-11-19 22:50:09 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($tableid,$tableid));
2020-11-19 22:59:47 +01:00
$undeliveredresultungrouped = $stmt->fetchAll();
$merged = array();
foreach($unpaidresultungrouped as $entry) {
$qid = $entry["queueid"];
$prodname = $entry["productname"];
$status = "unpaid";
if ($this->isQueueIdInList($qid, $undeliveredresultungrouped)) {
$status = "unpaid_undelivered";
}
$merged[] = array("queueid" => $qid,"productname" => $prodname,"status" => $status);
}
2020-11-19 22:47:44 +01:00
2020-11-19 22:59:47 +01:00
echo json_encode(array("status" => "OK","msg" => $merged));
}
function isQueueIdInList($queueid,$list) {
foreach($list as $entry) {
if ($entry['queueid'] == $queueid) {
return true;
}
}
return false;
2020-11-19 22:50:09 +01:00
}
function changeTable($fromTableId, $toTableId, $queueids) {
$ids = explode(",",$queueids);
foreach($ids as $id) {
if (!is_numeric($id)) {
echo json_encode(array("status" => "ERROR", "code" => NUMBERFORMAT_ERROR, "msg" => NUMBERFORMAT_ERROR_MSG));
return;
2020-11-19 22:47:44 +01:00
}
}
2020-11-19 22:50:09 +01:00
2020-11-19 23:14:48 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
2020-11-19 22:50:09 +01:00
$pdo->beginTransaction();
2020-11-19 23:10:26 +01:00
date_default_timezone_set(DbUtils::getTimeZone());
$currentTime = date('Y-m-d H:i:s');
$userid = $this->getUserId();
$sql = "INSERT INTO %records% (date,userid,tableid,action) VALUES(?,?,?,?)";
CommonUtils::execSql($pdo, $sql, array($currentTime,$userid,$fromTableId,T_FROM_TABLE));
$recordidFromTable = $pdo->lastInsertId();
$sql = "INSERT INTO %records% (date,userid,tableid,action) VALUES(?,?,?,?)";
CommonUtils::execSql($pdo, $sql, array($currentTime,$userid,$toTableId,T_TO_TABLE));
$recordidToTable = $pdo->lastInsertId();
$sql = "INSERT INTO %recordsqueue% (recordid,queueid) VALUES(?,?)";
foreach($ids as $id) {
CommonUtils::execSql($pdo, $sql, array($recordidFromTable,$id));
CommonUtils::execSql($pdo, $sql, array($recordidToTable,$id));
}
2020-11-19 22:50:09 +01:00
$sql = "UPDATE %queue% SET tablenr=? WHERE id IN($queueids) AND tablenr=?";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($toTableId,$fromTableId));
2020-11-19 22:47:44 +01:00
$pdo->commit();
echo json_encode(array("status" => "OK"));
}
function getJsonProductsOfTableToPay($tableid) {
2020-11-19 22:54:51 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
2020-11-19 23:12:35 +01:00
$unit = CommonUtils::caseOfSqlUnitSelection($pdo);
2020-11-19 23:15:07 +01:00
$sql = "SELECT Q.id as id,Q.unit,CONCAT($unit,Q.productname) as longname,Q.price as price,Q.tax,%prodtype%.kind as kind,%pricelevel%.name as pricelevelname,%products%.id as prodid,Q.togo as togo, ordertime,COALESCE(prodimageid,0) as prodimageid,COALESCE(printjobid,0) as printjobid
2020-11-19 23:12:35 +01:00
FROM %queue% Q
INNER JOIN %products% ON Q.productid = %products%.id
INNER JOIN %pricelevel% ON Q.pricelevel = %pricelevel%.id
2020-11-19 23:01:07 +01:00
INNER JOIN %prodtype% ON %products%.category = %prodtype%.id ";
2020-11-19 22:47:44 +01:00
if ($tableid == 0) {
$sql .= "WHERE tablenr is null ";
2020-11-19 22:59:47 +01:00
} else {
2020-11-19 22:47:44 +01:00
$sql .= "WHERE tablenr = $tableid ";
}
2020-11-19 23:12:35 +01:00
$sql .= "AND paidtime is null AND Q.toremove='0' AND isclosed is null ORDER BY kind,ordertime, id;";
2020-11-19 22:47:44 +01:00
2020-11-19 22:54:51 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute();
$result = $stmt->fetchAll();
2020-11-19 22:55:09 +01:00
$prodsToPay = array();
2020-11-19 22:59:47 +01:00
foreach ($result as $zeile) {
2020-11-19 22:47:44 +01:00
$thePrice = $zeile['price'];
$theTax = $zeile['tax'];
2020-11-19 22:59:47 +01:00
$thePriceLevelName = $zeile['pricelevelname'];
2020-11-19 22:47:44 +01:00
$longName = $zeile['longname'];
2020-11-19 22:55:20 +01:00
$togo = $zeile["togo"];
2020-11-19 22:47:44 +01:00
$queueid = $zeile['id'];
2020-11-19 23:12:00 +01:00
$prodimageid = $zeile['prodimageid'];
2020-11-19 23:12:07 +01:00
$printjobid = $zeile["printjobid"];
2020-11-19 22:47:44 +01:00
2020-11-19 22:54:51 +01:00
$extras = $this->getExtrasOfQueueItem($pdo,$queueid);
2020-11-19 22:47:44 +01:00
$prodId = $zeile['prodid'];
2020-11-19 23:12:07 +01:00
$prodsToPay[] = array("id" => $queueid, "prodid" => $prodId, "longname" => $longName, "pricelevelname" => $thePriceLevelName, "price" => $thePrice, "tax" => $theTax, "togo" => $togo, "prodimageid" => $prodimageid, "printjobid" => $printjobid, "extras" => $extras);
2020-11-19 22:47:44 +01:00
}
echo json_encode(array("status" => "OK", "msg" => $prodsToPay));
}
function displayBill($billtableitems,$totalPrice) {
$currency = $this->commonUtils->getCurrency();
2020-11-19 22:59:47 +01:00
$numberOfItemsToPay = count($billtableitems);
if ($numberOfItemsToPay > 0) {
echo "<br><br><table id=bill class=billtable>";
echo "<tr><th>Speise/Getränk<th id=pricecolheader>Preis ($currency)</tr>";
for ($i=0;$i < $numberOfItemsToPay; $i++) {
$aProductToPay = $billtableitems[$i];
echo "<tr>";
echo "<td>" . $aProductToPay['textOfButton'] . "<td id=pricecol>" . $aProductToPay['price'] . "</tr>";
}
echo "<tr><td id=totalprice colspan=2>Gesamtpreis: " . $totalPrice . " $currency </tr>";
}
2020-11-19 22:47:44 +01:00
echo "</table>";
}
2020-11-19 23:12:46 +01:00
static function declareProductBeDeliveredWithGivenPdo($pdo,$queueid) {
2020-11-19 22:47:44 +01:00
if (is_numeric($queueid)) {
date_default_timezone_set(DbUtils::getTimeZone());
$delivertime = date('Y-m-d H:i:s');
$updateSql = "UPDATE %queue% SET delivertime=? WHERE id=?";
2020-11-19 23:12:46 +01:00
CommonUtils::execSql($pdo, $updateSql, array($delivertime,$queueid));
2020-11-19 22:47:44 +01:00
$updateSql = "UPDATE %queue% SET readytime=? WHERE id=?";
2020-11-19 23:12:46 +01:00
CommonUtils::execSql($pdo, $updateSql, array($delivertime,$queueid));
2020-11-19 22:47:44 +01:00
}
}
function declareProductBeDelivered($queueid) {
if (is_numeric($queueid)) {
$pdo = $this->dbutils->openDbAndReturnPdo();
2020-11-19 23:12:46 +01:00
self::declareProductBeDeliveredWithGivenPdo($pdo, $queueid);
2020-11-19 22:59:47 +01:00
}
2020-11-19 22:47:44 +01:00
}
function declareMultipleProductsDelivered($queueids) {
$ids = explode(",",$queueids);
2020-11-19 22:59:47 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
2020-11-19 22:47:44 +01:00
$pdo->beginTransaction();
for ($i=0;$i < count($ids); $i++) {
$aQueueId = $ids[$i];
if (is_numeric($aQueueId)) {
2020-11-19 23:12:46 +01:00
self::declareProductBeDeliveredWithGivenPdo($pdo,$aQueueId);
2020-11-19 22:47:44 +01:00
}
}
$pdo->commit();
echo json_encode(array("status" => "OK"));
}
function declareProductNotBeDelivered($queueid) {
2020-11-19 22:54:51 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
2020-11-19 22:59:47 +01:00
if (is_numeric($queueid)) {
date_default_timezone_set(DbUtils::getTimeZone());
$delivertime = date('Y-m-d H:i:s');
2020-11-19 23:00:49 +01:00
$updateSql = "UPDATE %queue% SET delivertime=? WHERE id=?";
2020-11-19 22:54:51 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($updateSql));
2020-11-19 23:00:49 +01:00
$stmt->execute(array(null,$queueid));
2020-11-19 22:47:44 +01:00
}
}
2020-11-19 23:11:42 +01:00
public function getAllPreparedProductsForTableidAsArray($pdo,$tableid) {
2020-11-19 22:47:44 +01:00
if (!is_null($tableid)) {
2020-11-19 23:11:42 +01:00
if (!is_numeric($tableid)) {
return array("prods" => array(), "ids" => '');
}
2020-11-19 22:47:44 +01:00
$sql = "SELECT DISTINCT %queue%.id as id,tableno,longname,anoption,readytime ";
if ($this->areBillExisting($pdo)) {
$sql .= "FROM %queue%,%products%,%resttables%,%bill% ";
} else {
$sql .= "FROM %queue%,%products%,%resttables% ";
}
2020-11-19 23:00:46 +01:00
$sql .= "WHERE (readytime IS NOT NULL and delivertime IS NULL ";
2020-11-19 22:47:44 +01:00
$sql .= "AND %queue%.tablenr=%resttables%.id ";
$sql .= "AND %resttables%.id=" . $tableid . " ";
} else {
$sql = "SELECT DISTINCT %queue%.id as id,'' as tableno,longname,anoption,readytime ";
if ($this->areBillExisting($pdo)) {
2020-11-19 23:03:41 +01:00
$sql .= "FROM %queue%,%products%,%bill% ";
2020-11-19 22:47:44 +01:00
} else {
2020-11-19 23:03:41 +01:00
$sql .= "FROM %queue%,%products% ";
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:00:46 +01:00
$sql .= "WHERE (readytime IS NOT NULL and delivertime IS NULL ";
2020-11-19 23:11:42 +01:00
$sql .= "AND %queue%.tablenr is null ";
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:11:42 +01:00
$sql .= "AND %queue%.productid=%products%.id ";
$sql .= "AND %queue%.isclosed is null ";
2020-11-19 23:12:27 +01:00
$sql .= "AND toremove = '0') ";
2020-11-19 23:11:42 +01:00
if ($this->areBillExisting($pdo)) {
$sql .= "AND (%queue%.billid is null OR (";
$sql .= "%queue%.billid=%bill%.id AND %bill%.closingid is null)) ";
}
$sql = $sql . " ORDER BY tableno";
2020-11-19 22:47:44 +01:00
2020-11-19 23:14:58 +01:00
$tmp = DbUtils::substTableAlias($sql);
2020-11-19 23:11:42 +01:00
$dbresult = CommonUtils::fetchSqlAll($pdo, $sql, null);
2020-11-19 22:59:47 +01:00
2020-11-19 22:47:44 +01:00
$arrayOfProdsForTable = array();
2020-11-19 23:11:42 +01:00
$idsProdsOfTable = array(); // this is a hack! All queueids of a table redundant for "Deliver all"
2020-11-19 22:59:47 +01:00
foreach($dbresult as $zeile) {
2020-11-19 22:47:44 +01:00
$extras = $this->getExtrasOfQueueItem(null,$zeile['id']);
$anProdElem = array(
"id" => $zeile['id'],
"longname" => $zeile['longname'],
"option" => $zeile['anoption'],
"extras" => $extras,
2020-11-19 22:59:47 +01:00
"status" => "ready_to_deliver");
2020-11-19 22:47:44 +01:00
$arrayOfProdsForTable[] = $anProdElem;
2020-11-19 23:11:42 +01:00
$idsProdsOfTable[] = $zeile['id'];
2020-11-19 22:59:47 +01:00
}
2020-11-19 22:47:44 +01:00
2020-11-19 23:11:42 +01:00
return array("prods" => $arrayOfProdsForTable, "ids" => join(',',$idsProdsOfTable));
2020-11-19 22:47:44 +01:00
}
public function numberOfProductsForTableNotDelivered($pdo,$tableid) {
$sql = "SELECT DISTINCT %queue%.id as id ";
if (!is_null($tableid)) {
$sql .= "FROM %queue%,%resttables% ";
} else {
$sql .= "FROM %queue% ";
}
2020-11-19 23:00:46 +01:00
$sql .= "WHERE delivertime IS NULL ";
2020-11-19 22:50:09 +01:00
$sql .= "AND %queue%.isclosed is null ";
2020-11-19 22:47:44 +01:00
$sql .= "AND workprinted='0' ";
2020-11-19 23:12:27 +01:00
$sql .= "AND toremove = '0' ";
2020-11-19 22:47:44 +01:00
if (!is_null($tableid)) {
$sql .= "AND %queue%.tablenr=%resttables%.id ";
$sql .= "AND %resttables%.id=" . $tableid;
} else {
$sql .= "AND %queue%.tablenr is null ";
}
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute();
$dbresult = $stmt->fetchAll();
$numberOfProducts = count($dbresult);
return $numberOfProducts;
}
function getJsonAllPreparedProducts() {
$pdo = DbUtils::openDbAndReturnPdoStatic();
$sql = "SELECT DISTINCT tablenr ";
if ($this->areBillExisting($pdo)) {
$sql .= "FROM %queue%,%resttables%,%bill% ";
} else {
$sql .= "FROM %queue%,%resttables% ";
}
2020-11-19 23:00:46 +01:00
$sql .= "WHERE (readytime IS NOT NULL and delivertime IS NULL ";
2020-11-19 23:12:27 +01:00
$sql .= "AND toremove = '0' ";
2020-11-19 22:47:44 +01:00
$sql .= "AND %queue%.tablenr=%resttables%.id AND ";
2020-11-19 22:50:09 +01:00
$sql .= "%queue%.isclosed is null AND ";
2020-11-19 22:47:44 +01:00
$sql .= "%queue%.workprinted='0') ";
if ($this->areBillExisting($pdo)) {
2020-11-19 22:59:47 +01:00
$sql .= "AND (%queue%.billid is null OR (";
2020-11-19 22:47:44 +01:00
$sql .= "%queue%.billid=%bill%.id AND %bill%.closingid is null)) ";
}
$sql .= " ORDER BY tablenr";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute();
$result1 = $stmt->fetchAll();
$sql = "SELECT DISTINCT tablenr ";
if ($this->areBillExisting($pdo)) {
$sql .= "FROM %queue%,%bill% ";
} else {
$sql .= "FROM %queue% ";
}
2020-11-19 23:00:46 +01:00
$sql .= "WHERE (readytime IS NOT NULL and delivertime IS NULL ";
2020-11-19 23:12:27 +01:00
$sql .= "AND toremove = '0' ";
2020-11-19 22:47:44 +01:00
$sql .= "AND %queue%.tablenr is null AND ";
2020-11-19 22:50:09 +01:00
$sql .= "%queue%.isclosed is null AND ";
2020-11-19 22:47:44 +01:00
$sql .= "%queue%.workprinted='0') ";
if ($this->areBillExisting($pdo)) {
$sql .= "AND (%queue%.billid is null OR (";
$sql .= "%queue%.billid=%bill%.id AND %bill%.closingid is null)) ";
}
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute();
$result2 = $stmt->fetchAll();
$result = array_merge($result1,$result2);
$tablesToServe = array();
foreach($result as $zeile) {
$tablesToServe[] = $zeile['tablenr'];
}
$preparedProds_incomplete_tables = array();
$preparedProds = array();
$commonUtils = new CommonUtils();
foreach ($tablesToServe as $tableid) {
$arrayOfProdsAndIdsOfATable = $this->getAllPreparedProductsForTableidAsArray($pdo,$tableid);
$arrayOfProdsOfATable = $arrayOfProdsAndIdsOfATable['prods'];
$numberOfProductsTotalToServe = $this->numberOfProductsForTableNotDelivered($pdo,$tableid);
$numberOfReadyProducts = count($arrayOfProdsOfATable);
if ($numberOfReadyProducts >= $numberOfProductsTotalToServe) {
$tablestatus = "complete";
2020-11-19 22:54:51 +01:00
$tableheadeline = $commonUtils->getTableNameFromId($pdo,$tableid);
2020-11-19 22:47:44 +01:00
$preparedProds[] = array(
"tableheadline" => $tableheadeline,
2020-11-19 22:59:47 +01:00
"tableid" => $tableid,
2020-11-19 22:47:44 +01:00
"tablestatus" => $tablestatus,
2020-11-19 22:59:47 +01:00
"ids" => $arrayOfProdsAndIdsOfATable['ids'],
2020-11-19 22:47:44 +01:00
"prodsOfTable" => $arrayOfProdsOfATable);
} else {
$tablestatus = "incomplete";
2020-11-19 22:54:51 +01:00
$tableheadeline = "Tisch: " . $commonUtils->getTableNameFromId($pdo,$tableid);
2020-11-19 22:47:44 +01:00
$preparedProds_incomplete_tables[] = array(
"tableheadline" => $tableheadeline,
2020-11-19 22:59:47 +01:00
"tableid" => $tableid,
2020-11-19 22:47:44 +01:00
"tablestatus" => $tablestatus,
2020-11-19 22:59:47 +01:00
"ids" => $arrayOfProdsAndIdsOfATable['ids'],
2020-11-19 22:47:44 +01:00
"prodsOfTable" => $arrayOfProdsOfATable);
}
}
2020-11-19 23:00:55 +01:00
$newProdsToServe = self::getNewProductsToServe($pdo);
self::setNewProductsToServe($pdo, 0);
$items = array_merge($preparedProds,$preparedProds_incomplete_tables);
echo json_encode(array("items" => $items, "newproductstoserve" => $newProdsToServe));
2020-11-19 22:47:44 +01:00
}
/*
* Return as JSON object a list of max 10 entries of products that
* have been delivered to a table
*/
function getJsonLastDeliveredProducts() {
$pdo = DbUtils::openDbAndReturnPdoStatic();
$sql = "SELECT DISTINCT %queue%.id as id,tableno,longname,delivertime,anoption,%products%.id as prodid ";
if ($this->areBillExisting($pdo)) {
$sql .= "FROM %queue%,%resttables%,%products%,%bill% ";
} else {
$sql .= "FROM %queue%,%resttables%,%products% ";
}
2020-11-19 23:00:46 +01:00
$sql .= "WHERE (delivertime IS NOT NULL ";
2020-11-19 22:59:47 +01:00
$sql .= "AND %queue%.productid=%products%.id ";
2020-11-19 22:47:44 +01:00
$sql .= "AND %queue%.tablenr=%resttables%.id ";
2020-11-19 23:12:27 +01:00
$sql .= "AND toremove = '0' AND ";
2020-11-19 22:50:09 +01:00
$sql .= "%queue%.isclosed is null AND ";
2020-11-19 22:47:44 +01:00
$sql .= "%queue%.workprinted='0') ";
if ($this->areBillExisting($pdo)) {
2020-11-19 22:59:47 +01:00
$sql .= "AND (%queue%.billid is null OR (";
2020-11-19 22:47:44 +01:00
$sql .= "%queue%.billid=%bill%.id AND %bill%.closingid is null)) ";
}
$sql = $sql . "ORDER BY delivertime DESC LIMIT 10";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute();
$result1 = $stmt->fetchAll();
$sql = "SELECT DISTINCT %queue%.id as id,'' as tableno,longname,delivertime,anoption,%products%.id as prodid ";
if ($this->areBillExisting($pdo)) {
2020-11-19 23:03:41 +01:00
$sql .= "FROM %queue%,%products%,%bill% ";
2020-11-19 22:47:44 +01:00
} else {
2020-11-19 23:03:41 +01:00
$sql .= "FROM %queue%,%products% ";
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:00:46 +01:00
$sql .= "WHERE (delivertime IS NOT NULL ";
2020-11-19 22:47:44 +01:00
$sql .= "AND %queue%.productid=%products%.id ";
$sql .= "AND %queue%.tablenr is null ";
2020-11-19 23:12:27 +01:00
$sql .= "AND toremove = '0' AND ";
2020-11-19 22:50:09 +01:00
$sql .= "%queue%.isclosed is null AND ";
2020-11-19 22:47:44 +01:00
$sql .= "%queue%.workprinted='0') ";
if ($this->areBillExisting($pdo)) {
$sql .= "AND (%queue%.billid is null OR (";
$sql .= "%queue%.billid=%bill%.id AND %bill%.closingid is null)) ";
}
$sql = $sql . "ORDER BY delivertime DESC LIMIT 10";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute();
$result2 = $stmt->fetchAll();
$result = array_merge($result1,$result2);
$lastDeliveredProds = array();
foreach($result as $zeile) {
2020-11-19 22:59:47 +01:00
$productid = $zeile['prodid'];
$useConditions = $this->getUseKitchenAndSupplyForProd($pdo,$productid);
if ($useConditions["usesupply"] == 1) {
2020-11-19 22:47:44 +01:00
$extras = $this->getExtrasOfQueueItem(null,$zeile['id']);
$deliveredProd = array(
"id" => $zeile['id'],
"longname" => $zeile['longname'],
"option" => $zeile['anoption'],
"extras" => $extras,
"delivertime" => $zeile['delivertime'],
2020-11-19 22:59:47 +01:00
"tablename" => $zeile['tableno']);
2020-11-19 22:47:44 +01:00
$lastDeliveredProds[] = $deliveredProd;
2020-11-19 22:59:47 +01:00
}
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:11:42 +01:00
echo json_encode(array("status" => "OK","msg" => $lastDeliveredProds));
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:12:22 +01:00
2020-11-19 22:47:44 +01:00
/*
* Test if all queue items with the given ids are not paid
* -> if there are paid items --> report error by return negative value
*
* Set paid column with the given date
* Create bill
* Return a bill id
*/
2020-11-19 23:12:46 +01:00
public function declarePaidCreateBillReturnBillId($pdo,$ids,$tableid,$paymentId,$declareready,$host,$calledInternally = false,$reservationid='',$guestinfo='',$intguestid='',$userid=null, $billdate=null) {
2020-11-19 23:10:26 +01:00
date_default_timezone_set(DbUtils::getTimeZone());
$currentTime = date('Y-m-d H:i:s');
2020-11-19 23:12:46 +01:00
if (!is_null($billdate)) {
$currentTime = $billdate;
}
2020-11-19 23:00:58 +01:00
2020-11-19 23:02:08 +01:00
if ($intguestid == '') {
$intguestid = null;
}
2020-11-19 23:00:58 +01:00
if ($reservationid != "") {
$reservationid = substr($reservationid, 0, 30);
}
if ($guestinfo != "") {
$guestinfo = substr($guestinfo, 0, 30);
}
2020-11-19 22:47:44 +01:00
2020-11-19 23:10:46 +01:00
$printextras = CommonUtils::getConfigValue($pdo, 'printextras', 0);
2020-11-19 23:12:18 +01:00
if (is_null($userid)) {
$userid = $this->getUserId();
}
2020-11-19 22:47:44 +01:00
2020-11-19 23:00:55 +01:00
$ids = trim($ids, ",");
2020-11-19 22:47:44 +01:00
2020-11-19 23:00:55 +01:00
$ids_array = explode ( ',', $ids );
2020-11-19 23:00:05 +01:00
if (CommonUtils::callPlugin($pdo, "createBill", "replace")) {
return;
}
CommonUtils::callPlugin($pdo, "createBill", "before");
2020-11-19 23:00:55 +01:00
if (!$calledInternally) {
$pdo->beginTransaction();
}
2020-11-19 22:47:44 +01:00
2020-11-19 23:12:22 +01:00
$idList = explode(",", $ids);
$allNumeric = true;
foreach($idList as $anId) {
if (!is_numeric($anId)) {
$allNumeric = false;
}
}
2020-11-19 22:47:44 +01:00
$allNotPaid = true;
2020-11-19 23:12:22 +01:00
if ($allNumeric) {
$sql = "SELECT count(id) as countid FROM %queue% WHERE paidtime is not null AND id in ($ids)";
$counts = CommonUtils::fetchSqlAll($pdo, $sql);
if ($counts[0]["countid"] > 0) {
2020-11-19 23:00:55 +01:00
$allNotPaid = false;
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:12:22 +01:00
} else {
$allNotPaid = false;
2020-11-19 22:47:44 +01:00
}
$billid = (-1);
if ($allNotPaid == true) {
$billid = -1;
2020-11-19 23:12:22 +01:00
2020-11-19 23:14:48 +01:00
$nextBillIds = Bill::getNextBillIds($pdo);
$maxId = $nextBillIds["maxid"];
$nextBilluid = $nextBillIds["nextbilluid"];
2020-11-19 22:47:44 +01:00
2020-11-19 22:58:20 +01:00
$newprevbrutto = 0;
$newprevnetto = 0;
2020-11-19 23:12:02 +01:00
if (!is_null($maxId) && ($maxId > 0)) {
$billid = intval($maxId) + 1;
2020-11-19 22:47:44 +01:00
} else {
$billid = 1;
}
if (!$this->commonUtils->verifyLastBillId($pdo, $billid)) {
2020-11-19 23:00:55 +01:00
if (!$calledInternally) {
echo json_encode(array("status" => "ERROR", "code" => ERROR_INCONSISTENT_DB, "msg" => ERROR_INCONSISTENT_DB_MSG));
$pdo->rollBack();
}
2020-11-19 22:47:44 +01:00
return;
} else {
$this->commonUtils->setLastBillIdInWorkTable($pdo, $billid);
}
if (is_null($tableid)) {
$tableid = 0;
}
2020-11-19 23:14:02 +01:00
$sql = "INSERT INTO %records% (date,userid,tableid,action) VALUES(?,?,?,?)";
CommonUtils::execSql($pdo, $sql, array($currentTime,$userid,($tableid == 0 ? null : $tableid),T_BILL));
$recordid = $pdo->lastInsertId();
2020-11-19 23:00:55 +01:00
$idlist = join("','",$ids_array);
2020-11-19 23:10:09 +01:00
$sql = "SELECT SUM(price) as brutto,ROUND(SUM(price/(1 + %queue%.tax/100.0)),6) as netto FROM %queue% WHERE id IN ('$idlist')";
2020-11-19 23:00:55 +01:00
$row = CommonUtils::getRowSqlObject($pdo, $sql, null);
$brutto = $row->brutto;
$netto = $row->netto;
2020-11-19 23:12:07 +01:00
$signature = CommonUtils::calcSignatureForBill($currentTime, $brutto, $netto, $userid);
2020-11-19 22:47:44 +01:00
2020-11-19 23:14:48 +01:00
$billInsertSql = "INSERT INTO `%bill%` (`id` ,`billuid`, `billdate`,`brutto`,`netto`,`prevbrutto`,`prevnetto`,`tableid`,`paymentid`,`userid`,`ref`,`tax`,`host`,`reservationid`,`guestinfo`,`intguestid`,`printextras`,`signature`) VALUES (?,?,?,?,?,?,?,?,?,?,NULL,NULL,?,?,?,?,?,?)";
2020-11-19 22:47:44 +01:00
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($billInsertSql));
2020-11-19 23:14:48 +01:00
$stmt->execute(array($billid,$nextBilluid,$currentTime,$brutto,$netto,$newprevbrutto,$newprevnetto,$tableid,$paymentId,$userid,$host,$reservationid,$guestinfo,$intguestid,$printextras,$signature));
2020-11-19 22:47:44 +01:00
2020-11-19 22:59:47 +01:00
for ($i=0;$i<count($ids_array);$i++) {
$queueid = $ids_array[$i];
2020-11-19 22:47:44 +01:00
if (is_numeric($queueid)) {
if ($declareready == 0) {
2020-11-19 22:59:47 +01:00
$updateSql = "UPDATE %queue% SET paidtime=?, billid=? WHERE id=?";
2020-11-19 22:47:44 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($updateSql));
$stmt->execute(array($currentTime,$billid,$queueid));
} else {
$updateSql = "UPDATE %queue% SET paidtime=?, billid=?,readytime=?,delivertime=? WHERE id=?";
$stmt = $pdo->prepare(DbUtils::substTableAlias($updateSql));
$stmt->execute(array($currentTime,$billid,$currentTime,$currentTime,$queueid));
}
$billProdsSql = "INSERT INTO `%billproducts%` (`queueid`,`billid`) VALUES ( ?,?)";
2020-11-19 22:59:47 +01:00
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($billProdsSql));
2020-11-19 22:47:44 +01:00
$stmt->execute(array($queueid,$billid));
2020-11-19 23:10:26 +01:00
2020-11-19 23:14:02 +01:00
$sql = "INSERT INTO %recordsqueue% (recordid,queueid) VALUES(?,?)";
CommonUtils::execSql($pdo, $sql, array($recordid,$queueid));
2020-11-19 22:47:44 +01:00
}
}
2020-11-19 23:00:58 +01:00
Hotelinterface::insertIntoHsin($pdo,$billid);
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:14:48 +01:00
Bill::setPositionsOfProductsOnBillProductsTable($pdo, $billid);
$status = Bill::signOrdersBill($pdo, $billid,false);
if ($status["status"] != "OK") {
CommonUtils::log($pdo, "QUEUE", "Bill creation failed due to TSE error for bill with id=$billid from user $userid");
if (!$calledInternally) {
$pdo->rollBack();
echo json_encode(array("status" => "ERROR", "msg" => "TSE Error"));
}
return false;
}
2020-11-19 23:00:55 +01:00
if (!$calledInternally) {
$pdo->commit();
}
2020-11-19 22:47:44 +01:00
2020-11-19 23:12:22 +01:00
$billInfo = array("billid" => $billid, "date" => $currentTime, "brutto" => $brutto);
2020-11-19 23:14:48 +01:00
2020-11-19 23:02:19 +01:00
// Rksv::signBill($pdo, $billid);
2020-11-19 23:12:02 +01:00
$austriaEnabled = CommonUtils::getConfigValue($pdo, "austria", 0);
2020-11-19 23:12:43 +01:00
$cbirdFolder = trim(CommonUtils::getConfigValue($pdo, 'cbirdfolder', ''));
if (($austriaEnabled == 1) && ($cbirdFolder != "")) {
$austriabind = CommonUtils::getConfigValue($pdo, 'austriabind', 0);
2020-11-19 23:12:02 +01:00
$idlist = join(",",$ids_array);
$sql = "SELECT count(productname) as mycount,productname,tax,price FROM %queue% WHERE id in ($idlist) GROUP BY productname, price, tax";
$cbirdEntries = CommonUtils::fetchSqlAll($pdo, $sql);
2020-11-19 23:12:43 +01:00
if ($austriabind == 1) {
// 0=nothing, 1 = cbird, 2=QRK (R2B), 3=QRK(Receipt) - hier 1=cbird was chosen
if ($paymentId == 1) {
$paymentText = "Bar";
} else {
$sql = "SELECT name from %payment% WHERE id=?";
$r = CommonUtils::fetchSqlAll($pdo, $sql, array($paymentId));
$paymentText = $r[0]["name"];
}
$positionen = array();
foreach ($cbirdEntries as $aPosition) {
$priceInCent = $aPosition["price"] * 100;
$aPos = array("bezeichnung" => $aPosition["productname"],
"menge" => intval($aPosition["mycount"]),
"einzelpreis" => intval($priceInCent),
"ust" => intval($aPosition["tax"]));
$positionen[] = $aPos;
}
$cbirdEntry = array("zahlungsmittel" => $paymentText, "positionen" => $positionen);
$cbirdEntryJson = json_encode($cbirdEntry);
$currentTime = date('Y-m-d');
$longid = str_pad($billid, 10, '0', STR_PAD_LEFT);
2020-11-19 23:12:02 +01:00
$filename = $cbirdFolder . "/" . $currentTime . "-" . $longid . ".json";
file_put_contents($filename, $cbirdEntryJson);
2020-11-19 23:12:43 +01:00
2020-11-19 23:12:46 +01:00
} else if (($austriabind == 2) || ($austriabind == 3)) {
self::sendBillToQRK($pdo, $paymentId, $billid);
2020-11-19 23:12:02 +01:00
}
}
2020-11-19 23:00:05 +01:00
CommonUtils::callPlugin($pdo, "createBill", "after");
2020-11-19 23:00:18 +01:00
CommonUtils::log($pdo, "QUEUE", "Created bill with id=$billid from user $userid");
2020-11-19 23:00:55 +01:00
if (!$calledInternally) {
echo json_encode(array("status" => "OK", "msg" => $billInfo));
2020-11-19 23:12:18 +01:00
} else {
return $billid;
2020-11-19 23:00:55 +01:00
}
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:12:22 +01:00
2020-11-19 23:12:46 +01:00
public static function sendBillToQRK($pdo,$paymentId,$billid) {
$austriaEnabled = CommonUtils::getConfigValue($pdo, "austria", 0);
$cbirdFolder = trim(CommonUtils::getConfigValue($pdo, 'cbirdfolder', ''));
if (($austriaEnabled == 0) || ($cbirdFolder == "")) {
return;
}
if (!self::isPaymentSupportedByQRK($paymentId)) {
return;
}
$austriabind = CommonUtils::getConfigValue($pdo, 'austriabind', 0);
if ($austriabind == 2) {
self::sendBillToQRKAsR2B($pdo, $paymentId, $billid);
} else if ($austriabind == 3) {
self::sendBillToQRKAsReceipt($pdo, $paymentId, $billid);
}
}
private static function sendBillToQRKAsReceipt($pdo,$paymentId,$billid) {
$cbirdFolder = trim(CommonUtils::getConfigValue($pdo, 'cbirdfolder', ''));
$qrkPaidby = strval(self::getPayedByForQRK($paymentId));
$positionen = array();
$sql = "SELECT count(queueid) as mycount,Q.id,productname,tax,price FROM %queue% Q,%billproducts% BP where ";
$sql .= " BP.queueid=Q.id AND ";
$sql .= " BP.billid=? ";
$sql .= " GROUP BY productname,tax,price";
$entries = CommonUtils::fetchSqlAll($pdo, $sql, array($billid));
foreach ($entries as $aPosition) {
$thePrice = $aPosition["price"];
$thePriceFormatted = number_format($thePrice, 2, '.', '');
$theTax = $aPosition["tax"];
$theTaxFormatted = number_format($theTax, 2, '.', '');
$aPos = array("count" => strval($aPosition["mycount"]),
"name" => $aPosition["productname"],
"gross" => $thePriceFormatted,
"tax" => $theTaxFormatted);
$positionen[] = $aPos;
}
$qrkbill = array("customerText" => "","payedBy" => $qrkPaidby,"items" => $positionen);
$qrkreceipt = array("receipt" => array($qrkbill));
$qrkfilecontent = json_encode($qrkreceipt);
$filename = $cbirdFolder . "/QRK_Receipt_" . $billid . ".json";
file_put_contents($filename, $qrkfilecontent);
}
private static function sendBillToQRKAsR2B($pdo,$paymentId,$billid) {
$cbirdFolder = trim(CommonUtils::getConfigValue($pdo, 'cbirdfolder', ''));
$qrkPaidby = strval(self::getPayedByForQRK($paymentId));
$bruttoDb = CommonUtils::fetchSqlAll($pdo, "SELECT brutto FROM %bill% WHERE id=?", array($billid));
$brutto = $bruttoDb[0]["brutto"];
$bruttoFormatted = number_format($brutto, 2, '.', '');
$qrkPaidby = strval(self::getPayedByForQRK($paymentId));
$qrkbill = array("receiptNum" => strval($billid),
"gross" => "$bruttoFormatted",
"payedBy" => "$qrkPaidby",
"customerText" => "");
$qrkcontainer = array("r2b" => array($qrkbill));
$qrkfilecontent = json_encode($qrkcontainer);
$filename = $cbirdFolder . "/QRK_R2B_" . $billid . ".json";
file_put_contents($filename, $qrkfilecontent);
}
2020-11-19 23:12:43 +01:00
private static function isPaymentSupportedByQRK($paymentid) {
if (($paymentid == 1) || ($paymentid == 2) || ($paymentid == 3)) {
return true;
} else {
return false;
}
}
private static function getPayedByForQRK($paymentid) {
if ($paymentid == 1) {
return 0;
} else if ($paymentid == 2) {
return 1;
} else if ($paymentid == 3) {
return 2;
}
return 0;
}
2020-11-19 22:59:47 +01:00
private function getUserId() {
if(session_id() == '') {
session_start();
}
return $_SESSION['userid'];
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:10:09 +01:00
}