ordersprinter/gastsystem/sync.php

106 lines
3.1 KiB
PHP

<?php
require_once 'php/dbutils.php';
require_once 'php/config.php';
// REM* wget "http://localhost/bestellung/php/contenthandler.php?module=guestsync&command=sync"
class Sync {
public static function insertSystemData($pdo,$systemdata) {
try {
// REM* check for guestcode
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');
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));
}
}
// REM* update that an access from OrderSprinter core webapp has taken place
private static function updateOsAccessStatus($pdo,$timezone) {
date_default_timezone_set($timezone);
$date = new DateTime();
// REM* seconds since Jan 01 1970. (UTC)
$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!";
}