ordersprinter/webapp/php/printqueue.php

1032 lines
36 KiB
PHP

<?php
// Datenbank-Verbindungsparameter
require_once ('config.php');
require_once ('globals.php');
require_once ('dbutils.php');
require_once ('bill.php');
require_once ('closing.php');
require_once ('admin.php');
class PrintQueue {
var $dbutils;
var $userrights;
var $admin;
var $instance = null;
private static $FOOD = 1;
private static $DRINK = 2;
private static $RECEIPT = 3;
private static $CLOSING = 4;
public static $CANCELFOOD = 5;
public static $CANCELDRINK = 6;
public static $PICKUP = 7;
public static $FOOD_PRINTSERVER = 0;
public static $DRINK_PRINTSERVER = 1;
function __construct() {
$this->dbutils = new DbUtils();
$this->userrights = new Userrights();
$this->admin = new Admin();
}
function handleCommand($command) {
$fl = null;
if (isset($_GET['fl'])) {
$fl = intval($_GET['fl']);
}
if (isset($_GET['instance'])) {
$this->instance = $_GET['instance'];
}
$pass = '';
if (isset($_POST['pass'])) {
$pass = $_POST['pass'];
}
// these command are only allowed for user with waiter rights
if ($command == 'getNextReceiptPrintJobs') {
if(isset($_GET['printers'])) {
$this->getNextReceiptPrintJobs($pass,$_GET['language'],$_GET['printers'],$fl);
} else {
$this->getNextReceiptPrintJobs($pass,$_GET['language'],"1,2,3,4,5,6",$fl);
}
} else if ($command == 'getNextClosingPrintJobs') {
$this->getNextClosingPrintJobs($pass,$_GET['language'],$fl);
} else if ($command == 'getNextFoodWorkPrintJobs') {
if (isset($_GET['printer'])) {
$this->getNextFoodWorkPrintJobs($_GET['printer'],$pass,$fl);
} else {
$this->getNextFoodWorkPrintJobs(null,$pass,$fl);
}
} else if ($command == 'getNextDrinkWorkPrintJobs') {
if (isset($_GET['printer'])) {
$this->getNextDrinkWorkPrintJobs($_GET['printer'],$pass,$fl);
} else {
$this->getNextDrinkWorkPrintJobs(null,$pass,$fl);
}
} else if ($command == 'getNextPickupPrintJobs') {
if (isset($_GET['printer'])) {
$this->getNextPickupPrintPrintJobs($_GET['printer'],$pass,$fl);
} else {
$this->getNextPickupPrintPrintJobs(null,$pass,$fl);
}
} else if ($command == 'getNextCancelFoodWorkPrintJobs') {
if (isset($_GET['printer'])) {
$this->getNextCancelFoodWorkPrintJobs($_GET['printer'],$pass,$fl);
} else {
$this->getNextCancelFoodWorkPrintJobs(null,$pass,$fl);
}
} else if ($command == 'getNextCancelDrinkWorkPrintJobs') {
if (isset($_GET['printer'])) {
$this->getNextCancelDrinkWorkPrintJobs($_GET['printer'],$pass,$fl);
} else {
$this->getNextCancelDrinkWorkPrintJobs(null,$pass,$fl);
}
} else if ($command == 'deletePrintJob') {
$this->deletePrintJob($pass,$_POST['id']);
} else if ($command == 'queueReceiptPrintJob') {
if (isset($_POST['useaddrecprinter'])) {
$this->queueReceiptPrintJob($_POST['billid'],$_POST['useaddrecprinter']);
} else {
$this->queueReceiptPrintJob($_POST['billid'],0);
}
} else if ($command == 'queueClosingSummary') {
$this->queueClosingSummary($_GET['closingid']);
} else if ($command == 'testConnection') {
$this->testConnection($pass);
} else if ($command == 'getReceiptConfig') {
$this->getReceiptConfig($fl);
} else if ($command == 'getLogoAsPng') {
$this->getLogoAsPng();
} else if ($command == 'getLogoAsPngWithAlphaChannel') {
$this->getLogoAsPngWithAlphaChannel();
} else if ($command == 'getLogoAsWbmp') {
$this->getLogoAsWbmp();
} else if ($command == 'getPrintJobOverview') {
$pdo = DbUtils::openDbAndReturnPdoStatic();
$this->getPrintJobOverview($pdo);
} else if ($command == 'clearprintjobs') {
$pdo = DbUtils::openDbAndReturnPdoStatic();
$this->clearprintjobs($pdo);
} else if ($command == 'batchReceiptPrintJob') {
$this->batchReceiptPrintJob($_POST['start'],$_POST['end']);
} else if ($command == 'getLastLog') {
$this->getLastLog($pass);
} else if ($command == 'deleteSpooledPrintJob') {
$this->deleteSpooledPrintJob($_POST['id']);
} else if ($command == 'reprintworkreceipt') {
$this->reprintworkreceipt($_GET["workid"]);
} else {
echo "Kommando nicht erkannt!";
}
}
private function checkForPrinterInstance($pdo,$rectype) {
if (!is_null($this->instance)) {
$assignedInstance = CommonUtils::getConfigValue($pdo, $rectype . "prinstance", 1);
if ($this->instance == $assignedInstance) {
return true;
} else {
return false;
}
} else {
return true;
}
}
private function saveLastPrintServerAccess($pdo) {
date_default_timezone_set(DbUtils::getTimeZone());
$date = new DateTime();
$unixTimeStamp = $date->getTimestamp();
$sql = "SELECT count(id) as countid FROM %work% WHERE item=?";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array('lastprtserveraccess'));
$row = $stmt->fetchObject();
if ($row->countid == 0) {
$sql = "INSERT INTO %work% (item,value,signature) VALUES(?,?,?)";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array('lastprtserveraccess',$unixTimeStamp,null));
} else {
$sql = "UPDATE %work% SET value=? WHERE item=?";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($unixTimeStamp,'lastprtserveraccess'));
}
}
function testConnection($md5pass) {
header( "Expires: Mon, 20 Dec 1998 01:00:00 GMT" );
header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" );
header( "Cache-Control: no-cache, must-revalidate" );
header( "Pragma: no-cache" );
header( "Content-Type: text/html; charset=utf8" );
$isCorrect = $this->isPasswordCorrect(null,$md5pass,true);
if ($isCorrect) {
echo "ok";
} else {
// Output from isPasswordCorrect method already
// echo " - false -";
}
}
/*
* Insert a "work" (food or drink) job into the printjob queue. The POS Print Server will
* pick these jobs and delete them after successful printing
*/
public static function queueWorkPrintJob($pdo,$table,$timestamp,$prods,$kind,$printer,$username,$isTogo) {
$workId = Workreceipts::getNextWorkReceiptId($pdo);
$table .= " ($username)";
$content = json_encode(array("workid" => $workId,"table" => $table, "time" => $timestamp, "products" => $prods));
$printInsertSql = "INSERT INTO `%printjobs%` (`content`,`type`,`printer`) VALUES (?,?,?)";
CommonUtils::execSql($pdo, $printInsertSql, array($content,intval($kind) + 1,$printer));
$idOfWorkJob = $pdo->lastInsertId();
$sql = "UPDATE %queue% SET printjobid=? WHERE id=?";
foreach($prods as $aProd) {
$queueid = $aProd["id"];
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($idOfWorkJob,$queueid));
if (isset($aProd["allqueueids"])) {
foreach($aProd["allqueueids"] as $aQueueId) {
$stmt->execute(array($idOfWorkJob,$aQueueId));
}
}
}
CommonUtils::log($pdo,"QUEUE","Create work job with id=$idOfWorkJob for tableid $table from user $username of kind $kind for printer=$printer");
$printAPickupReceipt = false;
$printpickups = CommonUtils::getConfigValue($pdo, "printpickups", 0);
if ($printpickups == 1) {
$printAPickupReceipt = true;
} else if ($printpickups == 2) {
if ($isTogo) {
$printAPickupReceipt = true;
}
}
if ($printAPickupReceipt) {
$sql = "INSERT INTO `%printjobs%` (`content`,`type`,`printer`) VALUES (?,?,?)";
CommonUtils::execSql($pdo, $sql, array($content,self::$PICKUP,1));
}
}
function getPrintJobOverview($pdo) {
if (!($this->userrights->hasCurrentUserRight('right_manager')) &&
!($this->userrights->hasCurrentUserRight('is_admin'))
) {
echo json_encode(array("status" => "ERROR", "code" => ERROR_DB_PRIVS_MISSING, "msg" => ERROR_DB_PRIVS_MISSING_MSG));
return;
}
$jobs = array();
for ($printer=1;$printer<7;$printer++) {
$sql = "SELECT J.id as id,B.id as billid,billdate,brutto,";
$sql .= "CASE ";
$sql .= " WHEN tableid='-1' THEN '---' ";
$sql .= " WHEN tableid='0' THEN 'To-Go' ";
$sql .= " ELSE (SELECT tableno FROM %resttables% WHERE id=B.tableid) ";
$sql .= "END as tablename,";
$sql .= "type FROM %printjobs% J,%bill% B WHERE printer=? AND type=? AND content=B.id ";
$resultBills = CommonUtils::fetchSqlAll($pdo, $sql, array($printer,self::$RECEIPT));
$sql = "SELECT %printjobs%.id as id,%closing%.id as closingid,type,closingdate FROM %printjobs%,%closing% WHERE printer=? AND type = '" . self::$CLOSING . "' AND content=%closing%.id";
$resultClosings = CommonUtils::fetchSqlAll($pdo, $sql, array($printer));
$result = array_merge($resultBills,$resultClosings);
$jobs[] = array("printer" => $printer, "count" => count($result),"jobs" => $result);
}
$sql = "SELECT %printjobs%.id as id,content FROM %printjobs% WHERE type=? AND removed is null";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array(self::$FOOD));
$result = $stmt->fetchAll();
$works = array();
foreach($result as $r) {
$works[] = array("id" => $r['id'],"content" => json_decode($r['content']));
}
$jobs[] = array("printer" => 7, "count" => count($result),"jobs" => $works);
$stmt->execute(array(self::$DRINK));
$result = $stmt->fetchAll();
$works = array();
foreach($result as $r) {
$works[] = array("id" => $r['id'],"content" => json_decode($r['content']));
}
$jobs[] = array("printer" => 8, "count" => count($result),"jobs" => $works);
$stmt->execute(array(self::$PICKUP));
$result = $stmt->fetchAll();
$works = array();
foreach($result as $r) {
$works[] = array("id" => $r['id'],"content" => json_decode($r['content']));
}
$jobs[] = array("printer" => 9, "count" => count($result),"jobs" => $works);
echo json_encode(array("status" => "OK", "msg" => $jobs));
}
function clearprintjobs($pdo) {
if (!($this->userrights->hasCurrentUserRight('right_manager')) &&
!($this->userrights->hasCurrentUserRight('is_admin'))
) {
echo json_encode(array("status" => "ERROR", "code" => ERROR_DB_PRIVS_MISSING, "msg" => ERROR_DB_PRIVS_MISSING_MSG));
return;
}
$sql = "DELETE FROM %printjobs% WHERE (type <> ?) AND (type <> ?)";
CommonUtils::execSql($pdo, $sql, array(self::$FOOD,self::$DRINK));
$sql = "UPDATE %printjobs% SET removed=? WHERE (type = ?) OR (type = ?)";
CommonUtils::execSql($pdo, $sql, array(1,self::$FOOD,self::$DRINK));
$this->getPrintJobOverview($pdo);
}
function batchReceiptPrintJob($start,$end) {
try {
$start = intval($start);
$end = intval($end);
} catch (Exception $ex) {
echo json_encode(array("status" => "ERROR", "code" => NUMBERFORMAT_ERROR, "msg" => NUMBERFORMAT_ERROR_MSG));
return;
}
if(!($this->userrights->hasCurrentUserRight('right_bill'))) {
echo json_encode(array("status" => "ERROR", "code" => ERROR_BILL_NOT_AUTHOTRIZED, "msg" => ERROR_BILL_NOT_AUTHOTRIZED_MSG));
} else {
if ($start > $end) {
$tmp = $end;
$end = $start;
$start = $tmp;
}
if(session_id() == '') {
session_start();
}
$printer = $_SESSION['receiptprinter'];
$pdo = DbUtils::openDbAndReturnPdoStatic();
for($jobId=$start;$jobId <= $end;$jobId++) {
$sql = "SELECT count(id) as countid FROM %bill% WHERE id=?";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($jobId));
$row =$stmt->fetchObject();
if ($row->countid == 1) {
$printInsertSql = "INSERT INTO `%printjobs%` (`id` , `content`,`type`,`printer`) VALUES ( NULL,?,?,?)";
$stmt = $pdo->prepare(DbUtils::substTableAlias($printInsertSql));
$stmt->execute(array($jobId,'" . self::$RECEIPT . "',$printer));
}
}
echo json_encode(array("status" => "OK"));
}
}
function checkForUserRightManagerAdmin() {
if (!($this->userrights->hasCurrentUserRight('right_manager')) &&
!($this->userrights->hasCurrentUserRight('is_admin'))
) {
echo "Benutzerrechte nicht ausreichend!";
return false;
} else {
return true;
}
}
function reprintworkreceipt($workid) {
if ($this->checkForUserRightManagerAdmin()) {
$pdo = DbUtils::openDbAndReturnPdoStatic();
if (is_null($workid)) {
echo json_encode(array("status" => "ERROR","msg" => "Keine gültige ID angegeben!"));
return;
}
$workid = trim($workid);
$sql = "SELECT id,content FROM %printjobs% WHERE type IN (?,?,?,?)";
$result = CommonUtils::fetchSqlAll($pdo, $sql, array(self::$FOOD,self::$DRINK,self::$CANCELFOOD,self::$CANCELDRINK));
foreach ($result as $w) {
$content = json_decode($w["content"],true);
if ($content["workid"] == $workid) {
$sql = "UPDATE %printjobs% SET removed=? WHERE id=?";
CommonUtils::execSql($pdo, $sql, array(null,$w["id"]));
echo json_encode(array("status" => "OK"));
return;
}
}
echo json_encode(array("status" => "ERROR","msg" => "Kein Arbeitsbon-Druckjob mit der ID gefunden!"));
}
}
function deleteSpooledPrintJob($printjobid) {
if ($this->checkForUserRightManagerAdmin()) {
$pdo = DbUtils::openDbAndReturnPdoStatic();
$sql = "DELETE FROM %printjobs% WHERE id=?";
CommonUtils::execSql($pdo, $sql, array($printjobid));
echo json_encode(array("status" => "OK"));
}
}
public static function internalQueueReceiptPrintjob($pdo,$billid,$recprinter) {
try {
CommonUtils::log($pdo, "PRINTQUEUE", "Insert bill with id=$billid for printer=$printer into queue.");
$printInsertSql = "INSERT INTO `%printjobs%` (`content`,`type`,`printer`) VALUES (?,?,?)";
CommonUtils::execSql($pdo, $printInsertSql, array((string)($billid),self::$RECEIPT,$recprinter));
} catch (Exception $ex) {
return array("status" => "ERROR","msg" => $ex->getMessage());
}
return array("status" => "OK");
}
function queueReceiptPrintJob($billid,$useaddrecprinter) {
// waiter, or manager, bill, admin rights required
if (!($this->userrights->hasCurrentUserRight('right_paydesk')) &&
!($this->userrights->hasCurrentUserRight('right_manager')) &&
!($this->userrights->hasCurrentUserRight('right_bill')) &&
!($this->userrights->hasCurrentUserRight('right_waiter')) &&
!($this->userrights->hasCurrentUserRight('is_admin'))
) {
echo "Benutzerrechte nicht ausreichend!";
return false;
} else {
// PAY_PRINT_TYPE = 3 means printing as paydesk print -> choose the printer
// (print type is misused also for selection of printer)
if(session_id() == '') {
session_start();
}
$printer = $_SESSION['receiptprinter'];
// now get receipt info from bill table
$pdo = DbUtils::openDbAndReturnPdoStatic();
$doubleReceipt = CommonUtils::getConfigValue($pdo, 'doublereceipt', 0);
$isBillPaidByCard = Bill::isBillPaidByCard($pdo,$billid);
$noOfInsert = 1;
if ($isBillPaidByCard && ($doubleReceipt == 1)) {
$noOfInsert = 2;
}
CommonUtils::log($pdo, "PRINTQUEUE", "Insert bill $noOfInsert x with id=$billid for printer=$printer into queue.");
$sql = "SELECT setting FROM %config% WHERE name=?";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array("addreceipttoprinter"));
$row = $stmt->fetchObject();
$addprinter = $row->setting;
$printInsertSql = "INSERT INTO `%printjobs%` (`id` , `content`,`type`,`printer`) VALUES ( NULL,?,?,?)";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($printInsertSql));
for ($times=0;$times < $noOfInsert; $times++) {
$stmt->execute(array((string)($billid),self::$RECEIPT,$printer));
}
if (!is_null($addprinter) && ($addprinter != "") && ($useaddrecprinter == 1)) {
$stmt->execute(array((string)($billid),self::$RECEIPT,$addprinter));
}
echo json_encode("OK");
}
}
public function queueClosingSummary($closingid) {
if (!($this->userrights->hasCurrentUserRight('right_paydesk')) &&
!($this->userrights->hasCurrentUserRight('right_manager')) &&
!($this->userrights->hasCurrentUserRight('right_bill')) &&
!($this->userrights->hasCurrentUserRight('right_closing')) &&
!($this->userrights->hasCurrentUserRight('right_waiter')) &&
!($this->userrights->hasCurrentUserRight('is_admin'))
) {
echo "Benutzerrechte nicht ausreichend!";
return false;
} else {
if(session_id() == '') {
session_start();
}
$printer = $_SESSION['receiptprinter'];
$pdo = $this->dbutils->openDbAndReturnPdo();
CommonUtils::log($pdo, "PRINTQUEUE", "Insert closing with id=$closingid for printer=$printer into queue.");
$printInsertSql = "INSERT INTO `%printjobs%` (`id` , `content`,`type`,`printer`) VALUES ( NULL,?,?,?)";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($printInsertSql));
$stmt->execute(array((string)($closingid),self::$CLOSING,$printer));
echo json_encode("OK");
}
}
function getBigFontWorkReceiptSetting($pdo) {
$sql = "SELECT setting FROM %config% WHERE name=?";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array("bigfontworkreceipt"));
$row =$stmt->fetchObject();
return $row->setting;
}
function isPasswordCorrect($pdo,$pass,$verbose) {
if (is_null($pdo)) {
$pdo = DbUtils::openDbAndReturnPdoStatic();
}
$sql = "SELECT setting FROM %config% WHERE name=?";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array("printpass"));
$row =$stmt->fetchObject();
if ($row != null) {
$passInDb = $row->setting;
if ($passInDb != null) {
// plain comparison
if ($pass == $passInDb) {
return true;
} else {
if ($verbose) {
echo "Error: Falscher Printpass!";
}
}
} else {
if ($verbose) {
echo "Error: kein Printpass in DB gesetzt!";
}
}
}
if ($verbose) {
echo "Error: DB konnte nicht abgefragt werden!";
}
return false;
}
private static function outputEmptyImage() {
$my_img = imagecreate( 1,1 );
$background = imagecolorallocate( $my_img, 0, 0, 255 );
$black = imagecolorallocate($my_img, 0, 0, 0);
imagecolortransparent($my_img, $black);
imagepng( $my_img );
imagecolordeallocate( $my_img, $background );
imagecolordeallocate( $my_img, $black );
imagedestroy( $my_img );
}
function getLogoAsPng() {
$this->getLogoAsPngCore(false);
}
function getLogoAsPngWithAlphaChannel() {
$this->getLogoAsPngCore(true);
}
function getLogoAsPngCore($saveAlphaChannel) {
$sendEmptyImageInsteadOfNone = false;
if (isset($_GET["style"])) {
if ($_GET["style"] == "always") {
$sendEmptyImageInsteadOfNone = true;
}
}
$pdo = DbUtils::openDbAndReturnPdoStatic();
//header("Content-Disposition: attachment; filename=logo.png");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
header("Expires: Mon, 20 Dec 1998 01:00:00 GMT" );
header('Content-Type: ' . image_type_to_mime_type(IMAGETYPE_PNG));
$sql = "SELECT setting from %logo% WHERE name=?";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array('logoimg'));
$row = $stmt->fetchObject();
if ($stmt->rowCount() > 0) {
$img = $row->setting;
if (is_null($img)) {
if ($sendEmptyImageInsteadOfNone) {
self::outputEmptyImage();
}
} else {
$php_img = imagecreatefromstring($img);
$colorAlpha = null;
imagesavealpha($php_img, true);
if ($saveAlphaChannel) {
$colorAlpha = imagecolorallocatealpha($php_img, 0, 0, 0, 127);
imagepng($php_img, NULL);
imagecolordeallocate($php_img,$colorAlpha);
} else {
$width = imagesx($php_img);
$height = imagesy($php_img);
$bgImg = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($bgImg, 255, 255, 255);
imagefill($bgImg, 0, 0, $white);
imagecopyresampled(
$bgImg, $php_img,
0, 0, 0, 0,
$width, $height,
$width, $height);
imagepng($bgImg, NULL);
imagecolordeallocate($bgImg,$white);
imagedestroy($bgImg);
}
imagedestroy($php_img);
}
} else {
if ($sendEmptyImageInsteadOfNone) {
self::outputEmptyImage();
}
}
}
function getLogoAsWbmp() {
$pdo = $this->dbutils->openDbAndReturnPdo();
$genInfo = $this->admin->getGeneralConfigItems(false,$pdo);
header("Content-Disposition: attachment; filename=logo.wbmp");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
header("Expires: Mon, 20 Dec 1998 01:00:00 GMT" );
header('Content-Type: ' . image_type_to_mime_type(IMAGETYPE_WBMP));
$logourl = $genInfo["logourl"];
$img = file_get_contents("../" . $logourl);
$php_img = imagecreatefromstring($img);
$foreground_color = imagecolorallocate($im, 255, 0, 0);
imagewbmp($php_img, NULL, $foreground_color);
imagedestroy($php_img);
}
function getReceiptConfig($fl = null) {
$pdo = DbUtils::openDbAndReturnPdoStatic();
$decpoint = CommonUtils::getConfigValue($pdo, "decpoint", ",");
$billlanguage = CommonUtils::getConfigValue($pdo, "billlanguage", 0);
$version = CommonUtils::getConfigValue($pdo, "version", "0");
$currency = CommonUtils::getConfigValue($pdo, "currency", "Euro");
$companyinfo = CommonUtils::getConfigValue($pdo, "companyinfo", "");
$retArray = array("decpoint" => $decpoint,
"billlanguage" => $billlanguage,
"version" => $version,
"currency" => $currency,
"companyinfo" => $companyinfo
);
if (!is_null($fl) && ($fl >= 12)) {
$retArray["pollbills"] = CommonUtils::getConfigValue($pdo, "pollbills", 2);
$retArray["pollworksf"] = CommonUtils::getConfigValue($pdo, "pollworksf", 2);
$retArray["pollworksd"] = CommonUtils::getConfigValue($pdo, "pollworksd", 2);
$retArray["pollclosings"] = CommonUtils::getConfigValue($pdo, "pollclosings", 2);
}
if (!is_null($fl) && ($fl >= 13)) {
$retArray["pollpickups"] = CommonUtils::getConfigValue($pdo, "pollpickups", 2);
}
echo json_encode($retArray);
}
function getNextClosingPrintJobs($md5pass,$language,$fl=0) {
$pdo = $this->dbutils->openDbAndReturnPdo();
$isCorrect = $this->isPasswordCorrect($pdo,$md5pass,false);
if ($isCorrect) {
ob_start();
$this->saveLastPrintServerAccess($pdo);
$closing = new Closing();
$sql = "SELECT id,content,type,printer FROM %printjobs% WHERE type=? ORDER BY id";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array(4));
$result = $stmt->fetchAll();
$closingarray = array();
foreach($result as $aClos) {
$jobid = $aClos['id'];
$closid = $aClos["content"];
$printer = $aClos["printer"];
if (!is_null($this->instance)) {
if (!$this->checkForPrinterInstance($pdo, "k" . $printer)) {
continue;
}
}
$theClosing = $closing->getClosingSummaryWoSign($closid, $pdo, false,$fl);
$aClosing = array("id" => $jobid,"closing" => $theClosing, "printer" => $printer);
$closingarray[] = $aClosing;
}
echo json_encode($closingarray);
ob_end_flush();
} else {
echo json_encode(array());
}
}
function getTemplate($pdo,$templatekey) {
$sql = "SELECT setting FROM %config% WHERE name=?";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($templatekey));
$row =$stmt->fetchObject();
return $row->setting;
}
function getLastLog ($md5pass) {
$pdo = DbUtils::openDbAndReturnPdoStatic();
$isCorrect = $this->isPasswordCorrect($pdo,$md5pass,false);
if ($isCorrect || true) {
echo json_encode(CommonUtils::getLastLog($pdo));
} else {
echo json_encode("Log file from server unavaible due to wrong printcode");
}
}
function getNextReceiptPrintJobs($md5pass,$language,$printers,$fl) {
$pdo = $this->dbutils->openDbAndReturnPdo();
$isCorrect = $this->isPasswordCorrect($pdo,$md5pass,false);
if ($isCorrect) {
CommonUtils::log($pdo,"PRINTQUEUE", "getReceipts: p=$printers, fl=$fl");
ob_start();
$printersArr = explode ( ',', $printers );
$this->saveLastPrintServerAccess($pdo);
$template = $this->getTemplate($pdo, "rectemplate");
if (intval($language) > 2) {
$genInfo = $this->admin->getGeneralConfigItems(false,$pdo);
$language = $genInfo["billlanguage"];
}
$bill = new Bill();
$sql = "SELECT id,content,type,printer FROM %printjobs% WHERE type=? ORDER BY id";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array(3));
$result = $stmt->fetchAll();
CommonUtils::log($pdo,"PRINTQUEUE", "getNextReceiptPrintJobs: retrieve " . count($result) . " jobs");
$billarray = array();
foreach($result as $aBill) {
$printJobId = $aBill['id'];
$aBillId = $aBill["content"];
$printer = $aBill["printer"];
CommonUtils::log($pdo,"PRINTQUEUE", "getNextReceiptPrintJobs: collect bill with id=$aBillId for printer=$printer");
if (in_array($printer, $printersArr)) {
if (!$this->checkForPrinterInstance($pdo, "k" . $printer)) {
continue;
}
if (is_null($fl)) {
$receiptJob = array("id" => $printJobId,"bill" => $bill->getBillWithId($pdo,$aBillId,$language,$printer));
} else if ($fl >= 9) {
$hosttext = CommonUtils::getConfigValue($pdo, 'hosttext', '');
$receiptJob = array("id" => $printJobId,"bill" => $bill->getBillWithId($pdo,$aBillId,$language,$printer,true,true), "template" => $template, "hosttext" => $hosttext);
} else if ($fl >= 6) {
$receiptJob = array("id" => $printJobId,"bill" => $bill->getBillWithId($pdo,$aBillId,$language,$printer,true,true), "template" => $template);
} else if ($fl >= 4) {
$receiptJob = array("id" => $printJobId,"bill" => $bill->getBillWithId($pdo,$aBillId,$language,$printer,true), "template" => $template);
} else if ($fl >= 1) {
$receiptJob = array("id" => $printJobId,"bill" => $bill->getBillWithId($pdo,$aBillId,$language,$printer), "template" => $template);
}
$billarray[] = $receiptJob;
}
}
echo json_encode($billarray);
ob_end_flush();
CommonUtils::log($pdo,"PRINTQUEUE", "getNextReceiptPrintJobs: sent data to caller");
} else {
CommonUtils::log($pdo,"PRINTQUEUE", "getNextReceiptPrintJobs: Wrong printcode");
echo json_encode(array());
}
}
function getNextPickupPrintPrintJobs($printer,$md5pass,$fl) {
$pdo = $this->dbutils->openDbAndReturnPdo();
$isCorrect = $this->isPasswordCorrect($pdo,$md5pass,false);
if ($isCorrect) {
CommonUtils::log($pdo,"PRINTQUEUE", "getNextPickupPrintPrintJobs: type = " . self::$PICKUP . " , printer = $printer, fl= $fl");
if (!$this->checkForPrinterInstance($pdo, "p" . $printer)) {
echo json_encode(array());
return;
}
$this->saveLastPrintServerAccess($pdo);
$template = $this->getTemplate($pdo, "pickuptemplate");
if (is_null($printer)) {
$sql = "SELECT id,content,type FROM %printjobs% WHERE type=? AND removed is null ORDER BY id";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array(self::$PICKUP));
} else {
$sql = "SELECT id,content,type FROM %printjobs% WHERE type=? AND removed is null AND printer=? ORDER BY id";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array(self::$PICKUP,$printer));
}
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
CommonUtils::log($pdo,"PRINTQUEUE", "getNextPickupPrintPrintJobs: retrieve " . count($result) . " jobs");
$workarray = array();
foreach($result as $aWorkJob) {
$aWork = (array) json_decode($aWorkJob["content"]); // is in json format
$ps = (array) ($aWork["products"]);
foreach($ps as $p) {
if (isset($p->singleprod)) {
unset($p->singleprod);
}
if (isset($p->allqueueids)) {
unset($p->allqueueids);
}
}
CommonUtils::log($pdo,"PRINTQUEUE", "getNextPickupPrintPrintJobs: collect pickupreceipt with id=" . $aWorkJob["id"] . " for printer=$printer");
$workid = $aWork["workid"];
unset($aWork["workid"]);
$workarray[] = array("workid" => $workid,"id" => $aWorkJob["id"],"content" => $aWork, "bigfontworkreceipt" => 0, "template" => $template);
}
CommonUtils::log($pdo,"PRINTQUEUE", "getNextPickupPrintPrintJobs: sent data to caller");
echo json_encode($workarray);
} else {
CommonUtils::log($pdo,"PRINTQUEUE", "getNextPickupPrintPrintJobs: wrong printcode");
echo json_encode(array());
}
}
function getNextFoodWorkPrintJobs($printer,$md5pass,$fl) {
$this->getNextWorkPrintJobs($md5pass,self::$FOOD,$printer,$fl,"getNextFoodWorkPrintJobs");
}
function getNextDrinkWorkPrintJobs($printer,$md5pass,$fl) {
$this->getNextWorkPrintJobs($md5pass,self::$DRINK,$printer,$fl,"getNextDrinkWorkPrintJobs");
}
function getNextWorkPrintJobs($md5pass,$theType,$printer,$fl,$logmsg) {
$pdo = $this->dbutils->openDbAndReturnPdo();
$isCorrect = $this->isPasswordCorrect($pdo,$md5pass,false);
if ($isCorrect) {
CommonUtils::log($pdo,"PRINTQUEUE", "$logmsg: type = $theType, printer = $printer, fl= $fl");
$checkType = "f";
if ($theType == 2) {
$checkType = "d";
}
if (!$this->checkForPrinterInstance($pdo, $checkType . $printer)) {
echo json_encode(array());
return;
}
$this->saveLastPrintServerAccess($pdo);
$bigFontWorkReceipt = $this->getBigFontWorkReceiptSetting($pdo);
$templatekey = "foodtemplate";
if ($theType === 2) {
$templatekey = "drinktemplate";
}
$template = $this->getTemplate($pdo, $templatekey);
if (is_null($printer)) {
$sql = "SELECT id,content,type FROM %printjobs% WHERE type=? AND removed is null ORDER BY id";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($theType));
} else {
$sql = "SELECT id,content,type FROM %printjobs% WHERE type=? AND removed is null AND printer=? ORDER BY id";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($theType,$printer));
}
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
CommonUtils::log($pdo,"PRINTQUEUE", "getNextWorkPrintJobs: retrieve " . count($result) . " jobs");
$workarray = array();
foreach($result as $aWorkJob) {
$aWork = (array) json_decode($aWorkJob["content"]); // is in json format
$ps = (array) ($aWork["products"]);
foreach($ps as $p) {
if (isset($p->singleprod)) {
unset($p->singleprod);
}
if (isset($p->allqueueids)) {
unset($p->allqueueids);
}
}
CommonUtils::log($pdo,"PRINTQUEUE", "getNextWorkPrintJobs: collect work receipt with id=" . $aWorkJob["id"] . " for printer=$printer");
$workid = $aWork["workid"];
unset($aWork["workid"]);
if (($fl >= 5) && ($fl <= 6)) {
$prods = (array) ($aWork["products"]);
$targetProds = array();
foreach ($prods as $aProd) {
$theArrProd = (array) $aProd;
$newTargetProd = array("id" => $theArrProd["id"],
"longname" => $theArrProd["longname"],
"option" => $theArrProd["option"],
"price" => $theArrProd["price"],
"extras" => $theArrProd["extras"],
"ordertime" => $theArrProd["ordertime"],
"kind" => $theArrProd["kind"],
"printer" => $theArrProd["printer"],
);
$targetProds[] = $newTargetProd;
}
$aWork["products"] = $targetProds;
} else if ($fl < 5) {
$prods = (array) ($aWork["products"]);
$targetProds = array();
foreach ($prods as $aProd) {
$theArrProd = (array) $aProd;
$newTargetProd = array("id" => $theArrProd["id"],
"longname" => $theArrProd["longname"],
"option" => $theArrProd["option"],
"extras" => $theArrProd["extras"],
"ordertime" => $theArrProd["ordertime"],
"kind" => $theArrProd["kind"],
"printer" => $theArrProd["printer"],
);
$targetProds[] = $newTargetProd;
}
$aWork["products"] = $targetProds;
}
if ($fl >= 2) {
if ($fl >= 10) {
$workarray[] = array("workid" => $workid,"id" => $aWorkJob["id"],"content" => $aWork, "bigfontworkreceipt" => intval($bigFontWorkReceipt), "template" => $template);
} else {
$workarray[] = array("id" => $aWorkJob["id"],"content" => $aWork, "bigfontworkreceipt" => intval($bigFontWorkReceipt), "template" => $template);
}
} else {
// default without template
$workarray[] = array("id" => $aWorkJob["id"],"content" => $aWork, "bigfontworkreceipt" => intval($bigFontWorkReceipt));
}
}
CommonUtils::log($pdo,"PRINTQUEUE", "getNextWorkPrintJobs: sent data to caller");
echo json_encode($workarray);
} else {
CommonUtils::log($pdo,"PRINTQUEUE", "getNextWorkPrintJobs: wrong printcode");
echo json_encode(array());
}
}
function getNextCancelFoodWorkPrintJobs($printer,$md5pass,$fl) {
$this->getNextCancelWorkPrintJobs($md5pass,self::$CANCELFOOD,$printer,$fl);
}
function getNextCancelDrinkWorkPrintJobs($printer,$md5pass,$fl) {
$this->getNextCancelWorkPrintJobs($md5pass,self::$CANCELDRINK,$printer,$fl);
}
function getNextCancelWorkPrintJobs($md5pass,$theType,$printer,$fl) {
$pdo = $this->dbutils->openDbAndReturnPdo();
$isCorrect = $this->isPasswordCorrect($pdo,$md5pass,false);
if ($isCorrect) {
CommonUtils::log($pdo,"PRINTQUEUE", "getNextCancelWorkPrintJobs: type = $theType, printer = $printer, fl= $fl");
$checkType = "f";
if ($theType == 6) {
$checkType = "d";
}
if (!$this->checkForPrinterInstance($pdo, $checkType . $printer)) {
echo json_encode(array());
return;
}
$this->saveLastPrintServerAccess($pdo);
$templatekey = "canceltemplate";
$template = $this->getTemplate($pdo, $templatekey);
if (is_null($printer)) {
$sql = "SELECT id,content,type FROM %printjobs% WHERE type=? AND removed is null ORDER BY id";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($theType));
} else {
$sql = "SELECT id,content,type FROM %printjobs% WHERE type=? AND printer=? AND removed is null ORDER BY id";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($theType,$printer));
}
$result = $stmt->fetchAll();
CommonUtils::log($pdo,"PRINTQUEUE", "getNextCancelWorkPrintJobs: retrieve " . count($result) . " jobs");
$workarray = array();
foreach($result as $aWorkJob) {
$aWork = (array) json_decode($aWorkJob["content"],true); // is in json format
CommonUtils::log($pdo,"PRINTQUEUE", "getNextCancelWorkPrintJobs: collect work receipt with id=" . $aWorkJob["id"] . " for printer=$printer");
$workid = $aWork["workid"];
unset($aWork["workid"]);
$showType = self::$FOOD_PRINTSERVER;
if ($theType == self::$CANCELDRINK) {
$showType = self::$DRINK_PRINTSERVER;
}
$workarray[] = array("workid" => $workid,
"id" => $aWorkJob["id"],
"refworkid" => $aWork["refworkid"],
"longname" => $aWork["longname"],
"kind" => $showType,
"table" => $aWork["table"],
"time" => $aWork["time"],
"price" => $aWork["price"],
"extras" => $aWork["extras"],
"template" => $template);
}
CommonUtils::log($pdo,"PRINTQUEUE", "getNextWorkPrintJobs: sent data to caller");
echo json_encode($workarray);
} else {
CommonUtils::log($pdo,"PRINTQUEUE", "getNextWorkPrintJobs: wrong printcode");
echo json_encode(array());
}
}
function deletePrintJob($pass,$id) {
$pdo = $this->dbutils->openDbAndReturnPdo();
$isCorrect = $this->isPasswordCorrect($pdo,$pass,false);
if ($isCorrect) {
$pdo = $this->dbutils->openDbAndReturnPdo();
$this->saveLastPrintServerAccess($pdo);
$sql = "SELECT type FROM %printjobs% WHERE id=?";
$result = CommonUtils::fetchSqlAll($pdo, $sql, array($id));
if (count($result) > 0) {
$type = $result[0]['type'];
if (($type == self::$FOOD) || ($type == self::$DRINK) || ($type == self::$CANCELFOOD) || ($type == self::$CANCELDRINK) || ($type == self::$PICKUP)) {
$sql = "UPDATE %printjobs% SET removed=? WHERE id=?";
CommonUtils::execSql($pdo, $sql, array(1,$id));
} else {
$sql = "DELETE FROM %printjobs% WHERE id=?";
CommonUtils::execSql($pdo, $sql, array($id));
}
}
echo json_encode(array("status" => "OK", "code" => OK, "msg" => "Druckauftrag erfolgreich gelöscht."));
} else {
CommonUtils::log($pdo,"PRINTQUEUE", "deletePrintJob: wrong printcode");
echo json_encode(array("status" => "ERROR", "code" => ERROR_NOT_AUTHOTRIZED, "msg" => ERROR_NOT_AUTHOTRIZED_MSG));
}
}
}