ordersprinter/webapp/php/dbutils.php

333 lines
15 KiB
PHP
Raw Permalink Normal View History

2020-11-19 22:44:19 +01:00
<?php
// Datenbank-Verbindungsparameter
require_once ('config.php');
class DbUtils {
2020-11-19 22:47:44 +01:00
private static $timezone = null;
private static $prefix = null;
2020-11-19 23:11:36 +01:00
private static $dbname = null;
2020-11-19 22:47:44 +01:00
2020-11-19 23:13:59 +01:00
public static $WORKFLOW_DIGITAL_AND_WORK = 0;
public static $WORKFLOW_ONLY_DIGITAL = 1;
public static $WORKFLOW_ONLY_WORK = 2;
public static $WORKFLOW_WORK_WITH_SERVER = 3;
2020-11-19 23:14:48 +01:00
public static $PRICE_TYPE_BASE = 0;
public static $PRICE_TYPE_DICOUNT = 1;
public static $PRICE_TYPE_EXTRA_AMOUNT = 2;
public static $PROCESSTYPE_BELEG = 1;
public static $PROCESSTYPE_VORGANG = 2;
public static $PROCESSTYPE_SONSTIGER_VORGANG = 3;
public static $OPERATION_IN_BILL_TABLE = 1;
public static $OPERATION_IN_QUEUE_TABLE = 2;
public static $OPERATION_IN_CLOSING_TABLE = 3;
public static $NO_TSE = 0;
public static $TSE_OK = 1;
public static $TSE_KNOWN_ERROR = 2;
public static $TSE_RUNTIME_ERROR = 3;
public static $TSE_MISCONFIG = 4;
public static $OSLABEL = "OrderSprinter";
public static $OSVERSLABEL = "Version";
public static $ORDERTYPE_PRODUCT = 1;
public static $ORDERTYPE_1ZweckKauf = 2;
public static $ORDERTYPE_1ZweckEinl = 3;
2020-11-19 23:13:59 +01:00
2020-11-19 22:47:44 +01:00
public static function overruleTimeZone($timezone) {
self::$timezone = $timezone;
}
public static function overrulePrefix($prefix) {
self::$prefix = $prefix;
}
2020-11-19 23:11:36 +01:00
public static function overruleDbName($dbname) {
self::$dbname = $dbname;
}
public static function getDbName() {
$db = MYSQL_DB;
if (!is_null(self::$dbname)) {
$db = self::$dbname;
}
return $db;
}
2020-11-19 22:47:44 +01:00
2020-11-19 23:12:46 +01:00
public static function openDbAndReturnPdoStatic ($doEchoError = true) {
2020-11-19 22:47:44 +01:00
$dsn = 'mysql:host=' . MYSQL_HOST . ';dbname=' . MYSQL_DB;
$user = MYSQL_USER;
$password = MYSQL_PASSWORD;
$pdo = null;
try {
$pdo = new PDO($dsn, $user, $password);
$pdo ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
2020-11-19 23:15:07 +01:00
$sql = "SET SESSION sql_mode = 'STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION'";
CommonUtils::execSql($pdo, $sql, null);
2020-11-19 22:47:44 +01:00
}
catch (PDOException $e) {
2020-11-19 23:12:46 +01:00
if ($doEchoError) {
echo 'Connection failed: ' . $e->getMessage();
}
2020-11-19 22:47:44 +01:00
}
return $pdo;
}
2020-11-19 22:44:19 +01:00
function openDbAndReturnPdo () {
2020-11-19 22:59:47 +01:00
$dsn = 'mysql:host=' . MYSQL_HOST . ';dbname=' . MYSQL_DB;
$user = MYSQL_USER;
2020-11-19 22:44:19 +01:00
$password = MYSQL_PASSWORD;
2020-11-19 22:59:47 +01:00
$pdo = null;
try {
$pdo = new PDO($dsn, $user, $password);
$pdo ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
2020-11-19 22:44:19 +01:00
}
return $pdo;
}
function testDbAccess($host,$dbname,$user,$pass) {
2020-11-19 22:59:47 +01:00
$dsn = 'mysql:host=' . $host . ';dbname=' . $dbname;
$password = $pass;
$pdo = null;
try {
$pdo = new PDO($dsn, $user, $password);
$pdo ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
//
2020-11-19 22:44:19 +01:00
}
if ($pdo != null) {
return true;
} else {
return false;
}
}
/*
* To use sql strings that are easy to read the table names are used
* without variables. But since the user can specify a prefix for all
* tables the substitution must be done somewhere. This is the function
* that replaces the %TABLE% by $prefix_table
*/
2020-11-19 22:47:44 +01:00
public static function substTableAlias($sqlString) {
$prefix = TAB_PREFIX;
if (!is_null(self::$prefix)) {
$prefix = self::$prefix;
}
2020-11-19 23:02:12 +01:00
return self::substTableAliasCore($sqlString, $prefix);
}
public static function substTableAliasCore($sqlString,$prefix) {
2020-11-19 22:47:44 +01:00
$out = str_replace("%queue%",$prefix . 'queue',$sqlString);
$out = str_replace("%products%",$prefix . 'products',$out);
$out = str_replace("%user%",$prefix . 'user',$out);
$out = str_replace("%room%",$prefix . 'room',$out);
$out = str_replace("%resttables%",$prefix . 'resttables',$out);
2020-11-19 23:02:08 +01:00
$out = str_replace("%bill%",$prefix . 'bill',$out);
2020-11-19 23:02:12 +01:00
$out = str_replace("%customerlog%", $prefix . 'customerlog', $out);
2020-11-19 23:02:08 +01:00
$out = str_replace("%customers%",$prefix . 'customers',$out);
$out = str_replace("%groups%",$prefix . 'groups',$out);
$out = str_replace("%groupcustomer%",$prefix . 'groupcustomer',$out);
$out = str_replace("%vacations%", $prefix .'vacations',$out);
2020-11-19 22:47:44 +01:00
$out = str_replace("%tablemaps%",$prefix . "tablemaps",$out);
$out = str_replace("%tablepos%",$prefix . "tablepos",$out);
$out = str_replace("%pricelevel%",$prefix . 'pricelevel',$out);
$out = str_replace("%config%",$prefix . 'config',$out);
$out = str_replace("%closing%",$prefix . 'closing',$out);
$out = str_replace("%printjobs%",$prefix . 'printjob',$out);
$out = str_replace("%hist%",$prefix . 'hist',$out);
$out = str_replace("%histprod%",$prefix . 'histprod',$out);
$out = str_replace("%histconfig%",$prefix . 'histconfig',$out);
$out = str_replace("%histuser%",$prefix . 'histuser',$out);
$out = str_replace("%histactions%",$prefix . 'histactions',$out);
$out = str_replace("%payment%",$prefix . 'payment',$out);
$out = str_replace("%billproducts%", $prefix . 'billproducts',$out);
$out = str_replace("%work%",$prefix . 'work',$out);
$out = str_replace("%comments%",$prefix . 'comments',$out);
2020-11-19 23:00:58 +01:00
$out = str_replace("%hsin%",$prefix . 'hsin',$out);
$out = str_replace("%hsout%",$prefix . 'hsout',$out);
2020-11-19 22:47:44 +01:00
$out = str_replace("%reservations%",$prefix . 'reservations',$out);
$out = str_replace("%logo%",$prefix . 'logo',$out);
2020-11-19 23:00:18 +01:00
$out = str_replace("%log%",$prefix . 'log',$out);
2020-11-19 22:47:44 +01:00
$out = str_replace("%extras%", $prefix . 'extras',$out);
$out = str_replace("%extrasprods%",$prefix . 'extrasprods', $out);
$out = str_replace("%queueextras%",$prefix . 'queueextras', $out);
$out = str_replace("%ratings%",$prefix . 'ratings', $out);
2020-11-19 23:03:35 +01:00
$out = str_replace("%prodimages%",$prefix . 'prodimages', $out);
2020-11-19 23:03:48 +01:00
$out = str_replace("%roles%",$prefix . 'roles', $out);
2020-11-19 23:10:26 +01:00
$out = str_replace("%recordsqueue%",$prefix . 'recordsqueue', $out);
$out = str_replace("%records%",$prefix . 'records', $out);
2020-11-19 23:11:27 +01:00
$out = str_replace("%times%",$prefix . 'times', $out);
2020-11-19 23:11:33 +01:00
$out = str_replace("%tasks%",$prefix . 'tasks', $out);
$out = str_replace("%taskhist%",$prefix . 'taskhist', $out);
2020-11-19 23:14:48 +01:00
$out = str_replace("%tsevalues%",$prefix . 'tsevalues', $out);
$out = str_replace("%operations%",$prefix . 'operations', $out);
$out = str_replace("%terminals%",$prefix . 'terminals', $out);
$out = str_replace("%counting%",$prefix . 'counting', $out);
$out = str_replace("%vouchers%",$prefix . 'vouchers', $out);
2020-11-19 23:03:35 +01:00
2020-11-19 23:14:16 +01:00
$out = str_replace("%testchk%",$prefix . 'testchk',$out);
2020-11-19 22:47:44 +01:00
return (str_replace("%prodtype%",$prefix . 'prodtype',$out));
}
public function resolveTablenamesInSqlString($sqlString) {
return DbUtils::substTableAlias($sqlString);
}
public static function getTimeZone() {
if (is_null(self::$timezone)) {
if(session_id() == '') {
session_start();
}
if (isset($_SESSION['timezone'])) {
return $_SESSION['timezone'];
} else {
return "Europe/Berlin";
}
} else {
return self::$timezone;
}
2020-11-19 22:44:19 +01:00
}
2020-11-19 22:59:47 +01:00
2020-11-19 23:12:59 +01:00
public static function getTimeZoneDb($pdo) {
if (is_null($pdo)) {
return "Europe/Berlin";
}
try {
return CommonUtils::getConfigValue($pdo, 'timezone', "Europe/Berlin");
} catch (Exception $ex) {
return "Europe/Berlin";
}
}
2020-11-19 22:59:47 +01:00
public static $userCols = array(
array("col" => 'id', "hist" => 1, "new" => null, "update" => null),
array("col" => 'username', "hist" => 1, "new" => 'username', "update" => null),
array("col" => 'userpassword', "hist" => 0, "new" => null, "update" => null),
array("col" => 'is_admin', "hist" => 1, "new" => 'isAdmin', "update" => 'isAdmin'),
array("col" => 'right_waiter', "hist" => 1, "new" => 'rWaiter', "update" => 'rWaiter'),
array("col" => 'right_kitchen', "hist" => 1, "new" => 'rKitchen', "update" => 'rKitchen'),
array("col" => 'right_bar', "hist" => 1, "new" => 'rBar', "update" => 'rBar'),
array("col" => 'right_supply', "hist" => 1, "new" => 'rSupply', "update" => 'rSupply'),
array("col" => 'right_paydesk', "hist" => 1, "new" => 'rPayDesk', "update" => 'rPayDesk'),
array("col" => 'right_statistics', "hist" => 1, "new" => 'rStat', "update" => 'rStat'),
array("col" => 'right_bill', "hist" => 1, "new" => 'rBill', "update" => 'rBill'),
array("col" => 'right_products', "hist" => 1, "new" => 'rProducts', "update" => 'rProducts'),
array("col" => 'right_reservation', "hist" => 1, "new" => 'rReservation', "update" => 'rReservation'),
array("col" => 'right_rating', "hist" => 1, "new" => 'rRating', "update" => 'rRating'),
array("col" => 'right_changeprice', "hist" => 1, "new" => 'rChangeprice', "update" => 'rChangeprice'),
2020-11-19 23:02:08 +01:00
array("col" => 'right_customers', "hist" => 1, "new" => 'rCustomers', "update" => 'rCustomers'),
2020-11-19 23:11:52 +01:00
array("col" => 'right_pickups', "hist" => 1, "new" => 'rPickups', "update" => 'rPickups'),
2020-11-19 22:59:47 +01:00
array("col" => 'right_manager', "hist" => 1, "new" => 'rManager', "update" => 'rManager'),
array("col" => 'right_closing', "hist" => 1, "new" => 'rClosing', "update" => 'rClosing'),
2020-11-19 23:03:20 +01:00
array("col" => 'right_dash', "hist" => 1, "new" => 'rDash', "update" => 'rDash'),
2020-11-19 23:11:27 +01:00
array("col" => 'right_timetracking',"hist" => 1, "new" => 'rTimetracking', "update" => 'rTimetracking'),
array("col" => 'right_timemanager', "hist" => 1, "new" => 'rTimemanager', "update" => 'rTimemanager'),
2020-11-19 23:11:33 +01:00
array("col" => 'right_tasks', "hist" => 1, "new" => 'rTasks', "update" => 'rTasks'),
array("col" => 'right_tasksmanagement',"hist" => 1,"new" => 'rTasksmanagement', "update" => 'rTasksmanagement'),
2020-11-19 23:13:59 +01:00
array("col" => 'quickcash', "hist" => 1, "new" => null, "update" => null),
2020-11-19 22:59:47 +01:00
array("col" => 'active', "hist" => 1, "new" => null ,"default" => 1, "update" => null),
2020-11-19 23:11:33 +01:00
array("col" => 'area', "hist" => 1, "new" => null ,"default" => null, "update" => null),
2020-11-19 22:59:47 +01:00
array("col" => 'lastmodule', "hist" => 0, "new" => null ,"default" => null, "update" => null),
array("col" => 'ordervolume', "hist" => 0, "new" => null ,"default" => null, "update" => null),
array("col" => 'language', "hist" => 0, "new" => null, "update" => null),
array("col" => 'receiptprinter', "hist" => 0, "new" => null ,"default" => null, "update" => null),
array("col" => 'roombtnsize', "hist" => 0, "new" => null ,"default" => null, "update" => null),
array("col" => 'tablebtnsize', "hist" => 0, "new" => null ,"default" => null, "update" => null),
array("col" => 'prodbtnsize', "hist" => 0, "new" => null ,"default" => null, "update" => null),
array("col" => 'prefertablemap', "hist" => 0, "new" => null ,"default" => 1, "update" => null),
2020-11-19 23:03:35 +01:00
array("col" => 'preferimgdesk', "hist" => 0, "new" => null ,"default" => null, "update" => null),
array("col" => 'preferimgmobile', "hist" => 0, "new" => null ,"default" => null, "update" => null),
2020-11-19 23:03:38 +01:00
array("col" => 'showplusminus', "hist" => 0, "new" => null ,"default" => null, "update" => null),
2020-11-19 22:59:47 +01:00
array("col" => 'keeptypelevel', "hist" => 0, "new" => null ,"default" => 0, "update" => null),
array("col" => 'extrasapplybtnpos', "hist" => 0, "new" => null ,"default" => 1, "update" => null)
);
public static $prodCols = array(
2020-11-19 23:00:42 +01:00
array("col" => 'id', "hist" => 1, "property" => "prodid"),
array("col" => 'shortname', "hist" => 1, "property" => "shortName"),
array("col" => 'longname', "hist" => 1, "property" => "longName"),
array("col" => 'priceA', "hist" => 1, "property" => "priceA"),
array("col" => 'priceB', "hist" => 1, "property" => "priceB"),
array("col" => 'priceC', "hist" => 1, "property" => "priceC"),
2020-11-19 23:12:18 +01:00
array("col" => 'barcode', "hist" => 1, "property" => "barcode"),
2020-11-19 23:02:49 +01:00
array("col" => 'unit', "hist" => 1, "property" => "unit"),
array("col" => 'days', "hist" => 1, "property" => "days"),
2020-11-19 23:00:42 +01:00
array("col" => 'tax', "hist" => 1, "property" => "tax"),
2020-11-19 23:14:48 +01:00
array("col" => 'togotax', "hist" => 1, "property" => "togotax"),
2020-11-19 23:00:42 +01:00
array("col" => 'taxaustria', "hist" => 1, "property" => "taxaustria"),
array("col" => 'amount', "hist" => 0, "property" => "amount"),
array("col" => 'category', "hist" => 0, "property" => "category"),
array("col" => 'favorite', "hist" => 1, "property" => "favorite"),
2020-11-19 22:59:47 +01:00
array("col" => 'sorting', "hist" => 1),
2020-11-19 23:00:42 +01:00
array("col" => 'available', "hist" => 1, "property" => "available"),
array("col" => 'audio', "hist" => 1, "property" => "audio"),
2020-11-19 23:03:35 +01:00
array("col" => 'prodimageid', "hist" => 1, "property" => "prodimageid"),
2020-11-19 23:10:06 +01:00
array("col" => 'display', "hist" => 1, "property" => "display"),
2020-11-19 22:59:47 +01:00
array("col" => 'removed', "hist" => 0)
);
2020-11-19 23:14:16 +01:00
private static function dropDBTable($pdo,$tablename) {
try {
CommonUtils::execSql($pdo, "DROP TABLE $tablename", null);
return true;
} catch (Exception $ex) {
return false;
}
}
public static function checkForInstallUpdateDbRights($pdo) {
$tableexists = false;
try {
$result = CommonUtils::fetchSqlAll($pdo, "SELECT 1 from %testchk% LIMIT 1", null);
if (count($result) >= 0) {
$tableexists = true;
}
} catch (Exception $ex) {
$tableexists = false;
}
if ($tableexists) {
$ok = self::dropDBTable($pdo, '%testchk%');
if (!$ok) {
return array("status" => "OK","msg" => array("DROP"),"ok" => 0);
}
}
try {
$sql = "CREATE TABLE `%testchk%` (`id` INT (3)) CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = InnoDb";
CommonUtils::execSql($pdo, $sql, null);
} catch (Exception $ex) {
return array("status" => "OK","msg" => array("CREATE"),"ok" => 0);
}
$missingRights = array();
try {
CommonUtils::execSql($pdo, "ALTER TABLE %testchk% ADD testfield INT(1) NULL DEFAULT '0' AFTER id", null);
} catch (Exception $ex) {
$missingRights[] = "ALTER";
}
try {
CommonUtils::execSql($pdo, "INSERT INTO %testchk% (id,testfield) VALUES(?,?)", array(1,2));
} catch (Exception $ex) {
$missingRights[] = "INSERT";
}
try {
CommonUtils::execSql($pdo, "UPDATE %testchk% SET testfield=? WHERE id=?", array(10,1));
} catch (Exception $ex) {
$missingRights[] = "UPDATE";
}
$ok = self::dropDBTable($pdo, '%testchk%');
if (!$ok) {
$missingRights[] = "DROP";
}
if (count($missingRights) == 0) {
return array("status" => "OK","msg" => $missingRights,"ok" => 1);
} else {
return array("status" => "OK","msg" => $missingRights,"ok" => 0);
}
}
2020-11-19 23:02:12 +01:00
}