ordersprinter/webapp/php/guestsync.php

323 lines
9.5 KiB
PHP
Raw Permalink Normal View History

2020-11-19 23:10:06 +01:00
<?php
require_once ('dbutils.php');
require_once ('commonutils.php');
require_once 'queuecontent.php';
class Guestsync {
public static function handleCommand($command) {
$pdo = DbUtils::openDbAndReturnPdoStatic();
if($command == 'sync') {
$ret = self::sync($pdo);
echo json_encode($ret);
}
}
public static function sync($pdo) {
if (!self::shallWeSync($pdo)) {
return array("status" => "OK","msg" => '');
}
$url = CommonUtils::getConfigValue($pdo, 'guesturl', "");
if (is_null($url)) {
return array("status" => "OK","msg" => "");
} else {
$url = trim($url);
if ($url == "") {
return array("status" => "OK","msg" => "");
}
}
$guestcode = trim(CommonUtils::getConfigValue($pdo, 'guestcode', ''));
if ($guestcode == '') {
$ret = array("status" => "ERROR","msg" => "Guest system access code not set - stopping here for security reasons!");
return $ret;
}
$timezone = CommonUtils::getConfigValue($pdo, 'timezone', "Europe/Berlin");
$dailycode = CommonUtils::getConfigValue($pdo, 'dailycode', "dfhdztfghgjzt");
$resttables = json_encode(Roomtables::getTablesForGuestsystem($pdo));
// id,name,code in an array
$prodTypes = self::getMenuForGuests($pdo);
$types = json_encode($prodTypes["types"]);
$products = json_encode($prodTypes["products"]);
$currency = CommonUtils::getConfigValue($pdo, 'currency', 'Euro');
2020-11-19 23:15:14 +01:00
$guesttheme = CommonUtils::getConfigValue($pdo, 'guesttheme', 8);
2020-11-19 23:10:06 +01:00
$decpoint = CommonUtils::getConfigValue($pdo, 'decpoint', '.');
2020-11-19 23:10:21 +01:00
$askdaycode = CommonUtils::getConfigValue($pdo, 'askdaycode', "1");
$asktablecode = CommonUtils::getConfigValue($pdo, 'asktablecode', "1");
$guesttimeout = CommonUtils::getConfigValue($pdo, 'guesttimeout', "5");
2020-11-19 23:12:37 +01:00
$sql = "SELECT setting from %logo% WHERE name=? HAVING setting is not null";
$logoImg = CommonUtils::fetchSqlAll($pdo, $sql, array("logoimg"));
if (count($logoImg) > 0) {
$logo = base64_encode($logoImg[0]["setting"]);
} else {
$logo = '';
}
$prodimages = self::getImagesForGuestProducs($pdo);
2020-11-19 23:10:06 +01:00
$transferdata = array(
"timezone" => $timezone,
"dailycode" => $dailycode,
"resttables" => $resttables,
"guestcode" => $guestcode,
"types" => $types,
"products" => $products,
"currency" => $currency,
2020-11-19 23:15:14 +01:00
"guesttheme" => $guesttheme,
2020-11-19 23:10:21 +01:00
"decpoint" => $decpoint,
"askdaycode" => $askdaycode,
"asktablecode" => $asktablecode,
2020-11-19 23:12:37 +01:00
"guesttimeout" => $guesttimeout,
"logo" => $logo,
"prodimages" => $prodimages
2020-11-19 23:10:06 +01:00
);
$data = json_encode($transferdata);
$transferdataBase64 = base64_encode($data);
$guestorders = self::sendToGuestsystem($pdo,$url, $transferdataBase64);
$i=0;
}
private static function sendToGuestsystem($pdo,$url, $data) {
$url .= "/sync.php";
$query = http_build_query(array("data" => $data));
$opts = array(
'http' => array(
2020-11-19 23:14:48 +01:00
'header' =>
"Content-Type: application/x-www-form-urlencoded\r\n" .
2020-11-19 23:10:06 +01:00
"Content-Length: " . strlen($query) . "\r\n" .
"User-Agent:MyAgent/1.0\r\n",
'method' => 'POST',
'content' => $query
)
);
$context = stream_context_create($opts);
$ret = file_get_contents($url, false, $context);
if (!$ret) {
$ret = array("status" => "ERROR","msg" => "Communication with guest system not successful!");
}
self::insertWorkDataFromGuestSystem($pdo, $ret);
}
private static function insertWorkDataFromGuestSystem($pdo,$dataJson) {
2020-11-19 23:12:39 +01:00
$dataDecoded = json_decode($dataJson,true);
if ($dataDecoded["status"] == "OK") {
2020-11-19 23:10:06 +01:00
try {
2020-11-19 23:12:39 +01:00
$entries = $dataDecoded["msg"];
2020-11-19 23:10:06 +01:00
foreach($entries as $entry) {
$date = $entry["date"];
$tableid = $entry["tableid"];
$prodid = $entry["prodid"];
$dailycode = $entry["dailycode"];
$tablecode = $entry["tablecode"];
$permission = self::checkPermission($pdo, $tableid, $tablecode, $prodid, $dailycode);
if ($permission) {
2020-11-19 23:15:07 +01:00
$success = self::addToQueue($pdo,$date,$prodid,$tableid);
if (!$success) {
return false;
}
2020-11-19 23:10:06 +01:00
}
}
} catch (Exception $ex) {
echo $ex->getMessage();
2020-11-19 23:15:07 +01:00
return false;
2020-11-19 23:10:06 +01:00
}
}
2020-11-19 23:15:07 +01:00
return true;
2020-11-19 23:10:06 +01:00
}
private static function checkPermission($pdo,$tableid,$tablecode,$prodid,$dailycode) {
2020-11-19 23:10:21 +01:00
$askdaycode = CommonUtils::getConfigValue($pdo, 'askdaycode', 1);
if ($askdaycode == 1) {
$dailycodeInDb = trim(CommonUtils::getConfigValue($pdo, 'dailycode', ''));
if ($dailycode != trim($dailycodeInDb)) {
return false;
}
2020-11-19 23:10:06 +01:00
}
2020-11-19 23:10:21 +01:00
$asktablecode = CommonUtils::getConfigValue($pdo, 'asktablecode', 1);
if ($asktablecode == 1) {
2020-11-19 23:14:48 +01:00
$sql = "SELECT COALESCE(code,'') as code from %resttables% WHERE id=? AND allowoutorder=?";
2020-11-19 23:10:21 +01:00
$result = CommonUtils::fetchSqlAll($pdo, $sql, array($tableid,1));
if (count($result) == 0) {
return false;
}
$tablecodeInDb = trim($result[0]["code"]);
if ($tablecodeInDb != $tablecode) {
return false;
}
2020-11-19 23:10:06 +01:00
}
$sql = "SELECT id FROM %products% WHERE id=? AND removed is null AND (display is null OR display='KG' OR display='G')";
$result = CommonUtils::fetchSqlAll($pdo, $sql, array($prodid));
if (count($result) == 0) {
return false;
}
return true;
}
private static function shallWeSync($pdo) {
date_default_timezone_set(DbUtils::getTimeZone());
$date = new DateTime();
$currentTimeStamp = $date->getTimestamp();
$sql = "SELECT value FROM %work% WHERE item=?";
$result = CommonUtils::fetchSqlAll($pdo, $sql, array('lastsyncwithguest'));
if (count($result) == 0) {
$sql = "INSERT INTO %work% (item,value) VALUES(?,?)";
CommonUtils::execSql($pdo, $sql, array("lastsyncwithguest",$currentTimeStamp));
return true;
} else {
$lastaccess = $result[0]["value"];
if (($currentTimeStamp - $lastaccess) > 5) {
$sql = "UPDATE %work% SET value=? WHERE item=?";
CommonUtils::execSql($pdo, $sql, array($currentTimeStamp,"lastsyncwithguest"));
return true;
} else {
return false;
}
}
}
private static $typesWithContent = array();
2020-11-19 23:12:37 +01:00
private static function getImagesForGuestProducs($pdo) {
date_default_timezone_set(DbUtils::getTimeZone());
$dayofweek = date('N');
if ($dayofweek == 7) {
$dayofweek = 0;
}
2020-11-19 23:14:48 +01:00
$sql = "SELECT P.id as prodid,COALESCE(I.imgl,'-') as imagedata ";
2020-11-19 23:12:39 +01:00
$sql .= " FROM %products% P LEFT JOIN %prodimages% I ON P.prodimageid=I.id ";
$sql .= " WHERE P.available='1' AND P.removed is null ";
2020-11-19 23:12:37 +01:00
$sql .= " AND (days is null OR days like ?) AND (display = 'KG' OR display = 'G' OR display is null) ";
$sql .= " AND (unit is null OR unit='0') ";
2020-11-19 23:12:39 +01:00
2020-11-19 23:12:37 +01:00
$allProductImgs = CommonUtils::fetchSqlAll($pdo, $sql, array("%$dayofweek%"));
return $allProductImgs;
}
2020-11-19 23:10:06 +01:00
private static function getMenuForGuests($pdo) {
date_default_timezone_set(DbUtils::getTimeZone());
$dayofweek = date('N');
if ($dayofweek == 7) {
$dayofweek = 0;
}
$pricelevel = CommonUtils::getConfigValue($pdo, "pricelevel", 0);
$priceTxt = "priceA";
if ($pricelevel == 2) {
$priceTxt = "priceB";
} else if ($pricelevel == 3) {
$priceTxt = "priceC";
}
2020-11-19 23:14:48 +01:00
$sql = "select id,longname,category as ref,$priceTxt as price,COALESCE(unit,0) as unit,IF(days is not null, days, '0123456') as days ";
2020-11-19 23:10:06 +01:00
$sql .= " from %products% where available='1' AND removed is null AND (days is null OR days like ?) AND (display = 'KG' OR display = 'G' OR display is null) ";
$sql .= " AND (unit is null OR unit='0') ";
$sql .= " ORDER BY longname";
$allProducts = CommonUtils::fetchSqlAll($pdo, $sql, array("%$dayofweek%"));
2020-11-19 23:14:48 +01:00
$sql = "select id,name,COALESCE(reference,0) as reference,sorting from %prodtype% where removed is null";
2020-11-19 23:10:06 +01:00
$allTypes = CommonUtils::fetchSqlAll($pdo, $sql, null);
$filteredTypes = self::filterUsedTypes($allTypes, $allProducts);
return array("products" => $allProducts,"types" => $filteredTypes);
}
private static function filterUsedTypes($types,$products) {
self::$typesWithContent = array();
foreach ($products as $p) {
$ref = $p["ref"];
$typeOfProd = self::getTypeOfId($types, $ref);
if (!is_null($typeOfProd)) {
self::declareProdTypeAndParentsInUse($types, $typeOfProd);
}
}
$out = array();
$keys = array_keys(self::$typesWithContent);
foreach($keys as $aKey) {
$t = self::$typesWithContent[$aKey];
2020-11-19 23:10:26 +01:00
$out[] = array("id" => $t["id"],"reference" => $t["reference"],"name" => $t["name"],"sorting" => $t["sorting"]);
2020-11-19 23:10:06 +01:00
}
2020-11-19 23:10:26 +01:00
usort($out,"Products::cmptypes");
2020-11-19 23:10:06 +01:00
return $out;
}
private static function getTypeOfId($alltypes,$typeid) {
foreach($alltypes as $t) {
if ($t["id"] == $typeid) {
return $t;
}
}
return null;
}
private static function declareProdTypeAndParentsInUse($alltypes,$aType) {
$typeid = $aType["id"];
$reference = $aType["reference"];
2020-11-19 23:10:26 +01:00
$sorting = $aType["sorting"];
2020-11-19 23:10:06 +01:00
if (!array_key_exists($typeid, self::$typesWithContent)) {
2020-11-19 23:10:26 +01:00
self::$typesWithContent[$typeid] = array("id" => $typeid,"name" => $aType["name"],"reference" => $reference, "sorting" => $sorting);
2020-11-19 23:10:06 +01:00
$parent = null;
foreach($alltypes as $a) {
$typeid = $a["id"];
if ($typeid == $reference) {
$parent = $a;
break;
}
}
if (!is_null($parent)) {
self::declareProdTypeAndParentsInUse($alltypes, $parent);
}
}
}
private static function addToQueue($pdo,$date,$prodid,$tableid) {
$printjob = CommonUtils::getConfigValue($pdo, 'guestjobprint', 1);
if (is_null($printjob)) {
$printjob = 1;
}
$queue = new QueueContent();
2020-11-19 23:15:07 +01:00
$success = $queue->addProductListToQueueForGuest($pdo, $date, $tableid, $prodid, $printjob);
if (!$success) {
return false;
}
2020-11-19 23:12:53 +01:00
if ($printjob == 0) {
QueueContent::setFlagForCooking($pdo,$prodid);
}
2020-11-19 23:15:07 +01:00
return true;
2020-11-19 23:10:06 +01:00
}
2020-11-19 23:15:14 +01:00
}