ordersprinter/gastsystem/sync.php

119 lines
3.5 KiB
PHP
Raw Normal View History

2020-11-19 23:10:06 +01:00
<?php
require_once 'php/dbutils.php';
require_once 'php/config.php';
class Sync {
public static function insertSystemData($pdo,$systemdata) {
try {
if (!isset($systemdata["guestcode"])) {
return array("status" => "ERROR","msg" => "No Access code transmitted!");
}
$transmittedGuestcode = $systemdata["guestcode"];
if ($transmittedGuestcode != CODE) {
return array("status" => "ERROR","msg" => "Wrong Access code!");
}
self::insertIntoSystemTable($pdo, $systemdata, 'timezone');
self::insertIntoSystemTable($pdo, $systemdata, 'dailycode');
self::insertIntoSystemTable($pdo, $systemdata, 'resttables');
self::insertIntoSystemTable($pdo, $systemdata, 'types');
self::insertIntoSystemTable($pdo, $systemdata, 'products');
self::insertIntoSystemTable($pdo, $systemdata, 'currency');
self::insertIntoSystemTable($pdo, $systemdata, 'decpoint');
2020-11-19 23:10:21 +01:00
self::insertIntoSystemTableWithCheck($pdo, $systemdata, 'askdaycode');
self::insertIntoSystemTableWithCheck($pdo, $systemdata, 'asktablecode');
self::insertIntoSystemTableWithCheck($pdo, $systemdata, 'guesttimeout');
2020-11-19 23:10:06 +01:00
if (isset($systemdata["timezone"])) {
self::updateOsAccessStatus($pdo, $systemdata["timezone"]);
}
return array("status" => "OK");
} catch (Exception $ex) {
return array("status" => "ERROR","msg" => $ex->getMessage());
}
}
private static function insertIntoSystemTable($pdo,$data,$item) {
if (isset($data[$item])) {
$value = $data[$item];
$sql = "UPDATE %ossystem% SET value=? WHERE item=?";
DbUtils::execSql($pdo, $sql, array($value,$item));
}
}
2020-11-19 23:10:21 +01:00
private static function insertIntoSystemTableWithCheck($pdo,$data,$item) {
if (isset($data[$item])) {
$sql = "SELECT id FROM %ossystem% WHERE item=?";
$result = DbUtils::fetchSqlAll($pdo, $sql, array($item));
if (count($result) == 0) {
$sql = "INSERT INTO %ossystem% (item,value) VALUES(?,?)";
DbUtils::execSql($pdo, $sql, array($item,$data[$item]));
} else {
self::insertIntoSystemTable($pdo, $data, $item);
}
}
}
2020-11-19 23:10:06 +01:00
private static function updateOsAccessStatus($pdo,$timezone) {
date_default_timezone_set($timezone);
$date = new DateTime();
$currentTimeStamp = $date->getTimestamp();
$sql = "SELECT id FROM %gueststatus% WHERE item=?";
$result = DbUtils::fetchSqlAll($pdo, $sql, array('lastosaccess'));
if (count($result) == 0) {
$sql = "INSERT INTO %gueststatus% (date,item) VALUES(?,?)";
} else {
$sql = "UPDATE %gueststatus% SET date=? WHERE item=?";
}
DbUtils::execSql($pdo, $sql, array($currentTimeStamp,'lastosaccess'));
}
public static function fetchQueueData($pdo) {
// id | date | tableid | prodid | tablecode | dailycode
$pdo->beginTransaction();
$sql = "SELECT id,date,tableid,prodid,tablecode,dailycode FROM %queue%";
$result = DbUtils::fetchSqlAll($pdo, $sql, null);
$sql = "DELETE FROM %queue%";
DbUtils::execSql($pdo, $sql, null);
$pdo->commit();
return $result;
}
}
//ob_start();
//echo "111";
//var_dump($_POST);
//$result = ob_get_clean();
//echo "RESULT: $result ENDE";
//
//return;
if (isset($_POST["data"])) {
$pdo = DbUtils::openDbAndReturnPdoStatic();
$data = $_POST["data"];
$utfdata = base64_decode($data);
$objFormat = json_decode($utfdata, true);
$ret = Sync::insertSystemData($pdo,$objFormat);
if ($ret["status"] != "OK") {
echo json_encode($ret);
} else {
$queuedData = Sync::fetchQueueData($pdo);
$ret = array("status" => "OK","msg" => $queuedData);
echo json_encode($ret);
}
} else {
echo "No data transmitted!";
}