ordersprinter/webapp/php/products.php

1932 lines
70 KiB
PHP
Raw Permalink Normal View History

2020-11-19 22:47:44 +01:00
<?php
// Datenbank-Verbindungsparameter
require_once ('dbutils.php');
require_once ('queuecontent.php');
require_once ('commonutils.php');
require_once ('utilities/userrights.php');
require_once ('utilities/HistFiller.php');
require_once ('utilities/sorter.php');
2020-11-19 22:59:47 +01:00
require_once ('utilities/TypeAndProducts/ProductEntry.php');
2020-11-19 22:47:44 +01:00
2020-11-19 23:03:56 +01:00
2020-11-19 22:47:44 +01:00
class Products {
var $dbutils;
var $queue;
var $commonUtils;
var $userrights;
var $histfiller;
var $sorter;
function __construct() {
$this->dbutils = new DbUtils();
$this->queue = new QueueContent();
$this->commonUtils = new CommonUtils();
$this->userrights = new Userrights();
$this->histfiller = new HistFiller();
$this->sorter = new Sorter();
}
function isCmdAllowed($rights) {
if ($this->userrights->isCmdAllowedForUser($rights)) {
return true;
} else {
echo json_encode(array("status" => "ERROR", "code" => ERROR_NOT_AUTHOTRIZED, "msg" => ERROR_NOT_AUTHOTRIZED_MSG));
return false;
}
}
2020-11-19 23:10:48 +01:00
function handleCommand($command) {
2020-11-19 22:47:44 +01:00
2020-11-19 23:03:35 +01:00
$cmdArray = array('showDbProducts', 'getMenuLevelUp', 'applySingleProdData', 'createExtra', 'applyExtra', 'upExtra', 'delExtra','sortup','sortdown', 'delproduct',
'reassign', 'applyType', 'delType', 'getSingleProdData', 'getSingleTypeData', 'getPriceLevelInfo','setPriceLevelInfo', 'createProduct','createProdType',
2020-11-19 23:11:27 +01:00
'addGeneralComment','changeGeneralComment','delGeneralComment','upGeneralComment','downGeneralComment','sortProdAlpha','getOnlyAllProds','loadprodimage','loadfullprodimageset',
2020-11-19 23:12:24 +01:00
'deleteImageProdAssignment','cleanprodimagestable','getkeynames','assignProdImageToKey','prodimghmlexport','getAllActiveProducts','changesetofproducts');
2020-11-19 22:47:44 +01:00
if (in_array($command, $cmdArray)) {
if (!($this->userrights->hasCurrentUserRight('right_products'))) {
if ($command == 'createProdType') {
echo json_encode(array("status" => "ERROR", "code" => ERROR_PRODUCTS_NOT_AUTHOTRIZED, "msg" => ERROR_PRODUCTS_NOT_AUTHOTRIZED_MSG));
} else {
echo "Benutzerrechte nicht ausreichend!";
}
return false;
}
}
2020-11-19 23:14:10 +01:00
2020-11-19 23:14:16 +01:00
//$cmdsForMasterDataChange = array('applySingleProdData', 'createExtra', 'applyExtra', 'delExtra','delproduct', 'reassign', 'applyType', 'delType', 'createProduct','createProdType','changesetofproducts');
$cmdsForMasterDataChange = array();
2020-11-19 23:14:10 +01:00
if (in_array($command, $cmdsForMasterDataChange)) {
$pdo = DbUtils::openDbAndReturnPdoStatic();
if (!CommonUtils::canMasterDataBeChanged($pdo)) {
echo json_encode(array("status" => "ERROR", "code" => ERROR_MASTERDATA, "msg" => "Stammdatenänderungen erfordern einen vorherigen Tagesabschluss"));
return;
}
$pdo = null;
}
2020-11-19 22:47:44 +01:00
if($command == 'showDbProducts') {
$this->showDbProducts();
} else if ($command == 'getMenu') {
if ($this->isCmdAllowed(R_PRO)) {
$this->getMenu($_GET['ref'],null);
}
} else if ($command == 'getMenuLevelUp') {
$this->getMenuLevelUp($_GET['ref']);
} else if ($command == 'getSpeisekarte') {
if ($this->userrights->hasCurrentUserRight('is_admin') || ($this->userrights->hasCurrentUserRight('right_manager'))) {
2020-11-19 22:59:47 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
echo json_encode($this->getSpeisekarte($pdo));
2020-11-19 22:47:44 +01:00
} else {
2020-11-19 22:59:47 +01:00
echo json_encode(array("status" => "ERROR", "code" => ERROR_NOT_AUTHOTRIZED, "msg" => ERROR_NOT_AUTHOTRIZED_MSG));
2020-11-19 22:47:44 +01:00
}
} else if ($command == 'exportCsv') {
if (($this->userrights->hasCurrentUserRight('is_admin'))
|| ($this->userrights->hasCurrentUserRight('right_manager'))) {
$this->exportCsv();
}
} else if ($command == 'getAllTypesAndAvailProds') {
$this->getAllTypesAndAvailProds();
} else if ($command == 'getAllAvailProdsAlphaSorted') {
$this->getAllAvailProdsAlphaSorted();
} else if ($command == 'getAllExtrasAlphaSorted') {
$this->getAllExtrasAlphaSorted();
} else if ($command == 'getSingleProdData') {
2020-11-19 22:54:51 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
$this->getSingleProdData($pdo,$_GET['id']);
2020-11-19 22:47:44 +01:00
} else if ($command == 'getSingleTypeData') {
$this->getSingleTypeData($_GET['id']);
} else if ($command == 'applySingleProdData') {
$extras = null;
if(isset($_POST['extras'])) {
$extras = $_POST['extras'];
}
2020-11-19 23:00:42 +01:00
$prodEntry = new ProductEntry();
$prodEntry->createFromPostData($_POST);
$this->applySingleProdData($prodEntry,$_POST['changeExtras'],$extras,$_POST['assignextrastotype']);
2020-11-19 22:47:44 +01:00
} else if ($command == 'createExtra') {
2020-11-19 23:12:39 +01:00
$this->createExtra($_POST['name'],$_POST['price'],$_POST['maxamount']);
2020-11-19 22:47:44 +01:00
} else if ($command == 'delExtra') {
$this->delExtra($_POST['id']);
} else if ($command == 'applyExtra') {
2020-11-19 23:12:39 +01:00
$this->applyExtra($_POST['name'],$_POST['price'],$_POST['maxamount'],$_POST['id']);
2020-11-19 23:02:57 +01:00
} else if ($command == 'upExtra') {
$this->upExtra($_POST['id']);
2020-11-19 22:47:44 +01:00
} else if ($command == 'sortup') {
$this->sortup($_POST['prodid']);
} else if ($command == 'sortdown') {
$this->sortdown($_POST['prodid']);
} else if ($command == 'delproduct') {
$this->delproduct($_POST['prodid']);
} else if ($command == 'createProduct') {
2020-11-19 23:00:42 +01:00
$prodEntry = new ProductEntry();
$prodEntry->createFromPostData($_POST);
$this->createProduct($prodEntry);
2020-11-19 22:47:44 +01:00
} else if ($command == 'reassign') {
$this->reassign($_POST['productid'],$_POST['typeid']);
} else if ($command == 'createProdType') {
$this->createProdType($_POST['refid'],$_POST['name']);
} else if ($command == 'applyType') {
2020-11-19 23:12:05 +01:00
$this->applyType($_POST['id'],$_POST['name'],$_POST['kind'],$_POST['usekitchen'],$_POST['usesupply'],$_POST['printer'],$_POST['fixbind']);
2020-11-19 22:47:44 +01:00
} else if ($command == 'delType') {
$this->delType($_POST['id']);
} else if ($command == 'getPriceLevelInfo') {
$this->getPriceLevelInfo();
} else if ($command == 'setPriceLevelInfo') {
$this->setPriceLevelInfo($_POST['priceLevelId']);
} else if ($command == 'getAudioFiles') {
$this->getAudioFiles();
} else if ($command == 'addGeneralComment') {
$this->addGeneralComment($_POST['comment']);
} else if ($command == 'getAllGeneralComments') {
if ($this->userrights->hasCurrentUserRight('is_admin') || ($this->userrights->hasCurrentUserRight('right_waiter')) || ($this->userrights->hasCurrentUserRight('right_products'))) {
$this->getAllGeneralComments();
} else {
echo "Fehlende Benutzerechte";
}
} else if ($command == 'changeGeneralComment') {
$this->changeGeneralComment($_POST['id'],$_POST['comment']);
} else if ($command == 'delGeneralComment') {
$this->delGeneralComment($_POST['id']);
} else if ($command == 'upGeneralComment') {
$this->upGeneralComment($_POST['id']);
} else if ($command == 'downGeneralComment') {
$this->downGeneralComment($_POST['id']);
} else if ($command == 'getAssignedExtrasOfProd') {
if ($this->isCmdAllowed(R_WAI | R_PRO)) {
$this->getAssignedExtrasOfProd($_GET['prodid']);
}
2020-11-19 23:03:10 +01:00
} else if ($command == 'sortProdAlpha') {
$this->sortProdAlpha($_POST['refid']);
2020-11-19 23:03:35 +01:00
} else if ($command == 'getOnlyAllProds') {
$this->getOnlyAllProds();
} else if ($command == 'deleteImageProdAssignment') {
$this->deleteImageProdAssignment($_POST['prodid']);
} else if ($command == 'loadprodimage') {
$this->loadprodimage();
2020-11-19 23:11:27 +01:00
} else if ($command == 'loadfullprodimageset') {
$this->loadfullprodimageset();
2020-11-19 23:03:35 +01:00
} else if ($command == 'getprodimage') {
if ($_GET['prodid']) {
$size = 'h';
if (isset($_GET['size'])) {
$size = $_GET['size'];
}
$this->getprodimage($_GET['prodid'],$size);
} else {
$this->getprodimage(null);
}
2020-11-19 23:10:48 +01:00
} else if ($command == 'prodimghmlexport') {
self::exportImgHml();
2020-11-19 23:11:27 +01:00
} else if ($command == 'cleanprodimagestable') {
$pdo = DbUtils::openDbAndReturnPdoStatic();
self::cleanProdImagesTable($pdo);
echo json_encode(array("status" => "OK"));
} else if ($command == 'assignProdImageToKey') {
self::assignProdImageToKey($_POST['prodid'],$_POST['prodimageid']);
} else if ($command == 'getkeynames') {
self::getkeynames();
2020-11-19 23:12:24 +01:00
} else if ($command == 'getAllActiveProducts') {
$pdo = DbUtils::openDbAndReturnPdoStatic();
$allprods = self::getAllActiveProducts($pdo);
echo json_encode($allprods);
} else if ($command == 'changesetofproducts') {
$pdo = DbUtils::openDbAndReturnPdoStatic();
$ret = self::changesetofproducts($pdo,$_POST['changeset'],$_POST['newprods']);
echo json_encode($ret);
2020-11-19 22:47:44 +01:00
} else {
echo "Command not supported.";
}
}
2020-11-19 23:02:12 +01:00
private static $proddefs = array(
array("id" => "id","get" => "%products%.id as id","histid" => "prodid","histget" => "prodid","histexportname" => "Produktid","isnumber" => "0"),
2020-11-19 23:12:37 +01:00
array("id" => "shortname", "get" => "shortname","histid" => "shortname","histget" => "shortname","histexportname" => "Name in Bestellansicht","isnumber" => "0"),
array("id" => "longname", "get" => "longname","histid" => "longname","histget" => "longname","histexportname" => "Produktname","isnumber" => "0"),
2020-11-19 23:02:49 +01:00
array("id" => "available", "get" => "available","histid" => "available","histget" => "available","histexportname" => "","histexportname" => "Verfügbarkeit","isnumber" => "0", "exportvals" => array("default" => "Nein","1" => "Ja")),
2020-11-19 23:02:12 +01:00
array("id" => "priceA", "get" => "priceA","histid" => "priceA","histget" => "priceA","histexportname" => "Preis (Stufe A)","isnumber" => "1"),
array("id" => "priceB", "get" => "priceB","histid" => "priceB","histget" => "priceB","histexportname" => "Preis (Stufe B)","isnumber" => "1"),
array("id" => "priceC", "get" => "priceC","histid" => "priceC","histget" => "priceC","histexportname" => "Preis (Stufe C)","isnumber" => "1"),
2020-11-19 23:12:18 +01:00
array("id" => "barcode", "get" => "barcode","histid" => "barcode","histget" => "barcode","histexportname" => "Barcode","isnumber" => "0"),
2020-11-19 23:12:35 +01:00
array("id" => "unit", "get" => "unit","histid" => "unit","histget" => "unit","histexportname" => "Einheit","isnumber" => "0", "exportvals" => array(
"default" => "Stück",
"0" => "Stück",
"1" => "Preiseingabe bei Bestellung",
"2" => "Gewicht (kg)",
"3" => "Gewicht (gr)",
"4" => "Gewicht (mg)",
"5" => "Volimen (l)",
"6" => "Volumen (ml)",
2020-11-19 23:14:48 +01:00
"7" => "Länge (m)",
"8" => "EinzweckgutscheinKauf",
"9" => "EinzweckgutscheinEinl"
2020-11-19 23:12:35 +01:00
)
),
2020-11-19 23:02:49 +01:00
array("id" => "days", "get" => "days","histid" => "days","histget" => "days","histexportname" => "Tage","isnumber" => "0"),
2020-11-19 23:14:48 +01:00
array("id" => "tax", "get" => "IF(tax is not null, tax, '1') as tax","histid" => "tax","histget" => "tax","histexportname" => "Allg. Steuer","isnumber" => "1"),
array("id" => "togotax", "get" => "IF(togotax is not null, togotax, '2') as togotax","histid" => "togotax","histget" => "togotax","histexportname" => "To-Go Steuer","isnumber" => "1"),
2020-11-19 23:02:12 +01:00
array("id" => "taxaustria", "get" => "IF(taxaustria is not null, taxaustria, 'null') as taxaustria","histid" => "","histget" => "","histexportname" => "","isnumber" => "0"),
array("id" => "amount", "get" => "IF(amount is not null, amount, 'null') as amount","histid" => "","histget" => "","histexportname" => "","isnumber" => "0"),
array("id" => "audio", "get" => "IF(audio is not null, audio, '') as audio","histid" => "","histget" => "","histexportname" => "","isnumber" => "0"),
2020-11-19 23:02:49 +01:00
array("id" => "favorite", "get" => "IF(favorite is not null, favorite, '') as favorite","histid" => "prodid","histget" => "favorite","histexportname" => "Favorit","isnumber" => "0", "exportvals" => array("default" => "Nein","1" => "Ja")),
2020-11-19 23:02:12 +01:00
array("id" => "type", "get" => "'p' as type","histid" => "","histget" => "","histexportname" => "","isnumber" => "0"),
2020-11-19 23:10:06 +01:00
array("id" => "prodimageid", "get" => "IF(prodimageid is not null, prodimageid, 0) as prodimageid","histid" => "prodimageid","histget" => "prodimageid","histexportname" => "Bildnr","isnumber" => "1"),
array("id" => "display", "get" => "IF(display is not null, display, 'KG') as display","histid" => "display","histget" => "display","histexportname" => "Anzeige","isnumber" => "0")
2020-11-19 23:02:12 +01:00
);
2020-11-19 23:10:48 +01:00
public static function exportImgHml() {
2020-11-19 23:11:27 +01:00
$file_name = "bilddaten.ocs";
header("Content-type: text/x-csv");
header("Content-Disposition: attachment; filename=$file_name");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
header("Expires: 0");
2020-11-19 23:10:48 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
2020-11-19 23:11:27 +01:00
$sql = "SELECT id,keyname,imgh,imgm,imgl from %prodimages% order by id";
2020-11-19 23:10:48 +01:00
$result = CommonUtils::fetchSqlAll($pdo, $sql, null);
2020-11-19 23:11:27 +01:00
for ($i=0;$i<count($result);$i++) {
echo ($i+1) . ";" . $result[$i]['keyname'] . ';' . $result[$i]['imgh'] . ";" . $result[$i]['imgm'] . ";" . $result[$i]['imgl'] . "\n";
}
2020-11-19 23:10:48 +01:00
}
2020-11-19 22:47:44 +01:00
function getDateValueAsBoolInterpretatedIcon($aValue) {
if ($aValue != '0' ) {
$imgFile = "ok.png";
} else {
$imgFile = "notavailable.png";
}
return $imgFile;
}
private function getAllTypesAndAvailProds() {
2020-11-19 23:02:49 +01:00
date_default_timezone_set(DbUtils::getTimeZone());
$date = new DateTime();
$dayofweek = date('N');
if ($dayofweek == 7) {
$dayofweek = 0;
}
2020-11-19 22:47:44 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
$pdo->beginTransaction();
2020-11-19 23:10:21 +01:00
$pricelevel = CommonUtils::getConfigValue($pdo, 'pricelevel', 1);
2020-11-19 22:47:44 +01:00
$priceTxt = "priceA";
if ($pricelevel == 2) {
$priceTxt = "priceB";
} else if ($pricelevel == 3) {
$priceTxt = "priceC";
}
2020-11-19 23:10:23 +01:00
$sql = "select id,name,reference,sorting from %prodtype% where removed is null";
2020-11-19 23:10:21 +01:00
$result = CommonUtils::fetchSqlAll($pdo, $sql, null);
2020-11-19 22:47:44 +01:00
$typeArray = array();
foreach($result as $row) {
$ref = $row['reference'];
if ($ref == null) {
$ref = 0;
}
2020-11-19 23:10:23 +01:00
$typeArray[] = array("id" => $row['id'], "name" => $row['name'], "ref" => $ref,"sorting" => $row["sorting"]);
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:14:48 +01:00
$sql = "select id,shortname,longname,audio,category as ref,favorite,$priceTxt as price,COALESCE(barcode, '') as barcode,IF(unit is not null, unit, '0') as unit,IF(days is not null, days, '0123456') as days,IF(tax is not null, tax, 'null') as tax,IF(taxaustria is not null, taxaustria, 'null') as taxaustria,IF(amount is not null, amount, 'null') as amount,COALESCE(prodimageid,0) as prodimageid ";
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 = 'K' OR display is null) ORDER BY sorting";
2020-11-19 23:02:49 +01:00
$result = CommonUtils::fetchSqlAll($pdo, $sql, array("%$dayofweek%"));
2020-11-19 22:47:44 +01:00
$prodArray = array();
foreach($result as $row) {
$ref = $row['ref'];
if ($ref == null) {
$ref = 0;
}
$audio = $row['audio'];
if ($audio == null) {
$audio = "";
}
$fav = $row['favorite'];
if ($fav == null) {
$fav = 0;
}
2020-11-19 23:14:48 +01:00
$sql = "SELECT DISTINCT %extras%.id AS extraid,%extras%.name AS name,%extras%.price as price,COALESCE(%extras%.maxamount,1) as maxamount,%extras%.sorting as sorting FROM %extras%,%extrasprods%
2020-11-19 23:02:57 +01:00
WHERE %extrasprods%.prodid=? AND %extras%.id=%extrasprods%.extraid AND %extras%.removed is null ORDER BY sorting,name";
2020-11-19 22:47:44 +01:00
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($row['id']));
$extras = $stmt->fetchAll(PDO::FETCH_OBJ);
2020-11-19 23:12:18 +01:00
$prodArray[] = array("id" => $row['id'],
"name" => $row['shortname'], "longname" => $row['longname'],
"audio" => $audio,
"ref" => $ref,
"favorite" => $fav,
"price" => $row['price'],
"barcode" => $row['barcode'],
"unit" => $row['unit'],
"days" => $row['days'],
"tax" => $row['tax'],
"taxaustria" => $row['taxaustria'],
"amount" => $row['amount'],
"prodimageid" => $row['prodimageid'],
"extras" => $extras);
2020-11-19 22:47:44 +01:00
}
$pdo->commit();
2020-11-19 23:10:21 +01:00
$filteredTypes = self::filterUsedTypes($typeArray, $prodArray);
2020-11-19 23:10:23 +01:00
usort($filteredTypes,"Products::cmptypes");
2020-11-19 23:10:21 +01:00
$retArray = array("types" => $filteredTypes, "prods" => $prodArray);
2020-11-19 22:47:44 +01:00
echo json_encode($retArray);
}
2020-11-19 23:10:23 +01:00
public static function cmptypes($a, $b)
{
return $a['sorting'] - $b['sorting'];
}
2020-11-19 23:10:21 +01:00
private static function filterUsedTypes($types,$products) {
$typesWithContent = array();
foreach ($products as $p) {
$ref = $p["ref"];
$typeOfProd = self::getTypeOfId($types, $ref);
if (!is_null($typeOfProd)) {
$typesWithContent = self::declareProdTypeAndParentsInUse($types, $typeOfProd,$typesWithContent);
}
}
$out = array();
$keys = array_keys($typesWithContent);
foreach($keys as $aKey) {
$t = $typesWithContent[$aKey];
2020-11-19 23:10:23 +01:00
$out[] = array("id" => $t["id"],"name" => $t["name"],"ref" => $t["ref"],"sorting" => $t["sorting"]);
2020-11-19 23:10:21 +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,$typesWithContent) {
$typeid = $aType["id"];
$reference = $aType["ref"];
2020-11-19 23:10:23 +01:00
$sorting = $aType["sorting"];
2020-11-19 23:10:21 +01:00
if (!array_key_exists($typeid, $typesWithContent)) {
2020-11-19 23:10:23 +01:00
$typesWithContent[$typeid] = array("id" => $typeid,"name" => $aType["name"],"ref" => $reference,"sorting" => $sorting);
2020-11-19 23:10:21 +01:00
$parent = null;
foreach($alltypes as $a) {
$typeid = $a["id"];
if ($typeid == $reference) {
$parent = $a;
break;
}
}
if (!is_null($parent)) {
$typesWithContent = self::declareProdTypeAndParentsInUse($alltypes,$parent,$typesWithContent);
}
}
return $typesWithContent;
}
2020-11-19 22:47:44 +01:00
/*
* Return all available product with id and name, category
* (used for re-assignment to type)
*/
function getAllAvailProdsAlphaSorted() {
$pdo = $this->dbutils->openDbAndReturnPdo();
$sql = "select id,longname,category from %products% WHERE available='1' AND removed is null ORDER BY longname";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_OBJ);
echo json_encode($result);
}
function getAllExtrasAlphaSorted() {
2020-11-19 23:12:39 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
2020-11-19 22:47:44 +01:00
$ret = $this->getAllExtrasAlphaSortedCore($pdo);
echo json_encode(array("status" => "OK", "msg" => $ret));
}
function getAllExtrasAlphaSortedCore($pdo) {
$pdo = $this->dbutils->openDbAndReturnPdo();
2020-11-19 23:14:48 +01:00
$sql = "select id,name,price,COALESCE(maxamount,1) as maxamount,sorting from %extras% WHERE removed is null ORDER BY sorting,name";
2020-11-19 22:47:44 +01:00
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_OBJ);
return $result;
}
/*
* Return in array all products with their id and longname that have a reference to
* the given category.
*
* The output is this:
* ["id" => 1, "longname" => "Whatever Product"],
* ["id" => 2, "longname" => "Whatever Other Product"], ...
*/
2020-11-19 22:54:51 +01:00
private function getProductsWithReferenz($pdo,$ref) {
2020-11-19 22:47:44 +01:00
$prods = array();
2020-11-19 23:02:12 +01:00
$sqlselecttxt = self::getSqlSearchForProducts();
$sql = "SELECT $sqlselecttxt from %products% where removed is null AND category is null ORDER BY sorting";
2020-11-19 22:47:44 +01:00
if ($ref > 0) {
2020-11-19 23:02:12 +01:00
$sql = "SELECT $sqlselecttxt from %products% where removed is null AND category=$ref ORDER BY sorting";
2020-11-19 22:47:44 +01:00
}
2020-11-19 22:54:51 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $zeile) {
2020-11-19 23:02:12 +01:00
$prod_entry = array("type" => "p");
foreach(self::$proddefs as $aProdDef) {
$prod_entry[$aProdDef["id"]] = $zeile[$aProdDef["id"]];
}
2020-11-19 22:47:44 +01:00
$prods[] = $prod_entry;
}
return $prods;
}
private static function getAllSubTypes($pdo,$prodtypeid) {
$sql = "SELECT id,reference FROM %prodtype% WHERE reference=? AND removed is null";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($prodtypeid));
$allSubTypes = $stmt->fetchAll(PDO::FETCH_OBJ);
$subtypeids = array();
foreach ($allSubTypes as $aType) {
$typeids = self::getAllSubTypes($pdo,$aType->id);
$subtypeids = array_merge($subtypeids,$typeids);
}
return array_merge(array($prodtypeid),$subtypeids);
}
private static function getAllProdIdOfSameTypeAndBelow($pdo,$prodid) {
$sql = "SELECT category FROM %products% WHERE id=?";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($prodid));
$row =$stmt->fetchObject();
$theType = $row->category;
$allTypes = self::getAllSubTypes($pdo,$theType);
$prodIds = array();
foreach ($allTypes as $aTypeId) {
$sql = "SELECT id FROM %products% WHERE category=? AND removed is null";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($aTypeId));
$allProdIdsOfThisType = $stmt->fetchAll(PDO::FETCH_OBJ);
foreach($allProdIdsOfThisType as $aProd) {
$prodIds[] = $aProd->id;
}
}
return $prodIds;
}
/*
* Return in array all types with their id and name that have a reference to
* the given category.
*
* The output is this:
* ["id" => 1, "name" => "Meal"],
* ["id" => 2, "name" => "Drinks"], ...
*/
2020-11-19 22:54:51 +01:00
private function getProdTypesWithReferenz($pdo,$ref) {
2020-11-19 23:12:05 +01:00
$sql = "SELECT id,name,kind,usekitchen,usesupplydesk,printer,'t' as type,fixbind,sorting from %prodtype% where removed is null AND reference is null ORDER BY sorting";
2020-11-19 22:47:44 +01:00
if ($ref > 0) {
2020-11-19 23:12:05 +01:00
$sql = "SELECT id,name,kind,usekitchen,usesupplydesk,printer,'t' as type,fixbind,sorting from %prodtype% where removed is null AND reference=$ref ORDER BY sorting";
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:12:05 +01:00
$types = CommonUtils::fetchSqlAll($pdo, $sql, null);
2020-11-19 22:47:44 +01:00
return $types;
}
function showDbProducts() {
2020-11-19 22:54:51 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
$productArray = $this->getDbProductsWithRef_json_version($pdo,0,0);
2020-11-19 22:47:44 +01:00
echo json_encode($productArray);
}
private function getMenuLevelUp($ref) {
2020-11-19 23:14:48 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
2020-11-19 23:10:23 +01:00
$sql = "SELECT reference from %prodtype% where removed is null AND id=? ORDER BY sorting";
2020-11-19 22:47:44 +01:00
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($ref));
$currentRef =$stmt->fetchObject();
$this->getMenu($currentRef->reference,$pdo);
}
private function getMenu($ref,$pdo) {
if (is_null($pdo)) {
2020-11-19 23:12:05 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
2020-11-19 22:47:44 +01:00
}
if (!is_null($ref) && ($ref>0)) {
2020-11-19 23:12:05 +01:00
$sql = "SELECT id,name,kind,usekitchen,usesupplydesk,printer,'t' as type,fixbind from %prodtype% where removed is null AND id=?";
2020-11-19 22:47:44 +01:00
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($ref));
$currentProdType =$stmt->fetchObject();
} else {
$currentProdType = null;
}
if (!is_null($ref) && ($ref>0)) {
2020-11-19 23:12:05 +01:00
$sql = "SELECT id,name,kind,usekitchen,usesupplydesk,printer,'t' as type,fixbind from %prodtype% where removed is null AND reference=?";
2020-11-19 22:47:44 +01:00
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($ref));
} else {
2020-11-19 23:12:05 +01:00
$sql = "SELECT id,name,kind,usekitchen,usesupplydesk,printer,'t' as type,fixbind from %prodtype% where removed is null AND reference is null";
2020-11-19 22:47:44 +01:00
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute();
}
$containedTypes = $stmt->fetchAll(PDO::FETCH_OBJ);
2020-11-19 23:02:12 +01:00
$sqlselecttxt = self::getSqlSearchForProducts();
2020-11-19 22:47:44 +01:00
if (!is_null($ref) && ($ref>0)) {
2020-11-19 23:02:12 +01:00
$sql = "SELECT $sqlselecttxt from %products% where removed is null AND category=? ORDER BY sorting";
2020-11-19 22:47:44 +01:00
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($ref));
} else {
2020-11-19 23:02:12 +01:00
$sql = "SELECT $sqlselecttxt from %products% where removed is null AND category is null ORDER BY sorting";
2020-11-19 22:47:44 +01:00
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute();
}
$containedProds = $stmt->fetchAll(PDO::FETCH_OBJ);
$msg = array("currentType" => $currentProdType,"containedTypes" => $containedTypes,"containedProds" => $containedProds);
echo json_encode(array("status" => "OK", "msg" => $msg));
}
2020-11-19 22:54:51 +01:00
function readDbProducts($pdo) {
$speisekarte = $this->readDbProductsWithRef_json_version($pdo,0,0);
2020-11-19 22:59:47 +01:00
$speisekarte .= $this->readExtrasFromDb($pdo);
2020-11-19 22:47:44 +01:00
return $speisekarte;
}
function readExtrasFromDb($pdo) {
if (is_null($pdo)) {
2020-11-19 23:12:39 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:12:39 +01:00
$sql = "SELECT id,name,price,maxamount,sorting FROM %extras% WHERE removed is null ORDER by sorting";
2020-11-19 22:47:44 +01:00
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_OBJ);
$extrasTxt = "";
2020-11-19 22:59:47 +01:00
$decpoint = $this->getDecPoint($pdo);
2020-11-19 22:47:44 +01:00
foreach ($result as $aRes) {
2020-11-19 23:12:39 +01:00
$extrasTxt .= "!" . $aRes->name . " (ID:" . $aRes->id . ") # " ;
2020-11-19 22:47:44 +01:00
$priceTxt = number_format($aRes->price, 2, $decpoint, '');
2020-11-19 23:12:39 +01:00
$extrasTxt .= "Preis: " . $priceTxt . "; ";
$maxamount = $aRes->maxamount;
$extrasTxt .= "Max: " . $maxamount . "; ";
2020-11-19 22:47:44 +01:00
$sql = "SELECT prodid FROM %extrasprods% WHERE extraid=?";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($aRes->id));
$assignedProds = $stmt->fetchAll(PDO::FETCH_OBJ);
$assProdArr = array();
foreach ($assignedProds as $anAssProd) {
$assProdArr[] = "(" . $anAssProd->prodid . ")";
}
2020-11-19 23:12:39 +01:00
$extrasTxt .= "Zugewiesen: " . join(",",$assProdArr) . "\n";
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:12:39 +01:00
2020-11-19 22:47:44 +01:00
return $extrasTxt;
}
2020-11-19 22:59:47 +01:00
private function getDecPoint($pdo) {
2020-11-19 22:47:44 +01:00
$sql = "SELECT name,setting FROM %config% WHERE name=?";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array("decpoint"));
$row = $stmt->fetchObject();
return($row->setting);
}
2020-11-19 23:02:12 +01:00
public static function getSqlSearchForProducts() {
$sqlselect = array();
foreach(self::$proddefs as $aProdDef) {
$sqlselect[] = $aProdDef["get"];
}
return (join(",",$sqlselect));
}
public static function getSqlSearchForHistProducts() {
$sqlselect = array();
foreach(self::$proddefs as $aProdDef) {
$theHistId = $aProdDef["histget"];
if ($theHistId != '') {
$sqlselect[] = $aProdDef["histget"];
}
}
return (join(",",$sqlselect));
}
public static function getHistProdExportNames() {
$sqlselect = array();
foreach(self::$proddefs as $aProdDef) {
$theHistId = $aProdDef["histexportname"];
if ($theHistId != '') {
$sqlselect[] = $aProdDef["histexportname"];
}
}
return (join(";",$sqlselect));
}
2020-11-19 22:47:44 +01:00
private function exportCsv() {
2020-11-19 22:54:51 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
2020-11-19 22:59:47 +01:00
$decpoint = $this->getDecPoint($pdo);
2020-11-19 22:47:44 +01:00
$file_name = "datenexport-produkte.csv";
header("Content-type: text/x-csv");
header("Content-Disposition: attachment; filename=$file_name");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
header("Expires: 0");
2020-11-19 23:02:12 +01:00
echo("Eintragsid; Datum ;");
echo self::getHistProdExportNames();
echo("; Extras/Optionen; Beschreibung");
2020-11-19 22:47:44 +01:00
echo("\n");
$sql = "SELECT DISTINCT %hist%.id as id,date,";
2020-11-19 23:02:12 +01:00
$sql .= self::getSqlSearchForHistProducts();
$sql .= ",extras,description ";
2020-11-19 22:47:44 +01:00
$sql .= " FROM %hist%, %histprod%, %histactions% ";
$sql .= " WHERE (refid=%histprod%.id) ";
$sql .= " AND (action='1' OR action='4' OR action='5') ";
$sql .= " AND (action=%histactions%.id) ";
$sql .= " ORDER BY date,id";
2020-11-19 22:54:51 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $zeile) {
2020-11-19 23:02:12 +01:00
echo $zeile['id'] . ";" . $zeile['date'] . ";";
foreach(self::$proddefs as $aProdDef) {
$item = $aProdDef["histexportname"];
$itemsql = $aProdDef["histget"];
if ($item != "") {
if ($itemsql == 'tax') {
$value = ($zeile['tax']);
if ($value == null) {
$value = '-';
}
$value = str_replace(".",$decpoint,$value);
2020-11-19 23:02:49 +01:00
} else if (isset($aProdDef["exportvals"])) {
$exportvals = $aProdDef["exportvals"];
$value = $zeile[$itemsql];
if (isset($exportvals[$value])) {
$value = $exportvals[$value];
} else {
$value = $exportvals["default"];
}
2020-11-19 23:02:12 +01:00
} else {
$isNumber = $aProdDef["isnumber"];
$value = $zeile[$aProdDef["histget"]];
if ($isNumber == '1') {
$value = str_replace(".",$decpoint,$value);
} else {
$value = str_replace('"','""',$value);
}
}
echo $value . ";";
}
}
echo $zeile['extras'] . ";" . $zeile['description'] . "\n";
2020-11-19 22:47:44 +01:00
}
}
2020-11-19 22:54:51 +01:00
function getSingleProdData($pdo,$id) {
2020-11-19 22:47:44 +01:00
if (is_numeric($id)) {
2020-11-19 23:14:48 +01:00
$sql = "SELECT id,shortname,longname,priceA,priceB,priceC,COALESCE(barcode,'') as barcode,IF(unit is not null, unit, '0') as unit,IF(days is not null, days, '0123456') as days,IF(tax is not null, tax, 'null') as tax,available,audio,favorite,COALESCE(amount,'null') as amount,IF(display is not null, display, 'KG') as display from %products% where id=?";
2020-11-19 22:47:44 +01:00
2020-11-19 22:54:51 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($id));
$row = $stmt->fetchObject();
echo json_encode($row);
2020-11-19 22:47:44 +01:00
}
}
function getSingleTypeData($id) {
if (is_numeric($id)) {
$pdo = $this->dbutils->openDbAndReturnPdo();
2020-11-19 23:12:05 +01:00
$sql = "SELECT id,name,usekitchen,usesupplydesk,kind,reference,printer,fixbind FROM %prodtype% WHERE removed is NULL AND id=?";
2020-11-19 22:47:44 +01:00
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($id));
$affectedRows = $stmt->rowCount();
if ($affectedRows == 1) {
$row = $stmt->fetchObject();
echo json_encode(array("status" => "OK", "msg" => $row));
}
} else {
echo json_encode(array("status" => "ERROR", "code" => ERROR_GENERAL_DB_NOT_READABLE, "msg" => ERROR_GENERAL_DB_NOT_READABLE_MSG));
}
}
function reassign($prodid,$typeid) {
$pdo = $this->dbutils->openDbAndReturnPdo();
$pdo->beginTransaction();
$this->sorter->resortAfterProduct($pdo, $prodid);
$sql = "UPDATE %products% SET category=? WHERE id=?";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($typeid,$prodid));
$affectedRows = $stmt->rowCount();
$pdo->commit();
if ($affectedRows == 1) {
$this->sorter->setMaxSortingForProdId($pdo, $prodid);
echo json_encode(array("status" => "OK"));
} else {
echo json_encode(array("status" => "ERROR"));
}
}
2020-11-19 23:02:57 +01:00
private static function getMaxSortingOfExtras($pdo) {
$sql = "SELECT max(sorting) as maxsort FROM %extras% WHERE removed is null";
$row = CommonUtils::getRowSqlObject($pdo, $sql,null);
$max = 0;
if (!is_null($row) && !is_null($row->maxsort)) {
$max = intval($row->maxsort);
}
return $max;
}
2020-11-19 22:47:44 +01:00
private function getMaxSortOfGenComment($pdo) {
$sql = "SELECT MAX(sorting) as maxsort from %comments% WHERE prodid is null";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute();
$row = $stmt->fetchObject();
$maxSorting = 0;
if ($row != null) {
$maxSorting = intval($row->maxsort);
}
return $maxSorting;
}
private function getAssignedExtrasOfProd($prodid) {
$ret = $this->getAssignedExtrasOfProdCore($prodid,null);
echo json_encode(array("status" => "OK", "msg" => $ret, "prodid" => $prodid));
}
private function getAssignedExtrasOfProdCore($prodid,$pdo) {
if (is_null($pdo)) {
$pdo = $this->dbutils->openDbAndReturnPdo();
}
$sql = "SELECT DISTINCT %extras%.id AS extraid FROM %extras%,%extrasprods%
WHERE %extrasprods%.prodid=? AND %extras%.id=%extrasprods%.extraid AND %extras%.removed is null";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($prodid));
$result = $stmt->fetchAll(PDO::FETCH_OBJ);
$ids = array();
foreach ($result as $aRes) {
$ids[] = $aRes->extraid;
}
return $ids;
}
2020-11-19 23:12:39 +01:00
public static function createExtraCore($pdo,$name,$price,$maxamount,$assignedProdIds) {
2020-11-19 22:47:44 +01:00
if (is_null($pdo)) {
$pdo = DbUtils::openDbAndReturnPdoStatic();
}
2020-11-19 23:02:57 +01:00
try {
$sql = "SELECT id FROM %extras% WHERE name=? AND removed is null";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($name));
$numberOfExtras = $stmt->rowCount();
2020-11-19 22:47:44 +01:00
2020-11-19 23:02:57 +01:00
if ($numberOfExtras > 0) {
return ERROR_NAME_EXISTS_ALREADY;
}
2020-11-19 22:47:44 +01:00
2020-11-19 23:02:57 +01:00
$maxPos = self::getMaxSortingOfExtras($pdo);
2020-11-19 23:12:39 +01:00
$sql = "INSERT INTO `%extras%` (`name`,`price`,`maxamount`,`sorting`) VALUES(?,?,?,?)";
2020-11-19 23:02:57 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
2020-11-19 23:12:39 +01:00
$stmt->execute(array($name,$price,$maxamount,$maxPos+1));
2020-11-19 23:02:57 +01:00
$lastExtraId = $pdo->lastInsertId();
2020-11-19 23:12:39 +01:00
$sql = "DELETE FROM %extrasprods% WHERE extraid=?";
2020-11-19 22:47:44 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
2020-11-19 23:02:57 +01:00
$stmt->execute(array($lastExtraId));
foreach ($assignedProdIds as $assProdId) {
$sql = "INSERT INTO %extrasprods% (`id` , `extraid` , `prodid`) VALUES (NULL,?,?)";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($lastExtraId,$assProdId));
2020-11-19 22:47:44 +01:00
2020-11-19 23:02:57 +01:00
}
return OK;
} catch (Exception $ex) {
echo $ex->getMessage();
exit;
2020-11-19 22:47:44 +01:00
}
}
2020-11-19 23:12:39 +01:00
private function createExtra($name,$price,$maxamount) {
2020-11-19 23:14:48 +01:00
if (intval($maxamount) < 1) {
echo json_encode(array("status" => "ERROR", "msg" => "Anzahl zu klein.", "id" => $id));
return;
}
2020-11-19 22:47:44 +01:00
try {
$pdo = $this->dbutils->openDbAndReturnPdo();
$pdo->beginTransaction();
2020-11-19 23:12:39 +01:00
$ret = self::createExtraCore($pdo,$name,$price,$maxamount,array());
2020-11-19 22:47:44 +01:00
if ($ret == ERROR_NAME_EXISTS_ALREADY) {
echo json_encode(array("status" => "ERROR", "code" => ERROR_NAME_EXISTS_ALREADY, "msg" => ERROR_NAME_EXISTS_ALREADY_MSG));
$pdo->rollBack();
return;
}
$pdo->commit();
$this->getAllExtrasAlphaSorted();
}
catch (PDOException $e) {
echo json_encode(array("status" => "ERROR", "code" => ERROR_GENERAL_DB_NOT_READABLE, "msg" => ERROR_GENERAL_DB_NOT_READABLE_MSG));
}
}
2020-11-19 23:02:57 +01:00
private function upExtra($id) {
$pdo = DbUtils::openDbAndReturnPdoStatic();
$pdo->beginTransaction();
$sql = "SELECT sorting FROM %extras% WHERE id=?";
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($id));
$currentPos = intval($row->sorting);
if ($currentPos > 1) {
$sql = "SELECT id FROM %extras% WHERE sorting=? AND removed is NULL";
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($currentPos-1));
if (!is_null($row) && !is_null($row->id)) {
$idUpper = $row->id;
$sql = "UPDATE %extras% SET sorting=? WHERE id=?";
CommonUtils::execSql($pdo, $sql, array($currentPos,$idUpper));
CommonUtils::execSql($pdo, $sql, array($currentPos-1,$id));
}
}
$pdo->commit();
$this->getAllExtrasAlphaSorted();
}
2020-11-19 23:12:39 +01:00
private function applyExtra($name,$price,$maxamount,$id) {
2020-11-19 23:14:48 +01:00
if (intval($maxamount) < 1) {
echo json_encode(array("status" => "ERROR", "msg" => "Anzahl zu klein.", "id" => $id));
return;
}
2020-11-19 22:47:44 +01:00
try {
2020-11-19 23:14:48 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
2020-11-19 22:47:44 +01:00
$pdo->beginTransaction();
$sql = "SELECT id FROM %extras% WHERE name=? AND id <> ? AND removed is null";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($name,$id));
$numberOfExtras = $stmt->rowCount();
if ($numberOfExtras > 0) {
echo json_encode(array("status" => "ERROR", "code" => ERROR_NAME_EXISTS_ALREADY, "msg" => ERROR_NAME_EXISTS_ALREADY_MSG, "id" => $id));
$pdo->rollBack();
return;
}
2020-11-19 23:10:46 +01:00
2020-11-19 23:12:39 +01:00
$sql = "SELECT name,price,maxamount,sorting FROM %extras% WHERE id=?";
2020-11-19 23:10:46 +01:00
$result = CommonUtils::fetchSqlAll($pdo, $sql, array($id));
$oldname = $result[0]['name'];
$oldprice = $result[0]['price'];
2020-11-19 23:12:39 +01:00
$oldmaxamount = $result[0]['maxamount'];
2020-11-19 23:10:46 +01:00
2020-11-19 23:12:39 +01:00
if (($oldname == $name) && ($oldprice == $price) && ($oldmaxamount == $maxamount)) {
2020-11-19 22:47:44 +01:00
echo json_encode(array("status" => "ERROR", "code" => DB_NOT_CHANGED, "msg" => DB_NOT_CHANGED_MSG, "id" => $id));
$pdo->rollBack();
return;
2020-11-19 23:10:46 +01:00
} else {
$oldsorting = $result[0]['sorting'];
2020-11-19 23:12:39 +01:00
$sql = "INSERT INTO %extras% (name,price,maxamount,sorting,removed) VALUES(?,?,?,?,?)";
CommonUtils::execSql($pdo, $sql, array($name,$price,$maxamount,$oldsorting,null));
2020-11-19 23:10:46 +01:00
$newExtraId = $pdo->lastInsertId();
$sql = "UPDATE %extras% SET removed = ? WHERE id=?";
CommonUtils::execSql($pdo, $sql, array(1,$id));
$sql = "UPDATE %extrasprods% SET extraid=? WHERE extraid=?";
CommonUtils::execSql($pdo, $sql, array($newExtraId,$id));
2020-11-19 22:47:44 +01:00
}
$pdo->commit();
$this->getAllExtrasAlphaSorted();
}
catch (PDOException $e) {
echo json_encode(array("status" => "ERROR", "code" => ERROR_GENERAL_DB_NOT_READABLE, "msg" => ERROR_GENERAL_DB_NOT_READABLE_MSG, "id" => $id));
}
}
private function delExtra($id) {
try {
2020-11-19 23:02:57 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
$sql = "SELECT sorting FROM %extras% WHERE id=?";
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($id));
$currentPos = $row->sorting;
$maxPos = self::getMaxSortingOfExtras($pdo);
$sql = "UPDATE %extras% SET removed='1',sorting=? WHERE id=?";
2020-11-19 22:47:44 +01:00
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
2020-11-19 23:02:57 +01:00
$stmt->execute(array(null,$id));
2020-11-19 22:47:44 +01:00
$this->getAllExtrasAlphaSorted();
2020-11-19 23:02:57 +01:00
// decrease all sortings, independently of the removed flag, by 1
for ($i=$currentPos;$i<=$maxPos;$i++) {
$sql = "UPDATE %extras% SET sorting=? WHERE sorting=?";
CommonUtils::execSql($pdo, $sql, array($i-1,$i));
}
2020-11-19 22:47:44 +01:00
}
catch (PDOException $e) {
echo json_encode(array("status" => "ERROR", "code" => ERROR_GENERAL_DB_NOT_READABLE, "msg" => ERROR_GENERAL_DB_NOT_READABLE_MSG));
}
}
/**
* Add a new comment to the list of general comments that are not bound to a product
* @param string $comment
*/
private function addGeneralComment($comment) {
try {
$pdo = $this->dbutils->openDbAndReturnPdo();
$pdo->beginTransaction();
$maxSorting = $this->getMaxSortOfGenComment($pdo);
$sql = "INSERT INTO `%comments%` (`id`,`comment`,`prodid`,`active`,`sorting`) VALUES(NULL,?,NULL,1,?)";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($comment,$maxSorting+1));
$pdo->commit();
echo json_encode(array("status" => "OK"));
}
catch (PDOException $e) {
echo json_encode(array("status" => "ERROR", "code" => ERROR_GENERAL_DB_NOT_READABLE, "msg" => ERROR_GENERAL_DB_NOT_READABLE_MSG));
}
}
private function getAllGeneralComments() {
try {
$pdo = $this->dbutils->openDbAndReturnPdo();
$sql = "SELECT id,comment,sorting FROM %comments% WHERE prodid is null ORDER BY sorting ASC";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute();
$result = $stmt->fetchAll();
$commentArray = array();
foreach($result as $row) {
$commentArray[] = array("id" => $row['id'], "comment" => $row['comment'], "sorting" => $row['sorting']);
}
echo json_encode(array("status" => "OK", "msg" => $commentArray));
}
catch (PDOException $e) {
echo json_encode(array("status" => "ERROR", "code" => ERROR_GENERAL_DB_NOT_READABLE, "msg" => ERROR_GENERAL_DB_NOT_READABLE_MSG));
}
}
private function changeGeneralComment($id,$comment) {
try {
$pdo = $this->dbutils->openDbAndReturnPdo();
$sql = "UPDATE %comments% SET comment=? WHERE id=?";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($comment,$id));
echo json_encode(array("status" => "OK"));
}
catch (PDOException $e) {
echo json_encode(array("status" => "ERROR", "code" => ERROR_GENERAL_DB_NOT_READABLE, "msg" => ERROR_GENERAL_DB_NOT_READABLE_MSG));
}
}
private function getSortingOfComment($pdo,$id) {
$sql = "SELECT sorting FROM %comments% WHERE id=?";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($id));
$row = $stmt->fetchObject();
if ($row == null) {
return (-1);
} else {
return intval($row->sorting);
}
}
private function delGeneralComment($id) {
try {
$pdo = $this->dbutils->openDbAndReturnPdo();
$pdo->beginTransaction();
$sorting = $this->getSortingOfComment($pdo, $id);
if ($sorting < 0) {
echo json_encode(array("status" => "ERROR", "code" => ERROR_GENERAL_DB_NOT_READABLE, "msg" => ERROR_GENERAL_DB_NOT_READABLE_MSG));
return;
}
$sql = "DELETE FROM %comments% WHERE id=? AND prodid is null";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($id));
$sql = "SELECT id,sorting FROM %comments% WHERE sorting>? AND prodid is null";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($sorting));
$result = $stmt->fetchAll();
$commentsToUpdate = array();
foreach($result as $row) {
$theId = $row['id'];
$theSort = intval($row['sorting'])-1;
$sql = "UPDATE %comments% SET sorting=? WHERE id=?";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($theSort,$theId));
}
$pdo->commit();
echo json_encode(array("status" => "OK"));
}
catch (PDOException $e) {
echo json_encode(array("status" => "ERROR", "code" => ERROR_GENERAL_DB_NOT_READABLE, "msg" => ERROR_GENERAL_DB_NOT_READABLE_MSG));
}
}
private function upGeneralComment($id) {
try {
$pdo = $this->dbutils->openDbAndReturnPdo();
$pdo->beginTransaction();
$sorting = $this->getSortingOfComment($pdo, $id);
if ($sorting < 0) {
echo json_encode(array("status" => "ERROR", "code" => ERROR_GENERAL_DB_NOT_READABLE, "msg" => ERROR_GENERAL_DB_NOT_READABLE_MSG));
return;
}
if ($sorting == 1) {
$pdo->commit();
echo json_encode(array("status" => "OK"));
return;
}
$sql = "SELECT id FROM %comments% WHERE sorting=? AND prodid is null";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($sorting - 1));
$row = $stmt->fetchObject();
$previousId = $row->id;
$sql = "UPDATE %comments% SET sorting=? WHERE id=?";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($sorting,$previousId));
$sql = "UPDATE %comments% SET sorting=? WHERE id=?";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($sorting-1,$id));
$pdo->commit();
echo json_encode(array("status" => "OK"));
}
catch (PDOException $e) {
echo json_encode(array("status" => "ERROR", "code" => ERROR_GENERAL_DB_NOT_READABLE, "msg" => ERROR_GENERAL_DB_NOT_READABLE_MSG));
}
}
private function downGeneralComment($id) {
try {
$pdo = $this->dbutils->openDbAndReturnPdo();
$pdo->beginTransaction();
$sorting = $this->getSortingOfComment($pdo, $id);
if ($sorting < 0) {
echo json_encode(array("status" => "ERROR", "code" => ERROR_GENERAL_DB_NOT_READABLE, "msg" => ERROR_GENERAL_DB_NOT_READABLE_MSG));
return;
}
$maxSorting = $this->getMaxSortOfGenComment($pdo);
if (($maxSorting == 0) || ($maxSorting == $sorting)) {
$pdo->commit();
echo json_encode(array("status" => "OK"));
return;
}
$sql = "SELECT id FROM %comments% WHERE sorting=? AND prodid is null";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($sorting + 1));
$row = $stmt->fetchObject();
$nextId = $row->id;
$sql = "UPDATE %comments% SET sorting=? WHERE id=?";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($sorting,$nextId));
$sql = "UPDATE %comments% SET sorting=? WHERE id=?";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($sorting+1,$id));
$pdo->commit();
echo json_encode(array("status" => "OK"));
}
catch (PDOException $e) {
echo json_encode(array("status" => "ERROR", "code" => ERROR_GENERAL_DB_NOT_READABLE, "msg" => ERROR_GENERAL_DB_NOT_READABLE_MSG));
}
}
function sortup($prodid) {
$pdo = $this->dbutils->openDbAndReturnPdo();
$pdo->beginTransaction();
$this->sorter->sortup($pdo, $prodid);
$pdo->commit();
echo json_encode("OK");
}
function sortdown($prodid) {
$pdo = $this->dbutils->openDbAndReturnPdo();
$pdo->beginTransaction();
$this->sorter->sortdown($pdo, $prodid);
$pdo->commit();
echo json_encode("OK");
}
function delproduct($prodid) {
$pdo = $this->dbutils->openDbAndReturnPdo();
$pdo->beginTransaction();
$this->sorter->delproduct($pdo, $prodid);
$pdo->commit();
echo json_encode("OK");
}
2020-11-19 23:00:42 +01:00
function applySingleProdData(ProductEntry $prodEntry,$changeExtras,$extras,$assignextrastotype) {
$id = $prodEntry->getProdId();
if (!is_numeric($id)) {
return;
}
$pdo = DbUtils::openDbAndReturnPdoStatic();
$pdo->beginTransaction();
$prodEntry->applyProductInDb($pdo);
2020-11-19 22:47:44 +01:00
if ($assignextrastotype == 0) {
if ($changeExtras == 1) {
$this->changeExtraAssignment($pdo, $id, $extras);
}
2020-11-19 22:59:47 +01:00
HistFiller::updateProdInHist($pdo,$id);
2020-11-19 22:47:44 +01:00
} else {
$prodids = self::getAllProdIdOfSameTypeAndBelow($pdo,$id);
foreach ($prodids as $aProdId) {
$this->changeExtraAssignment($pdo, $aProdId, $extras);
$histextra = self::getExtrasForProd($pdo,$aProdId);
self::updateHistOnlyForExtrasOfProd($pdo, $aProdId,$histextra);
}
}
$pdo->commit();
2020-11-19 22:54:51 +01:00
$this->getSingleProdData($pdo,$id);
2020-11-19 22:47:44 +01:00
}
private static function updateHistOnlyForExtrasOfProd($pdo,$aProdId,$histextra) {
2020-11-19 22:59:47 +01:00
HistFiller::updateProdInHist($pdo, $aProdId);
2020-11-19 22:47:44 +01:00
}
function changeExtraAssignment($pdo,$prodid,$extras) {
$sql = "DELETE FROM %extrasprods% WHERE prodid=?";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($prodid));
$histextra = "";
if (!is_null($extras) && ($extras != "")) {
$sql = "INSERT INTO %extrasprods% (`id` , `extraid` , `prodid`) VALUES (NULL,?,?)";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
foreach($extras as $anExtra) {
$stmt->execute(array($anExtra,$prodid));
}
}
}
private static function getExtrasForProd($pdo,$prodid) {
$sql = "SELECT DISTINCT %extras%.name as extraname from %extras%,%extrasprods% where %extras%.removed is null AND %extrasprods%.extraid=%extras%.id AND %extrasprods%.prodid=?";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($prodid));
$result = $stmt->fetchAll();
$extraArr = array();
if (count($result) == 0) {
return "";
} else {
foreach($result as $row) {
$extraArr[] = $row['extraname'];
}
return(implode(", ", $extraArr));
}
}
2020-11-19 23:03:35 +01:00
function getOnlyAllProds() {
$pdo = DbUtils::openDbAndReturnPdoStatic();
2020-11-19 23:14:48 +01:00
$sql = "SELECT id,longname,COALESCE(prodimageid,0) as prodimageid FROM %products% WHERE removed is null ORDER BY longname";
2020-11-19 23:03:35 +01:00
$result = CommonUtils::fetchSqlAll($pdo, $sql, null);
$ret = array();
foreach($result as $aProd) {
$chainStr = $this->getTypeHierarchy($pdo, $aProd["id"]);
$ret[] = array("id" => $aProd["id"],"longname" => $aProd["longname"],"prodimageid" => $aProd["prodimageid"],"chain" => $chainStr);
}
echo json_encode(array("status" => "OK","msg" => $ret));
}
private function getTypeHierarchy($pdo,$prodid) {
$chain = array();
$sql = "SELECT category FROM %products% WHERE id=?";
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($prodid));
$cat = $row->category;
if (is_null($cat)) {
return "";
}
do {
$chain[] = $this->getNameOfProdType($pdo,$cat);
$cat = $this->getIdOfReferencedTypeProd($pdo,$cat);
} while (!is_null($cat));
return $this->chainArrayToStr($chain);
}
private function getIdOfReferencedTypeProd($pdo,$currentProdTypeId) {
$sql = "SELECT reference FROM %prodtype% WHERE id=?";
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($currentProdTypeId));
return $row->reference;
}
private function getNameOfProdType($pdo,$prodtypeid) {
$sql = "SELECT name FROM %prodtype% WHERE id=?";
$row = CommonUtils::getRowSqlObject($pdo, $sql, array($prodtypeid));
return $row->name;
}
private function chainArrayToStr($chainArr) {
$ret = array();
for ($i=(count($chainArr)-1);$i>=0;$i--) {
$ret[] = $chainArr[$i];
}
$retStr = implode(" -> ", $ret);
return $retStr;
}
2020-11-19 23:11:27 +01:00
private static function getkeynames() {
$pdo = DbUtils::openDbAndReturnPdoStatic();
$sql = "SELECT id,keyname FROM %prodimages% ORDER BY keyname";
$result = CommonUtils::fetchSqlAll($pdo, $sql, null);
echo json_encode(array("status" => "OK","msg" => $result));
}
private static function assignProdImageToKey($prodid,$prodimageid) {
try {
$pdo = DbUtils::openDbAndReturnPdoStatic();
$sql = "UPDATE %products% SET prodimageid=? WHERE id=?";
CommonUtils::execSql($pdo, $sql, array($prodimageid,$prodid));
$ret = array("status" => "OK","prodid" => $prodid);
echo json_encode($ret);
} catch (Exception $ex) {
echo json_encode(array("status" => "ERROR","msg" => $ex->getMessage()));
}
}
2020-11-19 23:03:35 +01:00
public static function cleanProdImagesTable($pdo) {
$sql = "UPDATE %products% SET prodimageid=? WHERE removed is not null";
CommonUtils::execSql($pdo, $sql, array(null));
$sql = "SELECT id FROM %prodimages%";
$result = CommonUtils::fetchSqlAll($pdo, $sql, null);
foreach($result as $res) {
$prodimageid = $res["id"];
$sql = "SELECT id FROM %products% WHERE prodimageid=?";
$referencingProds = CommonUtils::fetchSqlAll($pdo, $sql, array($prodimageid));
if (count($referencingProds) == 0) {
$sql = "DELETE FROM %prodimages% WHERE id=?";
CommonUtils::execSql($pdo, $sql, array($prodimageid));
}
}
}
2020-11-19 23:12:37 +01:00
function getprodimage($prodid,$size='h') {
2020-11-19 23:03:35 +01:00
$imgcol = 'imgh';
if ($size == 'm') {
$imgcol = 'imgm';
} else if ($size == 'l') {
$imgcol = 'imgl';
}
if (is_null($prodid)) {
2020-11-19 23:14:48 +01:00
CommonUtils::outTransImage();
2020-11-19 23:03:35 +01:00
exit;
} else {
$pdo = DbUtils::openDbAndReturnPdoStatic();
$sql = "SELECT $imgcol as img FROM %prodimages%,%products% WHERE %products%.prodimageid=%prodimages%.id AND %products%.id=?";
$result = CommonUtils::fetchSqlAll($pdo, $sql, array($prodid));
if (count($result) != 1) {
2020-11-19 23:14:48 +01:00
CommonUtils::outTransImage();
2020-11-19 23:03:35 +01:00
exit;
} else {
$imagedata = base64_decode($result[0]["img"]);
}
}
header("Content-type: image/png");
echo $imagedata;
exit;
}
private function deleteImageProdAssignment($prodid) {
$pdo = DbUtils::openDbAndReturnPdoStatic();
$pdo->beginTransaction();
try{
$sql = "UPDATE %products% SET prodimageid=? WHERE id=?";
CommonUtils::execSql($pdo, $sql, array(null,$prodid));
HistFiller::updateProdInHist($pdo, $prodid);
$pdo->commit();
} catch (Exception $ex) {
$pdo->rollBack();
}
echo json_encode($prodid);
}
function loadprodimage() {
$pdo = DbUtils::openDbAndReturnPdoStatic();
$prodid = $_GET['prodid'];
if ($_FILES['imagefile']['error'] != UPLOAD_ERR_OK
&& is_uploaded_file($_FILES['imagefile']['tmp_name'])) {
echo json_encode(array("status" => "ERROR","msg" => "Kann Datei nicht laden."));
exit();
}
if(!file_exists($_FILES['imagefile']['tmp_name'])) {
echo json_encode(array("status" => "ERROR","msg" => "Datei existiert nicht. Bitte PHP-Variablen upload_max_filesize und post_max_size_checken."));
exit();
}
if(!is_uploaded_file($_FILES['imagefile']['tmp_name'])) {
echo json_encode(array("status" => "ERROR","msg" => "Datei konnte nicht hochgeladen werden."));
exit();
}
$fn = $_FILES['imagefile']['tmp_name'];
2020-11-19 23:12:37 +01:00
$imageh = CommonUtils::scaleImg($fn, 300);
2020-11-19 23:03:35 +01:00
$imageBaseh_64 = base64_encode($imageh);
2020-11-19 23:12:37 +01:00
$imagem = CommonUtils::scaleImg($fn, 150);
2020-11-19 23:03:35 +01:00
$imageBasem_64 = base64_encode($imagem);
2020-11-19 23:12:37 +01:00
$imagel = CommonUtils::scaleImg($fn, 80);
2020-11-19 23:03:35 +01:00
$imageBasel_64 = base64_encode($imagel);
$pdo->beginTransaction();
try {
2020-11-19 23:11:27 +01:00
$sql = "SELECT SUBSTRING(REPLACE(longname,';','_'),1,30) as longname FROM %products% WHERE id=?";
$prodnameResult = CommonUtils::fetchSqlAll($pdo, $sql, array($prodid));
$keyname = "Bild_" . $prodid;
if (count($prodnameResult) > 0) {
$keyname = $prodnameResult[0]['longname'];
}
$sql = "SELECT id FROM %prodimages% WHERE keyname=?";
$res = CommonUtils::fetchSqlAll($pdo, $sql, array($keyname));
if (count($res) > 0) {
$sql = "UPDATE %prodimages% SET imgh=?,imgm=?,imgl=? WHERE keyname=?";
CommonUtils::execSql($pdo, $sql, array($imageBaseh_64,$imageBasem_64,$imageBasel_64,$keyname));
CommonUtils::execSql($pdo, "UPDATE %products% SET prodimageid=? WHERE id=?", array($res[0]["id"],$prodid));
} else {
$sql = "INSERT INTO %prodimages% (keyname,imgh,imgm,imgl) VALUES(?,?,?,?)";
CommonUtils::execSql($pdo, $sql, array($keyname,$imageBaseh_64,$imageBasem_64,$imageBasel_64));
$prodimageid = $pdo->lastInsertId();
2020-11-19 23:03:35 +01:00
2020-11-19 23:11:27 +01:00
$sql = "UPDATE %products% SET prodimageid=? WHERE id=?";
CommonUtils::execSql($pdo, $sql, array($prodimageid,$prodid));
}
2020-11-19 23:03:35 +01:00
HistFiller::updateProdInHist($pdo,$prodid);
} catch (Exception $ex) {
echo json_encode(array("status" => "ERROR","msg" => $ex->getMessage()));
$pdo->rollBack();
exit();
}
$pdo->commit();
echo json_encode(array("status" => "OK"));
}
2020-11-19 23:11:27 +01:00
function loadfullprodimageset() {
$pdo = DbUtils::openDbAndReturnPdoStatic();
if ($_FILES['textfile']['error'] != UPLOAD_ERR_OK
&& is_uploaded_file($_FILES['textfile']['tmp_name'])) {
echo json_encode(array("status" => "ERROR","msg" => "Kann Datei nicht laden."));
exit();
}
if(!file_exists($_FILES['textfile']['tmp_name'])) {
echo json_encode(array("status" => "ERROR","msg" => "Datei existiert nicht. Bitte PHP-Variablen upload_max_filesize und post_max_size_checken."));
exit();
}
if(!is_uploaded_file($_FILES['textfile']['tmp_name'])) {
echo json_encode(array("status" => "ERROR","msg" => "Datei konnte nicht hochgeladen werden."));
exit();
}
$file = $_FILES['textfile']['tmp_name'];
$handle = fopen ($file, "r");
$pdo->beginTransaction();
try {
while (!feof($handle)) {
$textline = trim(fgets($handle));
if ($textline != "") {
$parts = explode(';', $textline);
$keyname = $parts[1];
$sql = "SELECT id FROM %prodimages% WHERE keyname=?";
$res = CommonUtils::fetchSqlAll($pdo, $sql, array($keyname));
if (count($res) > 0) {
$sql = "UPDATE %prodimages% SET imgh=?,imgm=?,imgl=? WHERE keyname=?";
CommonUtils::execSql($pdo, $sql, array($parts[2],$parts[3],$parts[4],$keyname));
} else {
$sql = "INSERT INTO %prodimages% (keyname,imgh,imgm,imgl) VALUES(?,?,?,?)";
CommonUtils::execSql($pdo, $sql, array($keyname,$parts[2],$parts[3],$parts[4]));
}
}
}
fclose ($handle);
$pdo->commit();
} catch (Exception $ex) {
echo json_encode(array("status" => "ERROR","msg" => $ex->getMessage()));
$pdo->rollBack();
exit();
}
echo json_encode(array("status" => "OK"));
}
2020-11-19 23:03:10 +01:00
function sortProdAlpha($typeid) {
$pdo = DbUtils::openDbAndReturnPdoStatic();
$pdo->beginTransaction();
$sql = "SELECT id,longname FROM %products% WHERE category=? AND removed is null ORDER BY longname";
$result = CommonUtils::fetchSqlAll($pdo, $sql, array($typeid));
$sort = 1;
$sql = "UPDATE %products% SET sorting=? WHERE id=?";
foreach ($result as $prodentry) {
$prodid = $prodentry["id"];
CommonUtils::execSql($pdo, $sql, array($sort,$prodid));
$sort++;
}
$pdo->commit();
echo json_encode(array("status" => "OK"));
}
2020-11-19 23:10:23 +01:00
function getMaxSortOfTypeInKat($pdo,$idOfKat) {
if (!is_null($idOfKat)) {
$sql = "SELECT MAX(sorting) as maxsort,reference FROM %prodtype% WHERE reference=? GROUP BY reference";
$maxSorting = CommonUtils::fetchSqlAll($pdo, $sql, array($idOfKat));
} else {
$sql = "SELECT MAX(sorting) as maxsort,reference FROM %prodtype% WHERE reference is null AND removed is null GROUP BY reference";
$maxSorting = CommonUtils::fetchSqlAll($pdo, $sql, null);
}
if (count($maxSorting) != 1) {
return null;
} else {
return intval($maxSorting[0]["maxsort"]) + 1;
}
}
2020-11-19 22:47:44 +01:00
function createProdType($id,$prodTypeName) {
2020-11-19 23:10:21 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
$pdo->beginTransaction();
if ($id == "top") {
2020-11-19 23:10:23 +01:00
$nextSort = $this->getMaxSortOfTypeInKat($pdo, null);
2020-11-19 23:10:21 +01:00
$kind = 0;
if (isset($_POST["kind"])) {
$kind = $_POST["kind"];
}
2020-11-19 23:12:05 +01:00
$sql = "INSERT INTO `%prodtype%` (`name`,`usekitchen`,`usesupplydesk`,`kind`,`printer`,`fixbind`,`sorting`,`reference`) VALUES(?,1,1,?,1,?,?,?)";
CommonUtils::execSql($pdo, $sql, array($prodTypeName,$kind,0,$nextSort,null));
2020-11-19 23:10:21 +01:00
$pdo->commit();
echo json_encode(array("status" => "OK"));
return;
}
2020-11-19 22:47:44 +01:00
if (!is_numeric($id)) {
echo json_encode(array("status" => "ERROR", "code" => ERROR_GENERAL_ID_TYPE, "msg" => ERROR_GENERAL_ID_TYPE_MSG));
return;
}
2020-11-19 23:10:23 +01:00
$nextSort = $this->getMaxSortOfTypeInKat($pdo, $id);
2020-11-19 22:47:44 +01:00
$sql = "SELECT kind FROM %prodtype% WHERE id=?";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($id));
$row =$stmt->fetchObject();
if ($row == null) {
echo json_encode(array("status" => "ERROR", "code" => ERROR_GENERAL_DB_NOT_READABLE, "msg" => ERROR_GENERAL_DB_NOT_READABLE_MSG));
return;
}
$kind = $row->kind;
2020-11-19 23:12:05 +01:00
$sql = "INSERT INTO `%prodtype%` (`name`,`usekitchen`,`usesupplydesk`,`kind`,`printer`,`fixbind`,`sorting`,`reference`) VALUES(?,?,?,?,?,?,?,?)";
CommonUtils::execSql($pdo, $sql, array($prodTypeName,1,1,$kind,1,0,$nextSort,$id));
2020-11-19 22:47:44 +01:00
$pdo->commit();
echo json_encode(array("status" => "OK"));
}
2020-11-19 23:00:42 +01:00
function createProduct(ProductEntry $prodEntry) {
2020-11-19 23:14:48 +01:00
$typeId = $prodEntry->getCategory();
2020-11-19 23:00:42 +01:00
if (!is_numeric($typeId)) {
2020-11-19 22:47:44 +01:00
return;
}
2020-11-19 23:00:42 +01:00
try {
2020-11-19 23:14:48 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
$pdo->beginTransaction();
$newProdId = $prodEntry->createProductInDb($pdo);
$this->sorter->setMaxSortingForProdId($pdo, $newProdId);
2020-11-19 22:47:44 +01:00
2020-11-19 23:14:48 +01:00
$pdo->commit();
2020-11-19 23:00:42 +01:00
} catch (Exception $e) {
2020-11-19 23:14:48 +01:00
$pdo->rollBack();
echo json_encode("ERROR: " . $e->getMessage());
return;
2020-11-19 23:00:42 +01:00
}
2020-11-19 23:14:48 +01:00
2020-11-19 22:59:47 +01:00
HistFiller::createProdInHist($pdo, $newProdId);
2020-11-19 22:47:44 +01:00
echo json_encode("OK: sql");
}
2020-11-19 23:14:48 +01:00
2020-11-19 22:47:44 +01:00
/*
* Change the properties of a type of products
*/
2020-11-19 23:12:05 +01:00
function applyType($id,$name,$kind,$usekitchen,$usesupply,$printer,$fixbind) {
if (!is_numeric($id) || !is_numeric($kind) || !is_numeric($usekitchen) || !is_numeric($usesupply) || !is_numeric($printer) || !is_numeric($fixbind)) {
2020-11-19 22:47:44 +01:00
return;
}
2020-11-19 23:12:05 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
$updateSql = "UPDATE %prodtype% SET kind=?, name=?, usekitchen=?, usesupplydesk=?, printer=?, fixbind=? WHERE id=?";
CommonUtils::execSql($pdo, $updateSql, array($kind,$name,$usekitchen,$usesupply,$printer,$fixbind,$id));
2020-11-19 22:47:44 +01:00
echo json_encode("OK: $updateSql");
}
function delType($id) {
if (!is_numeric($id)) {
echo json_encode(array("status" => "FAILED"));
}
$pdo = $this->dbutils->openDbAndReturnPdo();
$pdo->beginTransaction();
$this->delTypeCore($pdo, $id);
$pdo->commit();
echo json_encode(array("status" => "OK"));
}
function delTypeCore($pdo,$id) {
2020-11-19 22:54:51 +01:00
$allTypesInThisLevel = $this->getProdTypesWithReferenz($pdo,$id);
2020-11-19 22:47:44 +01:00
foreach ($allTypesInThisLevel as $aType) {
$this->delTypeCore($pdo, $aType["id"]);
}
2020-11-19 22:54:51 +01:00
$allProdsInThisLevel = $this->getProductsWithReferenz($pdo,$id);
2020-11-19 22:47:44 +01:00
foreach ($allProdsInThisLevel as $aProd) {
self::declareProductAsDeletedWithoutResort($pdo, $aProd["id"]);
}
self::declareTypeAsDeleted($pdo, $id);
}
static private function declareTypeAsDeleted($pdo,$id) {
$sql = "UPDATE %prodtype% SET removed=? WHERE id=?";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array(1,$id));
2020-11-19 23:10:23 +01:00
2020-11-19 23:14:48 +01:00
$sql = "SELECT COALESCE(reference,0) as reference FROM %prodtype% WHERE id=?";
2020-11-19 23:10:23 +01:00
$parentRef = CommonUtils::fetchSqlAll($pdo, $sql, array($id));
if (count($parentRef) != 1) {
return;
}
$parentId = $parentRef[0]['reference'];
$sql = "SELECT id,reference,sorting FROM %prodtype% WHERE reference=? AND removed is null ORDER BY sorting";
$allChildren = CommonUtils::fetchSqlAll($pdo, $sql, array($parentId));
$sort = 1;
$sql = "UPDATE %prodtype% SET sorting=? WHERE id=?";
foreach($allChildren as $c) {
CommonUtils::execSql($pdo, $sql, array($sort,$c["id"]));
$sort++;
}
2020-11-19 22:47:44 +01:00
}
static private function declareProductAsDeletedWithoutResort($pdo,$id) {
$sql = "UPDATE %products% SET removed=? WHERE id=?";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array(1,$id));
}
/*
* Return a html table with all products in a structured way
*/
2020-11-19 22:54:51 +01:00
private function getDbProductsWithRef_json_version($pdo,$ref,$depth) {
2020-11-19 22:47:44 +01:00
$allProdsAndTypesInThisLevel = array();
2020-11-19 22:54:51 +01:00
$allProdsInThisLevel = $this->getProductsWithReferenz($pdo,$ref);
$allTypesInThisLevel = $this->getProdTypesWithReferenz($pdo,$ref);
2020-11-19 22:47:44 +01:00
for ($index_prod=0;$index_prod<count($allProdsInThisLevel);$index_prod++) {
$aProd = $allProdsInThisLevel[$index_prod];
$allProdsAndTypesInThisLevel[] = array("entry" => $aProd, "content" => '');
}
for ($index_type=0;$index_type < count($allTypesInThisLevel);$index_type++) {
$aProdType = $allTypesInThisLevel[$index_type];
$typeRef = $aProdType['id'];
2020-11-19 22:54:51 +01:00
$allProdsAndTypesInThisLevel[] = array("entry" => $aProdType,"content" => $this->getDbProductsWithRef_json_version($pdo,$typeRef,$depth+1));
2020-11-19 22:47:44 +01:00
}
return $allProdsAndTypesInThisLevel;
}
2020-11-19 22:54:51 +01:00
private function readDbProductsWithRef_json_version($pdo,$ref,$depth) {
2020-11-19 22:59:47 +01:00
$decpoint = $this->getDecPoint($pdo);
2020-11-19 22:47:44 +01:00
$text = "";
2020-11-19 22:54:51 +01:00
$allProdsInThisLevel = $this->getProductsWithReferenz($pdo,$ref);
$allTypesInThisLevel = $this->getProdTypesWithReferenz($pdo,$ref);
2020-11-19 22:47:44 +01:00
for ($index_prod=0;$index_prod<count($allProdsInThisLevel);$index_prod++) {
$aProd = $allProdsInThisLevel[$index_prod];
2020-11-19 22:59:47 +01:00
$prodText = ProductEntry::createProductStr($aProd,$decpoint);
$text .= substr(" ", 0, $depth) . $prodText . "\n";
2020-11-19 22:47:44 +01:00
}
for ($index_type=0;$index_type < count($allTypesInThisLevel);$index_type++) {
$aProdType = $allTypesInThisLevel[$index_type];
$typeRef = $aProdType['id'];
$indent = substr ( " " , 0 ,$depth);
$prodTypeName = $aProdType['name'];
$kind = ($aProdType['kind'] == 0 ? "F" : "D");
$usekitchen = ($aProdType['usekitchen'] == 1 ? "K" : "");
$usesupplydesk = ($aProdType['usesupplydesk'] == 1 ? "B" : "");
$printer = ($aProdType['printer']);
2020-11-19 23:12:05 +01:00
$fixbind = ($aProdType['fixbind']);
$fixBindTxt = "RD";
if ($fixbind == 1) {
$fixBindTxt = "KD";
}
2020-11-19 22:47:44 +01:00
2020-11-19 23:12:05 +01:00
$text .= $indent . $prodTypeName . " = $usekitchen$usesupplydesk$kind = $printer = $fixBindTxt\n";
2020-11-19 22:47:44 +01:00
2020-11-19 22:54:51 +01:00
$text .= $this->readDbProductsWithRef_json_version($pdo,$typeRef,$depth+1);
2020-11-19 22:47:44 +01:00
}
return $text;
}
private function getPriceLevelInfo() {
2020-11-19 22:54:51 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
2020-11-19 22:47:44 +01:00
if(session_id() == '') {
session_start();
}
$lang = $_SESSION['language'];
2020-11-19 22:54:51 +01:00
$currentPriceLevel = $this->commonUtils->getCurrentPriceLevel($pdo);
2020-11-19 22:47:44 +01:00
$currentPriceLevelId = $currentPriceLevel["id"];
$currentPriceLevelName = $currentPriceLevel["name"];
$pricelevels = array();
$sql = "SELECT id,name,info FROM %pricelevel%";
if ($lang == 1) {
$sql = "SELECT id,name,info_en as info FROM %pricelevel%";
} else if ($lang == 2) {
$sql = "SELECT id,name,info_esp as info FROM %pricelevel%";
}
2020-11-19 22:54:51 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $zeile) {
2020-11-19 22:47:44 +01:00
$theId = $zeile['id'];
$selected = "0";
if ($theId == $currentPriceLevelId) {
$selected = "1";
}
$levels_entry = array(
"id" => $theId,
"name" => $zeile['name'],
"info" => $zeile['info'],
"selected" => $selected);
$pricelevels[] = $levels_entry;
}
$retArray = array("currentId" => $currentPriceLevelId, "currentName" => $currentPriceLevelName, "levels" => $pricelevels);
echo json_encode($retArray);
}
private function setPriceLevelInfo($levelId) {
if (is_numeric($levelId)) {
2020-11-19 23:12:50 +01:00
$pdo = DbUtils::openDbAndReturnPdoStatic();
$hist = new HistFiller();
$hist->updateConfigInHist($pdo, "pricelevel", $levelId);
2020-11-19 22:47:44 +01:00
echo json_encode("OK");
}
}
2020-11-19 22:59:47 +01:00
public function getSpeisekarte($pdo) {
2020-11-19 22:47:44 +01:00
$legend = file_get_contents("../customer/menulegend.txt");
2020-11-19 22:59:47 +01:00
$decpoint = $this->getDecPoint($pdo);
2020-11-19 22:47:44 +01:00
$sql = "SELECT * FROM %products% WHERE removed is null";
2020-11-19 22:59:47 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
2020-11-19 22:47:44 +01:00
$stmt->execute();
$numberOfProds = $stmt->rowCount();
$sql = "SELECT * FROM %prodtype% WHERE removed is null";
2020-11-19 22:59:47 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
2020-11-19 22:47:44 +01:00
$stmt->execute();
$numberOfProdTypes = $stmt->rowCount();
$predef = file_get_contents ("../customer/speisekarte.txt");
$predef = str_replace('{.}',$decpoint,$predef);
if (($numberOfProds == 0) && ($numberOfProdTypes == 0)) {
$text = $legend;
} else {
2020-11-19 22:54:51 +01:00
$text = $legend . $this->readDbProducts($pdo);
2020-11-19 22:47:44 +01:00
}
2020-11-19 22:59:47 +01:00
return array("status" => "OK","msg" => $text, "predef" => $predef);
2020-11-19 22:47:44 +01:00
}
private function endsWith($haystack, $needle)
{
return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
}
private function getAudioFiles() {
$dir = '../customer';
$fileList = scandir($dir);
$audioFiles = array();
foreach ($fileList as $aFile) {
if ($this->endsWith($aFile, '.mp3') || $this->endsWith($aFile, '.ogg') || $this->endsWith($aFile, '.wav')) {
$audioFiles[] = $aFile;
}
}
echo json_encode($audioFiles);
}
2020-11-19 23:12:24 +01:00
private static function getAllProdTypesOfActiveProds($pdo) {
$sql = "SELECT DISTINCT category from %products% WHERE removed is null";
$result = CommonUtils::fetchSqlAll($pdo, $sql);
$types = array();
foreach($result as $pt) {
$types[] = self::getProdIdChain($pdo, $pt['category']);
}
return $types;
}
public static function getAllActiveProducts($pdo) {
try {
$alltypes = self::getAllProdTypesOfActiveProds($pdo);
$prods = array();
foreach($alltypes as $t) {
$sql = "SELECT * from %products% WHERE removed is null AND category=? ORDER BY sorting";
$result = CommonUtils::fetchSqlAll($pdo, $sql,array($t["id"]));
$prods[] = array("id" => $t["id"],"chain" => $t["chain"],"products" => $result);
$t["products"] = $result;
//$prods[] = array("type" => $t,"products" => $result);
}
return array("status" => "OK","msg" => $prods);
} catch (Exception $ex) {
return array("status" => "ERROR","msg" => $ex->getMessage());
}
}
private static function getProdIdChain($pdo,$prodid) {
$nameArr = self::getProdIdChainRecursiveCore($pdo, $prodid,array());
$chainNameArr = array();
for ($i=(count($nameArr)-1);$i>=0;$i--) {
$chainNameArr[] = $nameArr[$i];
}
return array("id" => $prodid,"chain" => implode(" - ", $chainNameArr));
}
private static function getProdIdChainRecursiveCore($pdo,$prodid,$name) {
$sql = "SELECT name,reference FROM %prodtype% WHERE id=?";
$r = CommonUtils::fetchSqlAll($pdo, $sql, array($prodid));
$curName = $r[0]['name'];
$name[] = $curName;
$parent = $r[0]['reference'];
if (is_null($parent)) {
return $name;
} else {
return self::getProdIdChainRecursiveCore($pdo, $parent, $name);
}
}
public static function changesetofproducts($pdo,$changeset,$newprods) {
try {
$pdo->beginTransaction();
foreach($changeset as $aChangedProd) {
$prodid = $aChangedProd["prodid"];
$longname = $aChangedProd["longname"];
$shortname = $aChangedProd["shortname"];
if (($longname == "") && ($shortname == "")) {
$pdo->rollBack();
return array("status" => "ERROR","msg" => "Name eines Produkts fehlt");
}
if (($longname == "") && ($shortname != "")) {
$longname = $shortname;
} else if (($longname != "") && ($shortname == "")) {
$shortname = $longname;
}
$priceA = $aChangedProd["priceA"];
$priceB = $aChangedProd["priceB"];
$priceC = $aChangedProd["priceC"];
if (!is_numeric($priceA) || !is_numeric($priceB) || !is_numeric($priceC)) {
$pdo->rollBack();
return array("status" => "ERROR","msg" => "Nicht alle Preise sind numerisch");
}
$sql = "UPDATE %products% SET shortname=?, longname=?, priceA=?, priceB=?, priceC=? WHERE id=?";
CommonUtils::execSql($pdo, $sql, array($shortname,$longname,$priceA,$priceB,$priceC,$prodid));
HistFiller::updateProdInHist($pdo,$prodid);
}
$sorter = new Sorter();
foreach($newprods as $prod) {
$prodtypeid = $prod["prodtypeid"];
$longname = $prod["longname"];
$shortname = $prod["shortname"];
$priceA = $prod["priceA"];
$priceB = $prod["priceB"];
$priceC = $prod["priceC"];
if (!is_numeric($prodtypeid)) {
$pdo->rollBack();
return array("status" => "ERROR","msg" => "Prod.kategorie ungültig");
}
if (($priceA == "") || ($priceB == "") || ($priceC == "")) {
$pdo->rollBack();
return array("status" => "ERROR","msg" => "Preisangabe eines neuen Produkts fehlt");
}
if (!is_numeric($priceA) || !is_numeric($priceB) || !is_numeric($priceC)) {
$pdo->rollBack();
return array("status" => "ERROR","msg" => "Preisangabe eines neuen Produkts ungültig");
}
if (($longname == "") || ($shortname == "")) {
$pdo->rollBack();
return array("status" => "ERROR","msg" => "Name eines neuen Produkts fehlt");
}
if (($longname == "") && ($shortname != "")) {
$longname = $shortname;
} else if (($longname != "") && ($shortname == "")) {
$shortname = $longname;
}
$prodEntry = new ProductEntry();
$prodEntry->createWithSubsetOfData($prodtypeid,$longname,$shortname,$priceA,$priceB,$priceC);
$newProdId = $prodEntry->createProductInDb($pdo);
$sorter->setMaxSortingForProdId($pdo, $newProdId);
HistFiller::createProdInHist($pdo, $newProdId);
}
$pdo->commit();
return array("status" => "OK");
} catch(Exception $ex) {
$pdo->rollBack();
return array("status" => "ERROR","msg" => $ex->getMessage());
}
}
2020-11-19 23:10:23 +01:00
}