ordersprinter/gastsystem/php/queue.php

64 lines
1.9 KiB
PHP

<?php
require_once 'dbutils.php';
require_once 'config.php';
defined('DB') || define ( 'DB','mysql' );
require_once 'ossystem.php';
class Queue {
public static function putintoqueue($pdo,$ordertime,$tableid,$tablecode,$dailycode,$orders) {
$timezone = DbUtils::getConfigItem($pdo, "timezone", "Europe/Berlin");
date_default_timezone_set($timezone);
$ordertime = date('Y-m-d H:i:s');
if (DB == "mysql") {
$pdo->beginTransaction();
$sql = "INSERT INTO %queue% (date,prodid,tableid,tablecode,dailycode) VALUES(?,?,?,?,?)";
foreach($orders as $anOrder) {
$prodid = $anOrder["prodid"];
DbUtils::execSql($pdo, $sql, array($ordertime,$prodid,$tableid,$tablecode,$dailycode));
$pdo->commit();
DbUtils::log("Added product with id $prodid to mysql queue table");
}
return array("status" => "OK");
} else {
$filename = dirname(__FILE__) . '/../db/' . QUEUE_FILE;
try {
$queueItems = array();
if (file_exists($filename)) {
$queueItemsFileContent = file_get_contents($filename);
$queueItems = json_decode($queueItemsFileContent,true);
}
foreach($orders as $anOrder) {
$prodid = $anOrder["prodid"];
$queueItems[] = array("date" => $ordertime,"prodid" => $prodid,"tableid" => $tableid, "tablecode" => $tablecode, "dailycode" => $dailycode);
file_put_contents($filename, json_encode($queueItems));
DbUtils::log("Add product with id $prodid to file queue");
}
return array("status" => "OK");
} catch (Exception $ex) {
return array("status" => "ERROR","msg" => $ex->getMessage());
}
}
}
}
if (isset($_GET["command"])) {
$command = $_GET["command"];
$pdo = null;
if (DB == "mysql") {
$pdo = DbUtils::openDbAndReturnPdoStatic();
}
switch ($command) {
case "putintoqueue":
$ret = Queue::putintoqueue($pdo,$_POST["ordertime"],$_POST["tableid"],$_POST["tablecode"],$_POST["dailycode"],$_POST["orders"]);
echo json_encode($ret);
break;
default:
break;
}
}