ordersprinter/webapp/php/queuecontent.php

2547 lines
92 KiB
PHP

<?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;
public static $INTERNAL_CALL_NO = false;
public static $INTERNAL_CALL_YES = true;
public static $lastSettingOfDisplayMode = 'all';
function __construct() {
$this->dbutils = new DbUtils();
$this->commonUtils = new CommonUtils();
$this->userrights = new Userrights();
}
function handleCommand($command) {
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
if ($command == "getJsonTableNameFromId") {
$this->getJsonTableNameFromId($_GET['tableid']);
return;
}
if ($command == "addAndCashProdByRestCall") {
$pdo = DbUtils::openDbAndReturnPdoStatic();
$this->addAndCashProdByRestCall($pdo, $_POST);
return;
}
// these command are only allowed for user with supply rights
$cmdArray = array('getJsonAllPreparedProducts', 'getJsonLastDeliveredProducts', 'declareProductBeDelivered', 'declareMultipleProductsDelivered','declareProductNotBeDelivered');
if (in_array($command, $cmdArray)) {
if (!($this->userrights->hasCurrentUserRight('right_supply'))) {
echo json_encode(array("status" => "ERROR","msg" => "Fehlende Benutzerrechte"));
return false;
}
}
// these command are only allowed for user with kitchen or bar rights
$cmdArray = array('declareProductBeCookingOrCooked', 'declareProductNOTBeCooked');
if (in_array($command, $cmdArray)) {
if (!($this->userrights->hasCurrentUserRight('right_kitchen')) && !($this->userrights->hasCurrentUserRight('right_bar'))) {
echo "Benutzerrechte nicht ausreichend!";
return false;
}
}
// these command are only allowed for user with waiter rights
$cmdArray = array('addProductListToQueue', 'removeProductFromQueue', 'changeTable','getProdsForTableChange');
if (in_array($command, $cmdArray)) {
if (!($this->userrights->hasCurrentUserRight('right_waiter'))) {
echo "Benutzerrechte nicht ausreichend!";
return false;
}
}
// these command are only allowed for user with paydesk rights
$cmdArray = array('getJsonProductsOfTableToPay', 'declarePaidCreateBillReturnBillId');
if (in_array($command, $cmdArray)) {
if (!($this->userrights->hasCurrentUserRight('right_paydesk'))) {
echo json_encode(array("status" => "ERROR", "code" => ERROR_PAYDESK_NOT_AUTHOTRIZED, "msg" => ERROR_PAYDESK_NOT_AUTHOTRIZED_MSG));
return false;
}
}
if ($command == 'addProductListToQueue') {
$this->addProductListToQueue($_POST["tableid"],$_POST["prods"],$_POST["print"],$_POST["payprinttype"]);
} else if ($command == 'getRecords') {
$pdo = DbUtils::openDbAndReturnPdoStatic();
$this->getRecords($pdo,$_GET["tableid"]);
} else if ($command == 'getAllTableRecords') {
$this->getAllTableRecords();
} else if ($command == 'getAllOrders') {
$this->getAllOrders();
} 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"]);
} else if ($command == 'getUnpaidTables') {
$this->getUnpaidTables();
} else if ($command == 'getProdsForTableChange') {
$this->getProdsForTableChange($_GET['tableId']);
} else if ($command == 'changeTable') {
$this->changeTable($_POST['fromTableId'],$_POST['toTableId'],$_POST['queueids']);
} 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') {
$pdo = DbUtils::openDbAndReturnPdoStatic();
$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);
} else {
echo "Command not supported.";
}
}
private static function setNewProductsToServe($pdo,$val) {
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) {
$sql = "SELECT count(id) as countid FROM %work% WHERE item=?";
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($item));
if ($row->countid == 0) {
$sql = "INSERT INTO %work% (item,value,signature) VALUES (?,?,?)";
CommonUtils::execSql($pdo, $sql, array($item,$val,null));
} else {
$sql = "UPDATE %work% SET value=? WHERE item=?";
CommonUtils::execSql($pdo, $sql, array($val,$item));
}
}
private static function getWorkItemFlag($pdo,$item) {
$sql = "SELECT count(id) as countid FROM %work% WHERE item=?";
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($item));
if ($row->countid == 0) {
return 0;
} else {
$sql = "SELECT value FROM %work% WHERE item=?";
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($item));
return $row->value;
}
}
function getJsonTableNameFromId($tableid) {
$pdo = DbUtils::openDbAndReturnPdoStatic();
$commonUtils = new CommonUtils();
echo json_encode($commonUtils->getTableNameFromId($pdo,$tableid));
}
function getUserName($userid) {
$pdo = $this->dbutils->openDbAndReturnPdo();
if (is_null($userid)) {
return "";
}
$sql = "SELECT username FROM %user% WHERE id=?";
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($userid));
if ($row != null) {
return($row->username);
} else {
return "";
}
}
private function areBillExisting($pdo) {
$sql = "SELECT count(id) as countid FROM %bill%";
$row = CommonUtils::getRowSqlObject($pdo, $sql, null);
$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) {
date_default_timezone_set(DbUtils::getTimeZone());
$currentTime = date('Y-m-d H:i:s');
$pdo = DbUtils::openDbAndReturnPdoStatic();
Guestsync::sync($pdo);
$unit = CommonUtils::caseOfSqlUnitSelection($pdo);
if ($this->areBillExisting($pdo)) {
$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% ";
} else {
$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% ";
}
$sql .= "WHERE (readytime IS NULL AND ";
$sql .= "Q.toremove='0' AND ";
$sql .= "Q.productid=%products%.id AND ";
$sql .= "Q.tablenr = %resttables%.id AND ";
$sql .= "%products%.category=%prodtype%.id AND ";
$sql .= "%prodtype%.kind=? AND ";
$sql .= "Q.isclosed is null AND ";
$sql .= "Q.workprinted='0') ";
if ($this->areBillExisting($pdo)) {
$sql .= "AND (Q.billid is null OR (";
$sql .= "Q.billid=%bill%.id AND %bill%.closingid is null)) ";
}
$sql .= "ORDER BY ordertime,longname,anoption,cooking DESC,Q.id";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($currentTime,$kind));
$result1 = $stmt->fetchAll();
$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 ";
$sql .= " ON Q.billid=b.id ";
$sql .= "WHERE (readytime IS NULL AND ";
$sql .= "Q.toremove='0' AND ";
$sql .= "Q.productid=%products%.id AND ";
$sql .= "Q.tablenr is null AND ";
$sql .= "%products%.category=%prodtype%.id AND ";
$sql .= "%prodtype%.kind=? AND ";
$sql .= "Q.isclosed is null AND ";
$sql .= "Q.workprinted='0') ";
$sql .= "AND (Q.billid is null OR ( ";
$sql .= "b.closingid is null)) ";
$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'],
"isguestorder" => $zeile['isguestorder'],
"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'],
"isguestorder" => $foundItem['isguestorder'],
"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;
}
}
}
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);
}
private function getExtrasOfQueueItemInTicketFormat($pdo,$queueid) {
$pureExtrasStrings = $this->getExtrasOfQueueItem($pdo, $queueid);
$extras = array();
foreach($pureExtrasStrings as $extraText) {
$extras[] = array("name" => $extraText);
}
return $extras;
}
private function getExtrasOfQueueItem($pdo,$queueid) {
if (is_null($pdo)) {
$pdo = DbUtils::openDbAndReturnPdoStatic();
}
$sql = "SELECT CONCAT(amount,'x ',name) as name FROM %queueextras% WHERE queueid=?";
$extras = CommonUtils::fetchSqlAll($pdo, $sql, array($queueid));
$extrasarr = array();
foreach($extras as $anExtra) {
$extrasarr[] = $anExtra["name"];
}
return $extrasarr;
}
private function getExtrasWithIdsOfQueueItem($pdo,$queueid) {
if (is_null($pdo)) {
$pdo = $this->dbutils->openDbAndReturnPdo();
}
$sql = "SELECT name,extraid,amount FROM %queueextras% WHERE queueid=?";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($queueid));
$extras = $stmt->fetchAll();
$extrasnames = array();
$extrasids = array();
$extrasamounts = array();
foreach($extras as $anExtra) {
$extrasnames[] = $anExtra["name"];
$extrasids[] = $anExtra["extraid"];
$extrasamounts[] = $anExtra["amount"];
}
return array("extrasnames" => $extrasnames,"extrasids" => $extrasids,"extrasamounts" => $extrasamounts);
}
private function getJobsToPrint($pdo,$kind,$printer,$queueIds) {
if (is_null($queueIds) || (count($queueIds) == 0)) {
return array();
}
$queueStr = implode(',',$queueIds);
$decpoint = CommonUtils::getConfigValue($pdo, 'decpoint', '.');
$groupWorkItems = 'groupworkitemsf';
if ($kind == 1) {
$groupWorkItems = 'groupworkitemsd';
}
$sql = "SELECT setting FROM %config% where name=?";
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($groupWorkItems));
$groupworkitems = $row->setting;
if (is_null($groupworkitems)) {
$groupworkitems = 1;
}
$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";
$queueItems = CommonUtils::fetchSqlAll($pdo, $sql, array($kind));
$jobs = array();
foreach($queueItems as $aQueueItem) {
$thePrinter = $aQueueItem["printer"];
$fixbind = $aQueueItem["fixbind"];
$queueid = $aQueueItem["id"];
$tableid = $aQueueItem["tableid"];
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;
}
}
}
if ($thePrinter == $printer) {
$extras = $this->getExtrasOfQueueItem($pdo,$queueid);
$formattedPrice = number_format($aQueueItem["price"], 2, $decpoint, '');
$theEntry = array(
"id" => $queueid,
"productid" => $aQueueItem["productid"],
"longname" => $aQueueItem["longname"],
"singleprod" => $aQueueItem["longname"],
"price" => $formattedPrice,
"togo" => $aQueueItem["togo"],
"anoption" => $aQueueItem["anoption"],
"kind" => $aQueueItem["kind"],
"printer" => $aQueueItem["printer"],
"extras" => $extras
);
if ($groupworkitems == 1) {
$this->grouping($jobs, $theEntry);
} else {
$jobs[] = $theEntry;
}
}
}
if ($groupworkitems == 1) {
$jobidsOfThisJob = array();
foreach($jobs as &$aJob) {
$aJob["singleprod"] = $aJob["longname"];
$cnt = $aJob["count"];
$aJob["longname"] = $cnt . "x " . $aJob["longname"];
$aJob["allqueueids"] = $aJob["queueids"];
}
}
return $jobs;
}
private function grouping(&$collection,$entry) {
$extrasTxt = join(",",$entry["extras"]);
$found = false;
foreach($collection as &$anEntry) {
if (($anEntry["longname"] == $entry["longname"]) && ($anEntry["price"] == $entry["price"]) && ($anEntry["togo"] == $entry["togo"]) && ($anEntry["anoption"] == $entry["anoption"]) && (join(",",$anEntry["extras"]) == $extrasTxt)) {
$found = true;
$anEntry["count"] = $anEntry["count"] + 1;
$anEntry["queueids"][] = $entry["id"];
}
}
if (!$found) {
$collection[] = array("id" => $entry["id"],
"productid" => $entry["productid"],
"longname" => $entry["longname"],
"singleprod" => $entry["singleprod"],
"printer" => $entry["printer"],
"anoption" => $entry["anoption"],
"price" => $entry["price"],
"togo" => $entry["togo"],
"kind" => $entry["kind"],
"extras" => $entry["extras"],
"count" => 1,
"queueids" => array($entry["id"])
);
}
}
private function filterKindQueueIds($pdo,$idArr,$kind) {
$retArr = array();
if (is_null($idArr) || (count($idArr) == 0)) {
return $retArr;
}
$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";
$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;
}
private function doWorkPrint($pdo,$theTableid,$insertedQueueIds,$username,$userid,$payPrintType,$lang,$declareReadyDelivered = true) {
$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)) {
$this->doWorkPrintCore($pdo,$theTableid,$foodIds,$username,$userid,$payPrintType,$lang,$declareReadyDelivered);
}
if (($payPrintType != "s") || ($oneProdForEachWorkRecd == 0)) {
$this->doWorkPrintCore($pdo,$theTableid,$drinkIds,$username,$userid,$payPrintType,$lang,$declareReadyDelivered);
}
if ($payPrintType != 's') {
return;
}
if ($oneProdForEachWorkRecf == 1) {
foreach($foodIds as $aQueueId) {
$this->doWorkPrintCore($pdo,$theTableid,array($aQueueId),$username,$userid,$payPrintType,$lang,$declareReadyDelivered);
}
}
if ($oneProdForEachWorkRecd == 1) {
foreach($drinkIds as $aQueueId) {
$this->doWorkPrintCore($pdo,$theTableid,array($aQueueId),$username,$userid,$payPrintType,$lang,$declareReadyDelivered);
}
}
}
private function doWorkPrintCore($pdo,$theTableid,$insertedQueueIds,$username,$userid,$payPrintType,$lang,$declareReadyDelivered = true) {
$foodJobsPrinter1 = $this->getJobsToPrint($pdo, 0, 1, $insertedQueueIds);
$foodJobsPrinter2 = $this->getJobsToPrint($pdo, 0, 2, $insertedQueueIds);
$foodJobsPrinter3 = $this->getJobsToPrint($pdo, 0, 3, $insertedQueueIds);
$foodJobsPrinter4 = $this->getJobsToPrint($pdo, 0, 4, $insertedQueueIds);
$drinkJobsPrinter1 = $this->getJobsToPrint($pdo, 1, 1, $insertedQueueIds);
$drinkJobsPrinter2 = $this->getJobsToPrint($pdo, 1, 2, $insertedQueueIds);
$drinkJobsPrinter3 = $this->getJobsToPrint($pdo, 1, 3, $insertedQueueIds);
$drinkJobsPrinter4 = $this->getJobsToPrint($pdo, 1, 4, $insertedQueueIds);
if ($payPrintType == "s") {
$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);
}
if ($declareReadyDelivered) {
$printAndQueueJobs = CommonUtils::getConfigValue($pdo, "printandqueuejobs", 0);
if ($printAndQueueJobs == 0) {
$this->declareReadyAndDelivered($pdo, $insertedQueueIds);
}
}
$result = array_merge($foodJobsPrinter1,$foodJobsPrinter2,$drinkJobsPrinter1,$drinkJobsPrinter2);
return $result;
}
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));
}
}
private function createAWorkReceiptAndQueueWorkPrint($pdo,$jobs,$theTableid,$kind,$printer,$user,$userid,$lang) {
if (count($jobs) < 1) {
return;
}
date_default_timezone_set(DbUtils::getTimeZone());
$germanTime = date('H:i');
$resultarray = array();
foreach($jobs as $aJob) {
$queueId = $aJob["id"];
$extras = $this->getExtrasOfQueueItemInTicketFormat($pdo,$queueId);
$togo = "";
if ($aJob['togo'] == 1) {
$togo = "To-Go";
}
$arr = array("id" => $queueId,
"longname" => $aJob['longname'],
"singleprod" => $aJob['singleprod'],
"price" => $aJob['price'],
"togo" => $togo,
"option" => $aJob['anoption'],
"extras" => $extras,
"ordertime" => $germanTime,
"kind" => $kind,
"printer" => $printer
);
if (isset($aJob["allqueueids"])) {
$arr["allqueueids"] = $aJob["allqueueids"];
}
$resultarray[] = $arr;
}
if (is_null($theTableid) || ($theTableid == 0)) {
$takeAwayStr = array("Zum Mitnehmen","Take away","Para llevar");
$tablename = $takeAwayStr[$lang];
} else {
$sql = "SELECT tableno,%room%.abbreviation FROM %resttables%,%room% WHERE %resttables%.id=? AND %resttables%.roomid=%room%.id";
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($theTableid));
if (is_null($row->abbreviation) || ($row->abbreviation == '')) {
$tablename = $row->tableno;
} else {
$tablename = $row->abbreviation . "-" . $row->tableno;
}
}
$isTogo = false;
if (is_null($theTableid)) {
$isTogo = true;
}
PrintQueue::queueWorkPrintJob($pdo, $tablename, $germanTime, $resultarray, $kind, $printer, $user,$userid,$isTogo);
}
private function getJsonLastMadeItems($kind) {
$pdo = DbUtils::openDbAndReturnPdoStatic();
$unit = CommonUtils::caseOfSqlUnitSelection($pdo);
if ($this->areBillExisting($pdo)) {
$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% ";
} else {
$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% ";
}
$sql .= "WHERE (readytime IS NOT NULL AND ";
$sql .= "delivertime IS NULL AND ";
$sql .= "Q.toremove = '0' AND ";
$sql .= "Q.productid=%products%.id AND ";
$sql .= "Q.tablenr = %resttables%.id AND ";
$sql .= "%products%.category=%prodtype%.id AND ";
$sql .= "%prodtype%.kind=? AND ";
$sql .= "Q.isclosed is null AND ";
$sql .= "Q.workprinted='0') ";
if ($this->areBillExisting($pdo)) {
$sql .= "AND (Q.billid is null OR (";
$sql .= "Q.billid=%bill%.id AND %bill%.closingid is null)) ";
}
$sql .= "ORDER BY readytime DESC LIMIT 10;";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($kind));
$result1 = $stmt->fetchAll();
if ($this->areBillExisting($pdo)) {
$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% ";
} else {
$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% ";
}
$sql .= "WHERE (readytime IS NOT NULL AND ";
$sql .= "delivertime IS NULL AND ";
$sql .= "Q.toremove = '0' AND ";
$sql .= "Q.productid=%products%.id AND ";
$sql .= "Q.tablenr is null AND ";
$sql .= "%products%.category=%prodtype%.id AND ";
$sql .= "%prodtype%.kind=? AND ";
$sql .= "Q.isclosed is null AND ";
$sql .= "Q.workprinted='0') ";
if ($this->areBillExisting($pdo)) {
$sql .= "AND (Q.billid is null OR (";
$sql .= "Q.billid=%bill%.id AND %bill%.closingid is null)) ";
}
$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);
$resultarray = array();
foreach($result as $zeile) {
$extras = $this->getExtrasOfQueueItem($pdo,$zeile['id']);
$productid = $zeile['prodid'];
$useConditions = $this->getUseKitchenAndSupplyForProd($pdo,$productid);
if ($useConditions["usekitchen"] == 1) {
$arr = array("id" => $zeile['id'],
"tablename" => $zeile['tableno'],
"longname" => $zeile['longname'],
"option" => $zeile['anoption'],
"extras" => $extras,
"readytime" => $zeile['readytime']
);
$resultarray[] = $arr;
}
}
$resultarray = $this->appendProdsForBarKitchenAndAutoDelivery($pdo,$kind, $resultarray);
echo json_encode($resultarray);
}
private function appendProdsForBarKitchenAndAutoDelivery($pdo,$kind,$resultarray) {
$unit = CommonUtils::caseOfSqlUnitSelection($pdo);
$sql = "SELECT DISTINCT Q.id as id,tableno as tablename,Q.unit,CONCAT($unit,Q.productname) as longname,delivertime,anoption,p.id as prodid ";
$sql .= "FROM %queue% Q ";
$sql .= "LEFT JOIN %bill% b ON Q.billid=b.id ";
$sql .= "INNER JOIN %products% p ON Q.productid=p.id ";
$sql .= "INNER JOIN %prodtype% t ON p.category=t.id AND t.kind=? AND t.usesupplydesk='0' AND t.usekitchen='1' ";
$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";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($kind));
$result = $stmt->fetchAll();
foreach ($result as $zeile) {
$extras = $this->getExtrasOfQueueItem($pdo,$zeile['id']);
$deliveredProd = array(
"id" => $zeile['id'],
"tablename" => $zeile['tablename'],
"longname" => $zeile['longname'],
"option" => $zeile['anoption'],
"extras" => $extras,
"readytime" => $zeile['delivertime']
);
$resultarray[] = $deliveredProd;
}
return($resultarray);
}
function getTableIdOfQueue($pdo,$queueid) {
$sql = "SELECT tablenr as tableid FROM %queue% WHERE id=?";
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($queueid));
return $row->tableid;
}
/*
* Kitchen can delare a product as being cooked
*/
function declareProductBeCookingOrCooked($queueid,$action) {
if (is_numeric($queueid)) {
$pdo = DbUtils::openDbAndReturnPdoStatic();
$pdo->beginTransaction();
$sql = "SELECT cooking,productid FROM %queue% WHERE id=?";
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($queueid));
if ($row != null) {
$cooking = $row->cooking;
$productid = $row->productid;
if ($action == 'r') {
self::reallyDeclareAsCooked($pdo,$queueid);
$useConditions = $this->getUseKitchenAndSupplyForProd($pdo,$productid);
if ($useConditions["usesupply"] == 0) {
self::declareProductBeDeliveredWithGivenPdo($pdo,$queueid);
} else {
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;
}
$this->doWorkPrint($pdo,$theTableid,array($queueid),$_SESSION['currentuser'],$_SESSION['userid'],$payprinttype, $_SESSION['language'],false);
}
$pdo->commit();
echo json_encode(array("status" => "OK"));
} else if ($action == 'c') {
if (!is_null($cooking)) {
$pdo->rollBack();
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=?";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($updSql));
$stmt->execute(array($userid,$queueid));
$pdo->commit();
echo json_encode(array("status" => "OK"));
}
}
} else {
$pdo->rollBack();
}
} else {
echo json_encode(array("status" => "ERROR", "code" => ERROR_GENERAL_ID_TYPE, "msg" => ERROR_GENERAL_ID_TYPE_MSG));
}
}
private static function reallyDeclareAsCooked($pdo,$queueid) {
date_default_timezone_set(DbUtils::getTimeZone());
$readytime = date('Y-m-d H:i:s');
$insertSql = "UPDATE %queue% SET readytime=? WHERE id=?";
CommonUtils::execSql($pdo, $insertSql, array($readytime,$queueid));
}
/*
* Product is not cooked (undo of kitchen)
*/
function declareProductNotBeCooked($queueid) {
if (is_numeric($queueid)) {
$pdo = $this->dbutils->openDbAndReturnPdo();
$pdo->beginTransaction();
$sql = "SELECT id FROM %queue% WHERE id=? AND readytime IS NOT NULL";
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($queueid));
if ($row != null) {
$foundid = $row->id;
if ($foundid == $queueid) {
$sql = "UPDATE %queue% SET readytime=?, delivertime=?, cooking=NULL WHERE id=?";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array(null,null,$queueid));
$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));
}
} else {
echo json_encode(array("status" => "ERROR", "code" => ERROR_GENERAL_ID_TYPE, "msg" => ERROR_GENERAL_ID_TYPE_MSG));
}
}
private function findCategoryOfProd($pdo,$prodid) {
$sql = "SELECT category FROM %products% WHERE id=?";
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($prodid));
return $row->category;
}
private function getUseKitchenAndSupplyForProdInCat($pdo,$catid) {
$sql = "SELECT usekitchen, usesupplydesk FROM %prodtype% WHERE id=?";
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($catid));
return array("usekitchen" => $row->usekitchen, "usesupply" => $row->usesupplydesk);
}
private function getUseKitchenAndSupplyForProd($pdo,$prodid) {
$catid = $this->findCategoryOfProd($pdo,$prodid);
return $this->getUseKitchenAndSupplyForProdInCat($pdo,$catid);
}
private static function getUseKitchenAndSupplyForProdWithPdo($pdo,$prodid) {
$sql = "SELECT usekitchen, usesupplydesk FROM %prodtype%,%products% WHERE %products%.category=%prodtype%.id AND %products%.id=?";
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($prodid));
if ($row != null) {
return array("usekitchen" => $row->usekitchen, "usesupply" => $row->usesupplydesk);
} else {
return array("usekitchen" => "1", "usesupply" => "1");
}
}
private function getAllOrders() {
if (!($this->userrights->hasCurrentUserRight('right_statistics')) ) {
echo json_encode(array("status" => "ERROR","msg" => "Benutzerrechte nicht ausreichend"));
return false;
}
$pdo = DbUtils::openDbAndReturnPdoStatic();
$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 ";
$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 ";
$sql .= " group by ordertime,productname,productid,price,toremove,ispaid,tablename,username ORDER BY ordertime,tablename,ispaid";
$allorders = CommonUtils::fetchSqlAll($pdo, $sql);
$sql = "SELECT COALESCE(SUM(price),'0.00') as sumprice FROM %queue% Q WHERE isclosed is null AND toremove=?";
$sumAllRes = CommonUtils::fetchSqlAll($pdo, $sql,array(0));
$sumAll = $sumAllRes[0]["sumprice"];
$sql = "SELECT COALESCE(SUM(price),'0.00') as sumprice FROM %queue% Q WHERE isclosed is null AND toremove=? AND billid is null";
$sumUnpaidRes = CommonUtils::fetchSqlAll($pdo, $sql,array(0));
$sumUnpaid = $sumUnpaidRes[0]["sumprice"];
$sql = "SELECT COALESCE(SUM(price),'0.00') as sumprice FROM %queue% Q WHERE isclosed is null AND toremove=? AND billid is not null";
$sumPaidRes = CommonUtils::fetchSqlAll($pdo, $sql,array(0));
$sumPaid = $sumPaidRes[0]["sumprice"];
$sql = "SELECT COALESCE(SUM(price),'0.00') as sumprice FROM %queue% Q WHERE isclosed is null AND toremove=? AND billid is null";
$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));
}
function getAllTableRecords() {
if (!($this->userrights->hasCurrentUserRight('right_statistics')) ) {
echo json_encode(array("status" => "ERROR","msg" => "Benutzerrechte nicht ausreichend"));
return false;
}
$pdo = DbUtils::openDbAndReturnPdoStatic();
$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) {
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;
}
$sql = "SELECT extraid,CONCAT(amount,'x ',name) as name FROM %queueextras% WHERE queueid=?";
$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);
}
return array("status" => "OK","msg" => $records);
}
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%` (
`id` , `tablenr`,`productid`,`pricelevel`,`price`,`pricetype`,`tax`,`taxaustria`,`productname`,`ordertime`,`orderuser`,`anoption`,`pricechanged`,`togo`,`readytime`,`delivertime`,`paidtime`,`billid`,`toremove`,`cooking`,`workprinted`)
VALUES (
NULL , ?,?,?,?,?,?,?,?,?,?,?,?,?, null, null, NULL,NULL,'0',NULL,'0');";
try {
$stmt = $pdo->prepare(DbUtils::substTableAlias($insertSql));
$stmt->execute(array(null,$prodid,1,$price,0,$tax,null,$productname,$ordertime,$userid,'',1,0));
$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();
$billid = $this->declarePaidCreateBillReturnBillId($pdo, $queueid, 0, 1, 1, 0, self::$INTERNAL_CALL_YES, '', '', '',$userid);
if ($printer >= 0) {
PrintQueue::internalQueueReceiptPrintjob($pdo, $billid, $printer);
}
echo json_encode(array("status" => "OK"));
}
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();
}
$quickcash = $_SESSION['quickcash'];
$pdo = DbUtils::openDbAndReturnPdoStatic();
$ret = $this->addProductListToQueueCore($pdo, $ordertime, $theTableid, $prods, $doPrint, $payprinttype,$_SESSION['userid'],$quickcash);
echo json_encode($ret);
}
/*
* 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
*/
public function addProductListToQueueCore($pdo,$ordertime,$theTableid,$prods,$doPrint,$payprinttype,$userid,$quickcash) {
if (intval($theTableid) == 0) {
$theTableid = null; // togo room
}
$printAndQueueJobs = CommonUtils::getConfigValue($pdo, "printandqueuejobs", 0);
if ($printAndQueueJobs == 1) {
$doPrint = 1;
}
$pdo->beginTransaction();
$workflowconfig = CommonUtils::getExistingConfigValue($pdo, 'workflowconfig');
$austria = CommonUtils::getExistingConfigValue($pdo, 'austria');
$commUtils = new CommonUtils();
$currentPriceLevel = $commUtils->getCurrentPriceLevel($pdo);
$currentPriceLevelId = $currentPriceLevel["id"];
$insertedQueueIdsForPrint = array();
$insertedQueueIdsTotal = array();
$sql = "INSERT INTO %records% (date,userid,tableid,action) VALUES(?,?,?,?)";
CommonUtils::execSql($pdo, $sql, array($ordertime,$userid,$theTableid,T_ORDER));
$recordid = $pdo->lastInsertId();
$tseEntries = array();
$queueitemids = array();
for ($i=0;$i<count($prods);$i++) {
$aProd = $prods[$i];
$productid = intval($aProd["prodid"]);
$theOption = $aProd["option"];
$theChangedPrice = $aProd["changedPrice"];
$theChangedPrice = str_replace(',','.',$theChangedPrice);
$unitamount = $aProd["unitamount"];
$getPriceSql = "SELECT priceA,priceB,priceC,longname,tax as prodtaxkey,togotax as prodtogotaxkey,taxaustria,amount,COALESCE(unit,0) as unit FROM %products% where id=?";
$row = CommonUtils::getRowSqlObject($pdo, $getPriceSql, array($productid));
if ($row == null) {
echo "Fehler: Preise nicht vorhanden"; // error
return;
}
if (!is_null($row->amount)) {
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);
}
}
$productname = $row->longname;
$unit = intval($row->unit);
$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;
if (($theChangedPrice == "NO") || (!is_numeric($theChangedPrice))) {
$price = $basePrice;
} else {
if ($theChangedPrice < $basePrice) {
$priceType = DBUtils::$PRICE_TYPE_DICOUNT;
} else if ($theChangedPrice > $basePrice) {
$priceType = DbUtils::$PRICE_TYPE_EXTRA_AMOUNT;
}
$price = $theChangedPrice;
}
$togo = $aProd["togo"];
$taxkeytouse = $prodtaxkey;
if (is_null($theTableid) || ($togo == 1)) {
$taxkeytouse = $prodtogotaxkey;
}
$taxToUse = CommonUtils::getTaxFromKey($pdo,$taxkeytouse);
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;
}
$extras = null;
if (array_key_exists("extras",$aProd)) {
$extras = $aProd["extras"];
}
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"];
$extraamount = $anExtra["amount"];
$sql = "SELECT price FROM %extras% WHERE id=?";
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($extraid));
$price += floatval($row->price * $extraamount);
}
}
}
if (is_numeric($unit) && ($unit > 1) && ($unit < 8)) {
if (!is_numeric($unitamount)) {
$pdo->rollBack();
return(array("status" => "ERROR", "msg" => "Mengenangabe nicht numerisch"));
} else {
$price = $price * $unitamount;
}
}
if (is_null($theTableid) || (is_numeric($theTableid) && is_numeric($productid))) {
$useConditions = self::getUseKitchenAndSupplyForProdWithPdo($pdo,$productid);
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";
}
}
$insertSql = "INSERT INTO `%queue%` (
`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`)
VALUES (
NULL , ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, null, null, NULL,NULL,'0',NULL,'0')";
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));
$queueid = $pdo->lastInsertId();
$sql = "INSERT INTO %recordsqueue% (recordid,queueid) VALUES(?,?)";
CommonUtils::execSql($pdo, $sql, array($recordid,$queueid));
if ($unit == 0) {
$orderAmount = 1;
} else {
$amountAsDouble = doubleval($unitamount);
$orderAmount = strval($amountAsDouble);
}
$quotedProdname = self::quoteTseName($productname);
$tseEntries[] = $orderAmount . ";\"" . $quotedProdname . "\";" . $price;
$queueitemids[] = $queueid;
if (!is_null($extras) && ($extras != "")) {
for ($j=0;$j<count($extras);$j++) {
$anExtra = $extras[$j];
$extraid = $anExtra["id"];
$extraname = $anExtra["name"];
$extraamount = $anExtra["amount"];
$sql = "INSERT INTO %queueextras% (`queueid`,`extraid`,`amount`,`name`) VALUES(?,?,?,?)";
CommonUtils::execSql($pdo, $sql, array($queueid,$extraid,$extraamount,$extraname));
}
}
if (($workflowconfig == DbUtils::$WORKFLOW_WORK_WITH_SERVER) && ($doPrint == DbUtils::$WORKFLOW_DIGITAL_AND_WORK)) {
self::reallyDeclareAsCooked($pdo,$queueid);
self::declareProductBeDeliveredWithGivenPdo($pdo,$queueid);
} else {
$insertedQueueIdsTotal[] = $queueid;
if ($useConditions["usekitchen"] == 0) {
self::reallyDeclareAsCooked($pdo,$queueid);
if ($useConditions["usesupply"] == 0) {
self::declareProductBeDeliveredWithGivenPdo($pdo,$queueid);
}
} else {
$insertedQueueIdsForPrint[] = $queueid;
if (($printAndQueueJobs == 0) && ($doPrint == 0)) {
self::setFlagForCooking($pdo,$productid);
}
}
}
}
}
$tseEntriesLine = implode("\n", $tseEntries);
$signStatus = self::signAtTSE($pdo, $tseEntriesLine, $queueitemids,false);
if ($signStatus["status"] != "OK") {
$pdo->rollBack();
return(array("status" => "ERROR","msg" => $signStatus["msg"]));
}
$generalQuickcash = CommonUtils::getConfigValue($pdo, "cashenabled", 1);
if (($generalQuickcash == 1) || ($quickcash == 1)) {
$idStr = join(',',$insertedQueueIdsTotal);
$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);
}
}
if ($doPrint == 1) {
if ($payprinttype == "s") {
$this->doWorkPrint($pdo,$theTableid,$insertedQueueIdsForPrint,$_SESSION['currentuser'],$userid,$payprinttype, $_SESSION['language']);
$pdo->commit();
return(array("status" => "OK"));
} else {
$result = $this->doWorkPrint($pdo,$theTableid,$insertedQueueIdsForPrint,$_SESSION['currentuser'],$userid,$payprinttype, $_SESSION['language']);
$pdo->commit();
return(array("status" => "OK", "msg" => $result));
}
}
$pdo->commit();
return(array("status" => "OK","queueids" => $insertedQueueIdsTotal));
}
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) {
$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"]);
}
$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");
}
}
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);
}
}
}
}
function addProductListToQueueForGuest($pdo,$ordertime,$theTableid,$prodid,$doPrint) {
if(session_id() == '') {
session_start();
}
date_default_timezone_set(DbUtils::getTimeZone());
$pdo->beginTransaction();
$workflowconfig = CommonUtils::getExistingConfigValue($pdo, 'workflowconfig');
$commUtils = new CommonUtils();
$currentPriceLevel = $commUtils->getCurrentPriceLevel($pdo);
$currentPriceLevelId = $currentPriceLevel["id"];
$insertedQueueIds = array();
$sql = "INSERT INTO %records% (date,userid,tableid,action) VALUES(?,?,?,?)";
CommonUtils::execSql($pdo, $sql, array($ordertime,null,$theTableid,T_ORDER));
$recordid = $pdo->lastInsertId();
$productid = $prodid;
$theOption = '';
$theChangedPrice = "NO";
$getPriceSql = "SELECT priceA,priceB,priceC,longname,tax as prodtaxkey,taxaustria,amount FROM %products% where id=?";
$row = CommonUtils::getRowSqlObject($pdo, $getPriceSql, array($productid));
if ($row == null) {
echo "Fehler: Preise nicht vorhanden"; // error
return false;
}
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
$taxkeytouse = $row->prodtaxkey;
$taxToUse = CommonUtils::getTaxFromKey($pdo,$taxkeytouse);
$taxaustrianumber = null;
if (is_null($theTableid) || (is_numeric($theTableid) && is_numeric($productid))) {
$useConditions = self::getUseKitchenAndSupplyForProdWithPdo($pdo,$productid);
$insertSql = "INSERT INTO `%queue%` (
`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')";
$stmt = $pdo->prepare(DbUtils::substTableAlias($insertSql));
$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();
$sql = "INSERT INTO %recordsqueue% (recordid,queueid) VALUES(?,?)";
CommonUtils::execSql($pdo, $sql, array($recordid,$queueid));
$quotedProdname = self::quoteTseName($productname);
$tseEntries = "1;\"" . $quotedProdname . "\";" . $price;
if (($workflowconfig == DbUtils::$WORKFLOW_DIGITAL_AND_WORK) && ($doPrint == DbUtils::$WORKFLOW_WORK_WITH_SERVER)) {
self::reallyDeclareAsCooked($pdo,$queueid);
self::declareProductBeDeliveredWithGivenPdo($pdo,$queueid);
} else {
if ($useConditions["usekitchen"] == 0) {
self::reallyDeclareAsCooked($pdo,$queueid);
if ($useConditions["usesupply"] == 0) {
self::declareProductBeDeliveredWithGivenPdo($pdo,$queueid);
}
} else {
$insertedQueueIds[] = $queueid;
}
}
$signStatus = self::signAtTSE($pdo, $tseEntries, array($queueid),false);
if ($signStatus["status"] != "OK") {
error_log("Signing guest order at TSE impossible. Ignore guest order");
$pdo->rollBack();
return false;
}
}
if ($doPrint == 1) {
$this->doWorkPrint($pdo,$theTableid,$insertedQueueIds,'Gastbestellung',0,'s', $_SESSION['language']);
}
$pdo->commit();
return true;
}
/*
* 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
* toremove = 1 (was never ordered...)
*/
function removeProductFromQueue($queueid,$isPaid,$isCooking,$isReady) {
if (is_numeric($queueid)) {
$pdo = DbUtils::openDbAndReturnPdoStatic();
$pdo->beginTransaction();
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"];
$sql = "SELECT count(id) as countid FROM %bill%";
$row = CommonUtils::getRowSqlObject($pdo, $sql, null);
$hasBills = ($row->countid > 0 ? true : false);
if ($hasBills) {
$sql = "UPDATE %queue%,%bill% ";
} else {
$sql = "UPDATE %queue% ";
}
$sql .= "SET %queue%.toremove='1' WHERE %queue%.id=? AND %queue%.toremove='0' ";
if ($isPaid == '1') {
$sql .= " AND paidtime IS NOT NULL ";
} else {
$sql .= " AND (paidtime IS NULL or paidtime is null) ";
}
Vouchers::handleRemoveQueueItem($pdo,$queueid);
if ($isCooking == '1') {
$sql .= " AND cooking is not null ";
} else {
$sql .= " AND cooking is null ";
}
if ($isReady == '1') {
$sql .= " AND readytime IS NOT NULL ";
} else {
$sql .= " AND (readytime IS NULL or readytime is null) ";
}
if ($hasBills) {
$sql .= " AND (billid is null OR (";
$sql .= " billid = %bill%.id AND %bill%.closingid is null)) ";
}
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($queueid));
$rowsAffected = $stmt->rowCount();
if ($rowsAffected == 1) {
$signRemoval = self::signRemovalOfQueueItem($pdo, $queueid);
if ($signRemoval["status"] != "OK") {
$pdo->rollBack();
return(array("status" => "ERROR","msg" => "TSE-Signierung fehlgeschlagen: " . $signRemoval["msg"]));
}
Workreceipts::createCancelWorkReceipt($pdo, $queueid);
$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));
$pdo->commit();
echo json_encode(array("status" => "OK"));
} else {
$pdo->rollBack();
echo json_encode(array("status" => "Failed", "msg" => "Affected rows: $rowsAffected"));
}
}
}
public static function signRemovalOfQueueItem($pdo,$queueid) {
$sql = "SELECT (IF(unit=0,ROUND(0-unitamount),0-unitamount)) as unitamount,unit,productname,price FROM %queue% WHERE id=?";
$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");
}
}
function getUnpaidTables() {
$pdo = DbUtils::openDbAndReturnPdoStatic();
$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";
$result = CommonUtils::fetchSqlAll($pdo, $sql, null);
$unpaidTables = array();
foreach ($result as $anUnpaidTable) {
$unpaidTables[] = $anUnpaidTable["tablename"];
}
$sql = "SELECT COUNT(id) as takeawayitems FROM %queue% WHERE tablenr is null AND paidtime is null AND %queue%.toremove='0' AND isclosed is null";
$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));
}
/*
* Return as JSON structure all products that are assigned to a specified table, with the
* specification that they are not delivered yet.
*
* toremove must not be 1, because = 1 means that is is paid but was cancelled later
* 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)) {
$prods = array();
$pdo = DbUtils::openDbAndReturnPdoStatic();
$sql = "SELECT count(id) as countid FROM %bill%";
$row = CommonUtils::getRowSqlObject($pdo, $sql, null);
if ($row->countid == 0) {
$sql = "SELECT DISTINCT %queue%.id as quid, ordertime FROM %queue% WHERE %queue%.toremove='0' AND isclosed is null AND ";
} else {
$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 ";
}
if ($tableid == 0) {
$sql .= "%queue%.tablenr is null ORDER BY ordertime, quid";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute();
} else {
$sql .= "%queue%.tablenr=? ORDER BY ordertime, quid";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($tableid));
}
$allNotClosedQueueIds = $stmt->fetchAll();
$resultids = array();
$sql = "SELECT count(id) as countid FROM %queue% WHERE %queue%.id=? AND (%queue%.delivertime IS NULL ";
$sql .= " OR %queue%.readytime IS NULL ";
$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();
$sql = "SELECT productid,readytime,paidtime,cooking,productname,anoption,productname,togo,pricechanged,price,unit,unitamount FROM %queue% WHERE id=? ORDER BY productid ";
$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';
if ($row->readytime == null) {
$isReady = "0"; // not yet prepared by the kitchen
}
if ($row->paidtime == null) {
$isPaid = "0"; // not yet paid
}
if (is_null($row->cooking)) {
$isCooking = '0';
}
$extras = $this->getExtrasWithIdsOfQueueItem($pdo,$anId);
$prodEntry = array(
"id" =>$anId,
"prodid" => $row->productid,
"longname" => $row->productname,
"option" => $row->anoption,
"pricechanged" => $row->pricechanged,
"togo" => $row->togo,
"price" => $row->price,
"unit" => $row->unit,
"unitamount" => $row->unitamount,
"extras" => $extras["extrasnames"],
"extrasids" => $extras["extrasids"],
"extrasamounts" => $extras["extrasamounts"],
"isready" => $isReady,
"isPaid" => $isPaid,
"isCooking" => $isCooking);
$prods[] = $prodEntry;
}
echo json_encode($prods);
}
}
function getProdsForTableChange($tableid) {
$pdo = DbUtils::openDbAndReturnPdoStatic();
if ($tableid == 0) {
$tableid = null;
}
$sql = "SELECT %queue%.id as queueid,productname FROM ";
$sql .= "%queue% WHERE ";
$sql .= "(tablenr=? OR (tablenr IS NULL AND ? IS NULL)) AND %queue%.toremove='0' AND isclosed is null AND billid is null ";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($tableid,$tableid));
$unpaidresultungrouped = $stmt->fetchAll();
$sql = "SELECT %queue%.id as queueid,productname FROM ";
$sql .= "%queue% LEFT OUTER JOIN %bill% ON %queue%.billid=%bill%.id WHERE ";
$sql .= "(tablenr=? OR (tablenr IS NULL AND ? IS NULL)) AND %queue%.toremove='0' AND isclosed is null AND billid is null AND (";
$sql .= "%queue%.delivertime IS NULL OR ";
$sql .= "(%queue%.delivertime IS NOT NULL AND workprinted='1')) ";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($tableid,$tableid));
$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);
}
echo json_encode(array("status" => "OK","msg" => $merged));
}
function isQueueIdInList($queueid,$list) {
foreach($list as $entry) {
if ($entry['queueid'] == $queueid) {
return true;
}
}
return false;
}
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;
}
}
$pdo = DbUtils::openDbAndReturnPdoStatic();
$pdo->beginTransaction();
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));
}
$sql = "UPDATE %queue% SET tablenr=? WHERE id IN($queueids) AND tablenr=?";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($toTableId,$fromTableId));
$pdo->commit();
echo json_encode(array("status" => "OK"));
}
function getJsonProductsOfTableToPay($tableid) {
$pdo = DbUtils::openDbAndReturnPdoStatic();
$unit = CommonUtils::caseOfSqlUnitSelection($pdo);
$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
FROM %queue% Q
INNER JOIN %products% ON Q.productid = %products%.id
INNER JOIN %pricelevel% ON Q.pricelevel = %pricelevel%.id
INNER JOIN %prodtype% ON %products%.category = %prodtype%.id ";
if ($tableid == 0) {
$sql .= "WHERE tablenr is null ";
} else {
$sql .= "WHERE tablenr = $tableid ";
}
$sql .= "AND paidtime is null AND Q.toremove='0' AND isclosed is null ORDER BY kind,ordertime, id;";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute();
$result = $stmt->fetchAll();
$prodsToPay = array();
foreach ($result as $zeile) {
$thePrice = $zeile['price'];
$theTax = $zeile['tax'];
$thePriceLevelName = $zeile['pricelevelname'];
$longName = $zeile['longname'];
$togo = $zeile["togo"];
$queueid = $zeile['id'];
$prodimageid = $zeile['prodimageid'];
$printjobid = $zeile["printjobid"];
$extras = $this->getExtrasOfQueueItem($pdo,$queueid);
$prodId = $zeile['prodid'];
$prodsToPay[] = array("id" => $queueid, "prodid" => $prodId, "longname" => $longName, "pricelevelname" => $thePriceLevelName, "price" => $thePrice, "tax" => $theTax, "togo" => $togo, "prodimageid" => $prodimageid, "printjobid" => $printjobid, "extras" => $extras);
}
echo json_encode(array("status" => "OK", "msg" => $prodsToPay));
}
function displayBill($billtableitems,$totalPrice) {
$currency = $this->commonUtils->getCurrency();
$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>";
}
echo "</table>";
}
static function declareProductBeDeliveredWithGivenPdo($pdo,$queueid) {
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=?";
CommonUtils::execSql($pdo, $updateSql, array($delivertime,$queueid));
$updateSql = "UPDATE %queue% SET readytime=? WHERE id=?";
CommonUtils::execSql($pdo, $updateSql, array($delivertime,$queueid));
}
}
function declareProductBeDelivered($queueid) {
if (is_numeric($queueid)) {
$pdo = $this->dbutils->openDbAndReturnPdo();
self::declareProductBeDeliveredWithGivenPdo($pdo, $queueid);
}
}
function declareMultipleProductsDelivered($queueids) {
$ids = explode(",",$queueids);
$pdo = DbUtils::openDbAndReturnPdoStatic();
$pdo->beginTransaction();
for ($i=0;$i < count($ids); $i++) {
$aQueueId = $ids[$i];
if (is_numeric($aQueueId)) {
self::declareProductBeDeliveredWithGivenPdo($pdo,$aQueueId);
}
}
$pdo->commit();
echo json_encode(array("status" => "OK"));
}
function declareProductNotBeDelivered($queueid) {
$pdo = DbUtils::openDbAndReturnPdoStatic();
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=?";
$stmt = $pdo->prepare(DbUtils::substTableAlias($updateSql));
$stmt->execute(array(null,$queueid));
}
}
public function getAllPreparedProductsForTableidAsArray($pdo,$tableid) {
if (!is_null($tableid)) {
if (!is_numeric($tableid)) {
return array("prods" => array(), "ids" => '');
}
$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% ";
}
$sql .= "WHERE (readytime IS NOT NULL and delivertime IS NULL ";
$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)) {
$sql .= "FROM %queue%,%products%,%bill% ";
} else {
$sql .= "FROM %queue%,%products% ";
}
$sql .= "WHERE (readytime IS NOT NULL and delivertime IS NULL ";
$sql .= "AND %queue%.tablenr is null ";
}
$sql .= "AND %queue%.productid=%products%.id ";
$sql .= "AND %queue%.isclosed is null ";
$sql .= "AND toremove = '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 tableno";
$tmp = DbUtils::substTableAlias($sql);
$dbresult = CommonUtils::fetchSqlAll($pdo, $sql, null);
$arrayOfProdsForTable = array();
$idsProdsOfTable = array(); // this is a hack! All queueids of a table redundant for "Deliver all"
foreach($dbresult as $zeile) {
$extras = $this->getExtrasOfQueueItem(null,$zeile['id']);
$anProdElem = array(
"id" => $zeile['id'],
"longname" => $zeile['longname'],
"option" => $zeile['anoption'],
"extras" => $extras,
"status" => "ready_to_deliver");
$arrayOfProdsForTable[] = $anProdElem;
$idsProdsOfTable[] = $zeile['id'];
}
return array("prods" => $arrayOfProdsForTable, "ids" => join(',',$idsProdsOfTable));
}
public function numberOfProductsForTableNotDelivered($pdo,$tableid) {
$sql = "SELECT DISTINCT %queue%.id as id ";
if (!is_null($tableid)) {
$sql .= "FROM %queue%,%resttables% ";
} else {
$sql .= "FROM %queue% ";
}
$sql .= "WHERE delivertime IS NULL ";
$sql .= "AND %queue%.isclosed is null ";
$sql .= "AND workprinted='0' ";
$sql .= "AND toremove = '0' ";
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% ";
}
$sql .= "WHERE (readytime IS NOT NULL and delivertime IS NULL ";
$sql .= "AND toremove = '0' ";
$sql .= "AND %queue%.tablenr=%resttables%.id AND ";
$sql .= "%queue%.isclosed is null AND ";
$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 .= " 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% ";
}
$sql .= "WHERE (readytime IS NOT NULL and delivertime IS NULL ";
$sql .= "AND toremove = '0' ";
$sql .= "AND %queue%.tablenr is null AND ";
$sql .= "%queue%.isclosed is null AND ";
$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";
$tableheadeline = $commonUtils->getTableNameFromId($pdo,$tableid);
$preparedProds[] = array(
"tableheadline" => $tableheadeline,
"tableid" => $tableid,
"tablestatus" => $tablestatus,
"ids" => $arrayOfProdsAndIdsOfATable['ids'],
"prodsOfTable" => $arrayOfProdsOfATable);
} else {
$tablestatus = "incomplete";
$tableheadeline = "Tisch: " . $commonUtils->getTableNameFromId($pdo,$tableid);
$preparedProds_incomplete_tables[] = array(
"tableheadline" => $tableheadeline,
"tableid" => $tableid,
"tablestatus" => $tablestatus,
"ids" => $arrayOfProdsAndIdsOfATable['ids'],
"prodsOfTable" => $arrayOfProdsOfATable);
}
}
$newProdsToServe = self::getNewProductsToServe($pdo);
self::setNewProductsToServe($pdo, 0);
$items = array_merge($preparedProds,$preparedProds_incomplete_tables);
echo json_encode(array("items" => $items, "newproductstoserve" => $newProdsToServe));
}
/*
* 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% ";
}
$sql .= "WHERE (delivertime IS NOT NULL ";
$sql .= "AND %queue%.productid=%products%.id ";
$sql .= "AND %queue%.tablenr=%resttables%.id ";
$sql .= "AND toremove = '0' AND ";
$sql .= "%queue%.isclosed is null AND ";
$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();
$result1 = $stmt->fetchAll();
$sql = "SELECT DISTINCT %queue%.id as id,'' as tableno,longname,delivertime,anoption,%products%.id as prodid ";
if ($this->areBillExisting($pdo)) {
$sql .= "FROM %queue%,%products%,%bill% ";
} else {
$sql .= "FROM %queue%,%products% ";
}
$sql .= "WHERE (delivertime IS NOT NULL ";
$sql .= "AND %queue%.productid=%products%.id ";
$sql .= "AND %queue%.tablenr is null ";
$sql .= "AND toremove = '0' AND ";
$sql .= "%queue%.isclosed is null AND ";
$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) {
$productid = $zeile['prodid'];
$useConditions = $this->getUseKitchenAndSupplyForProd($pdo,$productid);
if ($useConditions["usesupply"] == 1) {
$extras = $this->getExtrasOfQueueItem(null,$zeile['id']);
$deliveredProd = array(
"id" => $zeile['id'],
"longname" => $zeile['longname'],
"option" => $zeile['anoption'],
"extras" => $extras,
"delivertime" => $zeile['delivertime'],
"tablename" => $zeile['tableno']);
$lastDeliveredProds[] = $deliveredProd;
}
}
echo json_encode(array("status" => "OK","msg" => $lastDeliveredProds));
}
/*
* 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
*/
public function declarePaidCreateBillReturnBillId($pdo,$ids,$tableid,$paymentId,$declareready,$host,$calledInternally = false,$reservationid='',$guestinfo='',$intguestid='',$userid=null, $billdate=null) {
date_default_timezone_set(DbUtils::getTimeZone());
$currentTime = date('Y-m-d H:i:s');
if (!is_null($billdate)) {
$currentTime = $billdate;
}
if ($intguestid == '') {
$intguestid = null;
}
if ($reservationid != "") {
$reservationid = substr($reservationid, 0, 30);
}
if ($guestinfo != "") {
$guestinfo = substr($guestinfo, 0, 30);
}
$printextras = CommonUtils::getConfigValue($pdo, 'printextras', 0);
if (is_null($userid)) {
$userid = $this->getUserId();
}
$ids = trim($ids, ",");
$ids_array = explode ( ',', $ids );
if (CommonUtils::callPlugin($pdo, "createBill", "replace")) {
return;
}
CommonUtils::callPlugin($pdo, "createBill", "before");
if (!$calledInternally) {
$pdo->beginTransaction();
}
$idList = explode(",", $ids);
$allNumeric = true;
foreach($idList as $anId) {
if (!is_numeric($anId)) {
$allNumeric = false;
}
}
$allNotPaid = true;
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) {
$allNotPaid = false;
}
} else {
$allNotPaid = false;
}
$billid = (-1);
if ($allNotPaid == true) {
$billid = -1;
$nextBillIds = Bill::getNextBillIds($pdo);
$maxId = $nextBillIds["maxid"];
$nextBilluid = $nextBillIds["nextbilluid"];
$newprevbrutto = 0;
$newprevnetto = 0;
if (!is_null($maxId) && ($maxId > 0)) {
$billid = intval($maxId) + 1;
} else {
$billid = 1;
}
if (!$this->commonUtils->verifyLastBillId($pdo, $billid)) {
if (!$calledInternally) {
echo json_encode(array("status" => "ERROR", "code" => ERROR_INCONSISTENT_DB, "msg" => ERROR_INCONSISTENT_DB_MSG));
$pdo->rollBack();
}
return;
} else {
$this->commonUtils->setLastBillIdInWorkTable($pdo, $billid);
}
if (is_null($tableid)) {
$tableid = 0;
}
$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();
$idlist = join("','",$ids_array);
$sql = "SELECT SUM(price) as brutto,ROUND(SUM(price/(1 + %queue%.tax/100.0)),6) as netto FROM %queue% WHERE id IN ('$idlist')";
$row = CommonUtils::getRowSqlObject($pdo, $sql, null);
$brutto = $row->brutto;
$netto = $row->netto;
$signature = CommonUtils::calcSignatureForBill($currentTime, $brutto, $netto, $userid);
$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,?,?,?,?,?,?)";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($billInsertSql));
$stmt->execute(array($billid,$nextBilluid,$currentTime,$brutto,$netto,$newprevbrutto,$newprevnetto,$tableid,$paymentId,$userid,$host,$reservationid,$guestinfo,$intguestid,$printextras,$signature));
for ($i=0;$i<count($ids_array);$i++) {
$queueid = $ids_array[$i];
if (is_numeric($queueid)) {
if ($declareready == 0) {
$updateSql = "UPDATE %queue% SET paidtime=?, billid=? WHERE id=?";
$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 ( ?,?)";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($billProdsSql));
$stmt->execute(array($queueid,$billid));
$sql = "INSERT INTO %recordsqueue% (recordid,queueid) VALUES(?,?)";
CommonUtils::execSql($pdo, $sql, array($recordid,$queueid));
}
}
Hotelinterface::insertIntoHsin($pdo,$billid);
}
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;
}
if (!$calledInternally) {
$pdo->commit();
}
$billInfo = array("billid" => $billid, "date" => $currentTime, "brutto" => $brutto);
// Rksv::signBill($pdo, $billid);
$austriaEnabled = CommonUtils::getConfigValue($pdo, "austria", 0);
$cbirdFolder = trim(CommonUtils::getConfigValue($pdo, 'cbirdfolder', ''));
if (($austriaEnabled == 1) && ($cbirdFolder != "")) {
$austriabind = CommonUtils::getConfigValue($pdo, 'austriabind', 0);
$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);
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);
$filename = $cbirdFolder . "/" . $currentTime . "-" . $longid . ".json";
file_put_contents($filename, $cbirdEntryJson);
} else if (($austriabind == 2) || ($austriabind == 3)) {
self::sendBillToQRK($pdo, $paymentId, $billid);
}
}
CommonUtils::callPlugin($pdo, "createBill", "after");
CommonUtils::log($pdo, "QUEUE", "Created bill with id=$billid from user $userid");
if (!$calledInternally) {
echo json_encode(array("status" => "OK", "msg" => $billInfo));
} else {
return $billid;
}
}
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);
}
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;
}
private function getUserId() {
if(session_id() == '') {
session_start();
}
return $_SESSION['userid'];
}
}