2020-11-19 22:47:44 +01:00
< ? php
require_once ( 'dbutils.php' );
2020-11-19 23:10:26 +01:00
defined ( 'T_ORDER' ) || define ( 'T_ORDER' , 0 );
defined ( 'T_BILL' ) || define ( 'T_BILL' , 1 );
defined ( 'T_REMOVE' ) || define ( 'T_REMOVE' , 2 );
defined ( 'T_BILLSTORNO' ) || define ( 'T_BILLSTORNO' , 3 );
defined ( 'T_BILLSTORNOREMOVE' ) || define ( 'T_BILLSTORNOREMOVE' , 4 );
defined ( 'T_FROM_TABLE' ) || define ( 'T_FROM_TABLE' , 5 );
defined ( 'T_TO_TABLE' ) || define ( 'T_TO_TABLE' , 6 );
2020-11-19 22:47:44 +01:00
2020-11-19 23:14:48 +01:00
defined ( 'RANGE_ORDER' ) || define ( 'RANGE_ORDER' , 1 );
defined ( 'RANGE_BILL' ) || define ( 'RANGE_BILL' , 2 );
defined ( 'RANGE_CLOSING' ) || define ( 'RANGE_CLOSING' , 3 );
2020-11-19 22:47:44 +01:00
class CommonUtils {
var $dbutils ;
2020-11-19 23:00:05 +01:00
private static $plugins = null ;
2020-11-19 22:47:44 +01:00
2020-11-19 23:00:05 +01:00
function __construct () {
$this -> dbutils = new DbUtils ();
// $this->products = new Products(); --> endless loop!
2020-11-19 22:47:44 +01:00
// $this->lastSettingOfDisplayMode = "all";
2020-11-19 23:00:05 +01:00
//error_reporting(E_ALL);
}
public static function setPluginConfig ( $plugins ) {
self :: $plugins = $plugins ;
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:12:35 +01:00
public static $g_units_arr =
array (
array ( " text " => " Stück " , " value " => 0 , " id " => " piece " ),
array ( " text " => " Eingabe " , " value " => 1 , " id " => " piece " ),
array ( " text " => " kg " , " value " => 2 , " id " => " kg " ),
array ( " text " => " gr " , " value " => 3 , " id " => " gr " ),
array ( " text " => " mg " , " value " => 4 , " id " => " mg " ),
array ( " text " => " l " , " value " => 5 , " id " => " l " ),
array ( " text " => " ml " , " value " => 6 , " id " => " ml " ),
2020-11-19 23:14:48 +01:00
array ( " text " => " m " , " value " => 7 , " id " => " m " ),
array ( " text " => " EinzweckgutscheinKauf " , " value " => 8 , " id " => " EGK " ),
array ( " text " => " EinzweckgutscheinEinl " , " value " => 9 , " id " => " EGE " )
2020-11-19 23:12:35 +01:00
);
public static function g_units_export_arr () {
return array ();
}
2020-11-19 22:47:44 +01:00
function verifyLastBillId ( $pdo , $nextIdToUse ) {
if ( $nextIdToUse == 1 ) {
return true ;
}
2020-11-19 23:00:05 +01:00
if ( is_null ( $pdo )) {
$pdo = $this -> dbutils -> openDbAndReturnPdo ();
2020-11-19 22:47:44 +01:00
}
$nextIdToUse = intval ( $nextIdToUse );
2020-11-19 23:00:05 +01:00
$sql = " SELECT value,signature FROM %work% WHERE item=? " ;
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
$stmt -> execute ( array ( " lastbillid " ));
$row = $stmt -> fetchObject ();
2020-11-19 22:47:44 +01:00
$lastBillid = intval ( $row -> value );
2020-11-19 23:00:05 +01:00
$lastBillInc = $lastBillid + 1 ;
2020-11-19 22:47:44 +01:00
if ( $lastBillInc != $nextIdToUse ) {
return false ;
} else {
2020-11-19 23:12:18 +01:00
$sql = " SELECT id FROM %bill% WHERE id=? " ;
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
$stmt -> execute ( array ( $nextIdToUse ));
if ( $stmt -> rowCount () > 0 ) {
2020-11-19 23:00:05 +01:00
return false ;
2020-11-19 22:47:44 +01:00
} else {
2020-11-19 23:12:18 +01:00
// is there a gap or does the previous id exist?
2020-11-19 22:47:44 +01:00
$sql = " SELECT id FROM %bill% WHERE id=? " ;
2020-11-19 23:00:05 +01:00
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
2020-11-19 23:12:18 +01:00
$stmt -> execute ( array ( $nextIdToUse - 1 ));
if ( $stmt -> rowCount () != 1 ) {
2020-11-19 22:47:44 +01:00
return false ;
} else {
2020-11-19 23:12:18 +01:00
return true ;
2020-11-19 23:00:05 +01:00
}
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:12:18 +01:00
2020-11-19 22:47:44 +01:00
}
}
2020-11-19 23:12:07 +01:00
2020-11-19 22:47:44 +01:00
function getKeyFromWorkTable ( $pdo , $key ) {
$sql = " SELECT signature FROM %work% WHERE item=? " ;
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
$stmt -> execute ( array ( $key ));
$row = $stmt -> fetchObject ();
return ( $row -> signature );
}
2020-11-19 23:12:07 +01:00
public static function setMd5OfLastBillidInWorkTable ( $pdo ) {
$sql = " SELECT value FROM %work% where item=? " ;
$r = self :: fetchSqlAll ( $pdo , $sql , array ( " lastbillid " ));
if ( count ( $r ) > 0 ) {
$maxid = $r [ 0 ][ " value " ];
$signature = md5 ( " B( $maxid ) " );
$sql = " UPDATE %work% SET signature=? WHERE item=? " ;
self :: execSql ( $pdo , $sql , array ( $signature , " lastbillid " ));
}
}
2020-11-19 22:47:44 +01:00
function setLastBillIdInWorkTable ( $pdo , $lastBillId ) {
2020-11-19 23:00:05 +01:00
if ( is_null ( $pdo )) {
$pdo = $this -> dbutils -> openDbAndReturnPdo ();
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:12:07 +01:00
$signature = md5 ( " B( $lastBillId ) " );
2020-11-19 23:00:05 +01:00
$sql = " UPDATE %work% SET value=?, signature=? WHERE item=? " ;
2020-11-19 22:47:44 +01:00
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
$stmt -> execute ( array ( $lastBillId , $signature , " lastbillid " ));
}
function verifyBill ( $pdo , $id ) {
if ( is_null ( $pdo )) {
$pdo = $this -> dbutils -> openDbAndReturnPdo ();
}
2020-11-19 23:10:09 +01:00
$sql = " SELECT billdate,brutto,ROUND(netto,2) as netto,userid,IF(tax is not null, tax, '0.00') as tax,signature,status FROM %bill% WHERE id=? " ;
2020-11-19 23:00:05 +01:00
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
$stmt -> execute ( array ( $id ));
$row = $stmt -> fetchObject ();
2020-11-19 22:47:44 +01:00
$billdate = $row -> billdate ;
$brutto = $row -> brutto ;
$netto = $row -> netto ;
$tax = $row -> tax ;
$userid = $row -> userid ;
$signature = $row -> signature ;
2020-11-19 23:03:29 +01:00
$status = $row -> status ;
2020-11-19 23:12:07 +01:00
return ( self :: verifyBillByValues ( $pdo , $billdate , $brutto , $netto , $userid , $signature , $status ));
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:12:07 +01:00
public static function verifyBillByValues ( $pdo , $billdate , $brutto , $netto , $userid , $signature , $status ) {
2020-11-19 23:03:29 +01:00
if (( $status == " c " ) && ( $brutto == 0.00 )) {
// workaround - the signature for cash inserts of vaue 0.00 are sometimes incorrect
return true ;
}
2020-11-19 23:00:05 +01:00
if ( is_null ( $signature )) {
return false ;
2020-11-19 22:47:44 +01:00
}
if ( is_null ( $pdo )) {
2020-11-19 23:12:07 +01:00
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:00:05 +01:00
$brutto = number_format ( $brutto , 2 , " . " , '' );
$netto = number_format ( $netto , 2 , " . " , '' );
2020-11-19 23:12:07 +01:00
$data = " D( $billdate )B( $brutto )N( $netto )T(0)U( $userid ) " ;
$md5OfData = md5 ( $data );
if ( $signature != $md5OfData ) {
2020-11-19 23:00:05 +01:00
return false ;
} else {
return true ;
2020-11-19 22:47:44 +01:00
}
}
2020-11-19 23:12:07 +01:00
public static function calcSignaturesForAllBills ( $pdo ) {
$sql = " SELECT id,billdate,brutto,netto,userid FROM %bill% " ;
$r = CommonUtils :: fetchSqlAll ( $pdo , $sql );
$sql = " UPDATE %bill% SET signature=? WHERE id=? " ;
foreach ( $r as $b ) {
$bruttostr = number_format ( $b [ " brutto " ], 2 , " . " , '' );
$nettostr = number_format ( $b [ " netto " ], 2 , " . " , '' );
$theTime = $b [ " billdate " ];
$userid = $b [ " userid " ];
$data = md5 ( " D( $theTime )B( $bruttostr )N( $nettostr )T(0)U( $userid ) " );
CommonUtils :: execSql ( $pdo , $sql , array ( $data , $b [ " id " ]));
}
}
2020-11-19 22:47:44 +01:00
2020-11-19 23:12:07 +01:00
public static function calcSignatureForBill ( $theTime , $brutto , $netto , $userid ) {
2020-11-19 22:47:44 +01:00
// now calculate the signature for the bill entry
$bruttostr = number_format ( $brutto , 2 , " . " , '' );
$nettostr = number_format ( $netto , 2 , " . " , '' );
2020-11-19 23:12:07 +01:00
$data = " D( $theTime )B( $bruttostr )N( $nettostr )T(0)U( $userid ) " ;
$signature = md5 ( $data );
2020-11-19 22:47:44 +01:00
return $signature ;
}
function createGridTableWithSqrtSizeOfButtons ( $inputArray ) {
2020-11-19 23:00:05 +01:00
// create a table that is optimal (sqrt-like size)
2020-11-19 22:47:44 +01:00
$numberOfIcons = count ( $inputArray );
if ( $numberOfIcons == 0 ) {
// no items to display
return ;
2020-11-19 23:00:05 +01:00
}
$numberOfCols = ceil ( sqrt ( $numberOfIcons ));
$porcentageWidth = floor ( 100 / $numberOfCols );
echo '<table class=gridtable>' ;
2020-11-19 22:47:44 +01:00
$colcounter = 0 ;
for ( $index = 0 ; $index < $numberOfIcons ; $index ++ ) {
2020-11-19 23:00:05 +01:00
if ( $colcounter == 0 ) {
echo " <tr><td> " ;
2020-11-19 22:47:44 +01:00
}
$anEntry = $inputArray [ $index ];
$textOfButton = $anEntry [ " textOfButton " ]; #
$onClickMethod = $anEntry [ " onClickMethod " ]; // With parameters!
$button = '<input type="button" value="' . $textOfButton . '"' ;
$button = $button . ' onclick="' . $onClickMethod . '"' ;
$button = $button . ' style="height: 50px; width:' . $porcentageWidth . '%; font-size:20px; background-color:#b3b3c9" />' ;
echo $button ;
$colcounter ++ ;
if ( $colcounter == $numberOfCols ) {
$colcounter = 0 ;
echo " </tr> " ;
}
2020-11-19 23:00:05 +01:00
}
echo " </tr> " ;
2020-11-19 22:47:44 +01:00
echo " </table> " ;
}
function createGridTableWithSqrtSizeOfStyleButtons ( $inputArray ) {
$this -> createGridTableWithSqrtSizeOfStyleButtonsAndHeader ( $inputArray , '' , 'dummy' );
}
2020-11-19 22:54:51 +01:00
function getTableNameFromId ( $pdo , $tableid ) {
2020-11-19 22:47:44 +01:00
if ( is_null ( $tableid ) || ( $tableid == 0 )) {
return " - " ; // togo
2020-11-19 23:00:05 +01:00
}
2020-11-19 22:54:51 +01:00
$sql = " SELECT tableno FROM %resttables% WHERE id=? " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $tableid ));
2020-11-19 23:00:05 +01:00
$row = $stmt -> fetchObject ();
return $row -> tableno ;
2020-11-19 22:47:44 +01:00
}
2020-11-19 22:54:51 +01:00
function getCurrentPriceLevel ( $pdo ) {
2020-11-19 22:47:44 +01:00
$sql = " SELECT setting FROM %config% WHERE name='pricelevel' " ;
2020-11-19 22:54:51 +01:00
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ();
$row = $stmt -> fetchObject ();
$pricelevelid = $row -> setting ;
2020-11-19 22:47:44 +01:00
2020-11-19 22:54:51 +01:00
$sql = " SELECT id,name FROM %pricelevel% WHERE id=? " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $pricelevelid ));
$row = $stmt -> fetchObject ();
$pricelevelname = $row -> name ;
2020-11-19 22:47:44 +01:00
return ( array ( " id " => $pricelevelid , " name " => $pricelevelname ));
}
2020-11-19 23:00:05 +01:00
function createGridTableWithSqrtSizeOfStyleButtonsAndHeader ( $inputArray , $headline , $headercolor ) {
// create a table that is optimal (sqrt-like size)
2020-11-19 22:47:44 +01:00
$numberOfIcons = count ( $inputArray );
2020-11-19 23:00:05 +01:00
if ( $numberOfIcons == 0 ) {
// no items to display
return ;
}
$numberOfCols = ceil ( sqrt ( $numberOfIcons ));
$porcentageWidth = floor ( 100.0 / $numberOfCols );
2020-11-19 22:47:44 +01:00
echo '<table class=gridtable>' ;
// Headline
if ( $headline <> '' ) {
echo '<tr><th style="background-color:#' . $headercolor . '">' . $headline . '</th>' ;
}
2020-11-19 23:00:05 +01:00
$colcounter = 0 ;
for ( $index = 0 ; $index < $numberOfIcons ; $index ++ ) {
if ( $colcounter == 0 ) {
echo " <tr><td> " ;
}
$anEntry = $inputArray [ $index ];
$textOfButton = $anEntry [ " textOfButton " ]; #
2020-11-19 22:47:44 +01:00
$onClickMethod = $anEntry [ " onClickMethod " ]; // With parameters!
2020-11-19 23:00:05 +01:00
$style = $anEntry [ " style " ];
2020-11-19 22:47:44 +01:00
$button = '<input type="button" value="' . $textOfButton . '"' ;
2020-11-19 23:00:05 +01:00
$button = $button . ' onclick="' . $onClickMethod . '"' ;
$button = $button . ' style="' . $style . '; width:' . $porcentageWidth . '%;" />' ;
echo $button ;
$colcounter ++ ;
if ( $colcounter == $numberOfCols ) {
$colcounter = 0 ;
echo " </tr> " ;
}
}
echo " </tr> " ;
echo " </table> " ;
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:00:05 +01:00
function getCurrency () {
$pdo = $this -> dbutils -> openDbAndReturnPdo ();
2020-11-19 22:47:44 +01:00
2020-11-19 23:00:05 +01:00
$sql = " SELECT setting from %config% where name='currency' " ;
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
$stmt -> execute ();
$row = $stmt -> fetchObject ();
if ( $row != null ) {
return $row -> setting ;
2020-11-19 22:47:44 +01:00
} else {
return " Euro " ;
2020-11-19 23:00:05 +01:00
}
}
2020-11-19 23:11:33 +01:00
public static function getRowSqlObject ( $pdo , $sql , $params = null ) {
2020-11-19 23:00:55 +01:00
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
if ( is_null ( $params )) {
$stmt -> execute ();
} else {
$stmt -> execute ( $params );
}
return ( $stmt -> fetchObject ());
}
2020-11-19 23:11:33 +01:00
public static function fetchSqlAll ( $pdo , $sql , $params = null ) {
2020-11-19 23:00:58 +01:00
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
if ( is_null ( $params )) {
$stmt -> execute ();
} else {
$stmt -> execute ( $params );
}
2020-11-19 23:10:06 +01:00
return ( $stmt -> fetchAll ( PDO :: FETCH_ASSOC ));
2020-11-19 23:00:58 +01:00
}
2020-11-19 23:00:55 +01:00
public static function execSql ( $pdo , $sql , $params ) {
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
if ( is_null ( $params )) {
$stmt -> execute ();
} else {
$stmt -> execute ( $params );
}
}
2020-11-19 23:03:23 +01:00
public static function getConfigValueStmt ( $pdo , $stmt , $item , $default ) {
$stmt -> execute ( array ( $item ));
$row = $stmt -> fetchObject ();
if ( $row -> countid == 0 ) {
return $default ;
} else {
return self :: getExistingConfigValue ( $pdo , $item );
}
}
2020-11-19 23:00:05 +01:00
public static function getConfigValue ( $pdo , $item , $default ) {
$sql = " SELECT count(id) as countid FROM %config% WHERE name=? " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $item ));
2020-11-19 23:14:10 +01:00
$row = $stmt -> fetchObject ();
2020-11-19 23:00:05 +01:00
if ( $row -> countid == 0 ) {
return $default ;
} else {
2020-11-19 23:00:35 +01:00
return self :: getExistingConfigValue ( $pdo , $item );
2020-11-19 23:00:05 +01:00
}
}
2020-11-19 23:14:10 +01:00
public static function getConfigValueAtClosingTime ( $pdo , $item , $default , $closingid ) {
if ( is_null ( $closingid )) {
return self :: getConfigValue ( $pdo , $item , $default );
} else {
$sql1 = " SELECT MAX(H.id) from %hist% H,%histconfig% C,%config% CO WHERE H.refid=C.id AND (H.action=2 OR H.action=6) AND C.configid=CO.id AND CO.name=? AND H.clsid <= ? " ;
$sql = " SELECT setting FROM %histconfig% HCO,%hist% H WHERE H.refid=HCO.id AND H.id=( $sql1 ) " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $item , $closingid ));
$r = $stmt -> fetchAll ( PDO :: FETCH_ASSOC );
if ( count ( $r ) == 0 ) {
return $default ;
} else {
return $r [ 0 ][ " setting " ];
}
}
}
public static function getConfigValueAtDateTime ( $pdo , $item , $default , $datetime ) {
$sql1 = " SELECT MAX(HC.id) as hcid FROM %hist% H, %histconfig% HC, %config% C WHERE date < ? and H.refid=HC.id AND HC.configid=C.id AND (H.action=2 OR H.action=6) AND C.name=? " ;
$sql = " SELECT setting FROM %histconfig% where id=( $sql1 ) " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $datetime , $item ));
$r = $stmt -> fetchAll ( PDO :: FETCH_ASSOC );
if ( count ( $r ) == 0 ) {
return $default ;
} else {
return $r [ 0 ][ " setting " ];
}
}
2020-11-19 23:00:31 +01:00
public static function getExistingConfigValue ( $pdo , $item ) {
$sql = " SELECT setting FROM %config% WHERE name=? " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $item ));
$row = $stmt -> fetchObject ();
return $row -> setting ;
}
2020-11-19 23:00:05 +01:00
public static function callPlugin ( $pdo , $fct , $condition ) {
try {
if ( ! is_null ( self :: $plugins )) {
if ( array_key_exists ( $fct , self :: $plugins )) {
$plugin = self :: $plugins -> $fct ;
if (( $plugin -> execution ) === $condition ) {
$cls = $plugin -> PluginClass ;
$fct = $plugin -> PluginFct ;
$call = " Plugin \\ $cls :: $fct " ;
call_user_func ( $call , $pdo );
return true ;
}
}
}
} catch ( Exception $e ) { }
return false ;
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:00:18 +01:00
2020-11-19 23:03:29 +01:00
public static function log ( $pdo , $component , $message ) {
$dblog = self :: getConfigValue ( $pdo , " dblog " , 1 );
if ( $dblog == 1 ) {
2020-11-19 23:12:59 +01:00
date_default_timezone_set ( DbUtils :: getTimeZoneDb ( $pdo ));
2020-11-19 23:03:29 +01:00
$currentTime = date ( 'Y-m-d H:i:s' );
$sql = " INSERT INTO %log% (date,component,message) VALUES(?,?,?) " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $currentTime , $component , $message ));
}
2020-11-19 23:00:18 +01:00
}
2020-11-19 23:03:29 +01:00
2020-11-19 23:00:18 +01:00
public static function getLog ( $pdo ) {
$sql = " SELECT date,component,message FROM %log% " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ();
$result = $stmt -> fetchAll ();
$txt = " " ;
foreach ( $result as $aLogLine ) {
$txt .= $aLogLine [ " date " ] . " ; " . $aLogLine [ " component " ] . " ; " . $aLogLine [ " message " ] . " \n " ;
}
return $txt ;
}
public static function getLastLog ( $pdo ) {
$sql = " SELECT date,component,message FROM %log% WHERE DATE_SUB(NOW(),INTERVAL 2 HOUR) <= date " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ();
$result = $stmt -> fetchAll ();
$txt = " " ;
foreach ( $result as $aLogLine ) {
$txt .= $aLogLine [ " date " ] . " ; " . $aLogLine [ " component " ] . " ; " . $aLogLine [ " message " ] . " \n " ;
}
return $txt ;
}
public static function keepOnlyLastLog ( $pdo ) {
$sql = " DELETE FROM %log% WHERE DATE_SUB(NOW(),INTERVAL 2 HOUR) > date " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ();
}
2020-11-19 23:03:43 +01:00
public static function strEndsWith ( $haystack , $needle )
{
return $needle === " " || substr ( $haystack , - strlen ( $needle )) === $needle ;
}
2020-11-19 23:10:26 +01:00
public static function startsWith ( $aText , $needle )
{
return $needle === " " || strpos ( $aText , $needle ) === 0 ;
}
2020-11-19 23:12:35 +01:00
public static function caseOfSqlUnitSelection ( $pdo ) {
$decpoint = htmlspecialchars ( CommonUtils :: getConfigValue ( $pdo , " decpoint " , " . " ));
$unit = " CASE " ;
foreach ( CommonUtils :: $g_units_arr as $aUnit ) {
2020-11-19 23:14:48 +01:00
if (( $aUnit [ " value " ] > 1 ) && ( $aUnit [ " value " ] < 8 )) {
2020-11-19 23:12:35 +01:00
$unit .= " WHEN Q.unit=' " . $aUnit [ " value " ] . " ' THEN CONCAT(REPLACE(unitamount,'.',' $decpoint '),' " . $aUnit [ " text " ] . " ',' ') " ;
}
}
$unit .= " ELSE '' " ;
$unit .= " END " ;
return $unit ;
}
2020-11-19 23:12:37 +01:00
public static function scaleImg ( $fn , $maxDim ) {
list ( $width , $height , $type , $attr ) = getimagesize ( $fn );
$size = getimagesize ( $fn );
$ratio = $size [ 0 ] / $size [ 1 ]; // width/height
if ( $ratio > 1 ) {
$width = $maxDim ;
$height = $maxDim / $ratio ;
} else {
$width = $maxDim * $ratio ;
$height = $maxDim ;
}
$src = imagecreatefromstring ( file_get_contents ( $fn ));
$dst = imagecreatetruecolor ( $width , $height );
imagealphablending ( $dst , false );
imagesavealpha ( $dst , true );
$transparent = imagecolorallocatealpha ( $dst , 255 , 255 , 255 , 127 );
imagefilledrectangle ( $dst , 0 , 0 , $width , $height , $transparent );
imagecopyresampled ( $dst , $src , 0 , 0 , 0 , 0 , $width , $height , $size [ 0 ], $size [ 1 ]);
imagedestroy ( $src );
ob_start ();
imagepng ( $dst ); // adjust format as needed
$imagedata = ob_get_contents ();
ob_end_clean ();
imagedestroy ( $dst );
return $imagedata ;
}
2020-11-19 23:10:26 +01:00
2020-11-19 23:14:02 +01:00
public static function getFirstSqlQuery ( $pdo , $sql , $params , $default ) {
$result = self :: fetchSqlAll ( $pdo , $sql , $params );
if ( count ( $result ) > 0 ) {
return $result [ 0 ][ " value " ];
} else {
return $default ;
}
}
2020-11-19 23:14:10 +01:00
public static function canMasterDataBeChanged ( $pdo ) {
2020-11-19 23:14:13 +01:00
$sql = " SELECT COUNT(id) as countid FROM %queue% WHERE isclosed is null OR isclosed='0' " ;
2020-11-19 23:14:10 +01:00
$res = CommonUtils :: fetchSqlAll ( $pdo , $sql );
if ( intval ( $res [ 0 ][ " countid " ]) > 0 ) {
return false ;
} else {
return true ;
}
}
2020-11-19 23:14:48 +01:00
public static function checkRights ( $command , $rights ) {
if ( session_id () == '' ) {
session_start ();
}
if ( ! array_key_exists ( $command , $rights )) {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_COMMAND_NOT_FOUND , " msg " => ERROR_COMMAND_NOT_FOUND_MSG ));
return false ;
}
$cmdRights = $rights [ $command ];
if ( $cmdRights [ " loggedin " ] == 1 ) {
if ( ! isset ( $_SESSION [ 'angemeldet' ]) || ! $_SESSION [ 'angemeldet' ]) {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_NOT_AUTHOTRIZED , " msg " => ERROR_NOT_AUTHOTRIZED_MSG ));
return false ;
}
}
if ( $cmdRights [ " isadmin " ] == 1 ) {
if ( ! isset ( $_SESSION [ 'angemeldet' ]) || ! $_SESSION [ 'angemeldet' ]) {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_NOT_AUTHOTRIZED , " msg " => ERROR_NOT_AUTHOTRIZED_MSG ));
return false ;
} else {
if ( $_SESSION [ 'is_admin' ] == false ) {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_COMMAND_NOT_ADMIN , " msg " => ERROR_COMMAND_NOT_ADMIN_MSG ));
return false ;
}
}
}
if ( ! is_null ( $cmdRights [ " rights " ])) {
foreach ( $cmdRights [ " rights " ] as $aRight ) {
if ( $aRight == 'manager_or_admin' ) {
if (( $_SESSION [ 'is_admin' ]) || ( $_SESSION [ 'right_manager' ])) {
return true ;
}
} else if ( $aRight == 'dash' ) {
if ( $_SESSION [ 'right_dash' ]) {
return true ;
}
}
}
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_NOT_AUTHOTRIZED , " msg " => ERROR_NOT_AUTHOTRIZED_MSG ));
return false ;
}
return true ;
}
public static function base64_encode_url ( $string ) {
$null = base64_encode ( $string );
$eins = strtr ( $null , '+/' , '-_' );
$zwei = rtrim ( $eins , '=' );
$bla = $zwei ;
return rtrim ( strtr ( base64_encode ( $string ), '+/' , '-_' ), '=' );
}
public static function base64_decode_url ( $string ) {
return base64_decode ( str_replace ([ '-' , '_' ], [ '+' , '/' ], $string ));
}
public static function referenceValueInTseValuesTable ( $pdo , $tsevalue ) {
$sql = " SELECT id FROM %tsevalues% WHERE textvalue=? " ;
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $tsevalue ));
if ( count ( $result ) == 0 ) {
$sql = " INSERT INTO %tsevalues% (textvalue) VALUES(?) " ;
CommonUtils :: execSql ( $pdo , $sql , array ( $tsevalue ));
return $pdo -> lastInsertId ();
} else {
return $result [ 0 ][ " id " ];
}
}
public static function outputEmptyImage () {
header ( " Content-Type: image/png " );
$my_img = imagecreate ( 1 , 1 );
$background = imagecolorallocate ( $my_img , 255 , 255 , 255 );
$black = imagecolorallocate ( $my_img , 0 , 0 , 0 );
imagecolortransparent ( $my_img , $black );
imagepng ( $my_img );
imagecolordeallocate ( $my_img , $background );
imagecolordeallocate ( $my_img , $black );
imagedestroy ( $my_img );
}
public static function outputWideEmptyImage () {
header ( " Content-Type: image/png " );
$my_img = imagecreate ( 1000 , 10 );
$background = imagecolorallocate ( $my_img , 255 , 255 , 255 );
$black = imagecolorallocate ( $my_img , 0 , 0 , 0 );
imagecolortransparent ( $my_img , $black );
imagepng ( $my_img );
imagecolordeallocate ( $my_img , $background );
imagecolordeallocate ( $my_img , $black );
imagedestroy ( $my_img );
}
public static function getMasterDataAtCertainDateTime ( $pdo , $thedatetime , $templatename ) {
$sql = " SELECT H.date from %hist% H, %histconfig% HC, %config% C WHERE HC.configid=C.id AND C.name='usetse' and H.refid=HC.id AND H.action='2' " ;
$resut = CommonUtils :: fetchSqlAll ( $pdo , $sql );
$dateOf2_0_0 = $resut [ 0 ][ " date " ];
$systemParams = array (
array ( " companyinfo " , " 2010-01-01 00:00:00 " ),
array ( " hosttext " , " 2010-01-01 00:00:00 " ),
array ( " uid " , $dateOf2_0_0 ),
array ( " sn " , $dateOf2_0_0 ),
array ( " systemid " , $dateOf2_0_0 ),
array ( $templatename , $dateOf2_0_0 ),
array ( " cashtemplate " , $dateOf2_0_0 ),
array ( " coinvalname " , $dateOf2_0_0 ),
array ( " notevalname " , $dateOf2_0_0 ),
array ( " dsfinvk_name " , $dateOf2_0_0 ),
array ( " dsfinvk_street " , $dateOf2_0_0 ),
array ( " dsfinvk_postalcode " , $dateOf2_0_0 ),
array ( " dsfinvk_city " , $dateOf2_0_0 ),
array ( " dsfinvk_country " , $dateOf2_0_0 ),
array ( " dsfinvk_stnr " , $dateOf2_0_0 ),
array ( " dsfinvk_ustid " , $dateOf2_0_0 ),
array ( " version " , " 2010-01-01 00:00:00 " ));
$sql = " SELECT setting FROM %histconfig% HC where id=( " ;
$sql .= " SELECT MAX(HC.id) as maxid from %hist% H, %histconfig% HC, %config% C WHERE HC.configid=C.id AND C.name=? and H.refid=HC.id " ;
$sql .= " AND (H.action='2' OR H.action='6') " ;
$sql .= " AND H.date <= GREATEST(?,?) " ;
$sql .= " ) " ;
$out = array ();
foreach ( $systemParams as $aParam ) {
$theParamName = $aParam [ 0 ];
$minDate = $aParam [ 1 ];
$settingResult = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $theParamName , $thedatetime , $minDate ));
if ( $theParamName == $templatename ) {
$out [ 'template' ] = $settingResult [ 0 ][ " setting " ];
} else {
$out [ $theParamName ] = $settingResult [ 0 ][ " setting " ];
}
}
return $out ;
}
public static function outTransImage () {
$name = '../img/trans.png' ;
$fp = fopen ( $name , 'rb' );
header ( " Content-Type: image/png " );
header ( " Content-Length: " . filesize ( $name ));
fpassthru ( $fp );
}
public static function getTaxesArray ( $pdo ) {
$normaltax = CommonUtils :: getConfigValue ( $pdo , 'tax' , 19.00 );
$togotax = CommonUtils :: getConfigValue ( $pdo , 'togotax' , 7.00 );
$taxes = array (
array ( " key " => 1 , " value " => $normaltax , " name " => " Allgemeiner Steuersatz (§ 12 Abs. 1 UStG) " ),
array ( " key " => 2 , " value " => $togotax , " name " => " Ermäßigter Steuersatz (§ 12 Abs. 2 UStG) " ),
array ( " key " => 3 , " value " => 10.70 , " name " => " Durchschnittsatz (§ 24 Abs. 1 Nr. 3 UStG) übrige Fälle " ),
array ( " key " => 4 , " value " => 5.50 , " name " => " Durchschnittsatz (§ 24 Abs. 1 Nr. 1 UStG) " ),
array ( " key " => 5 , " value " => 0.00 , " name " => " Nicht Steuerbar " ),
array ( " key " => 6 , " value " => 0.00 , " name " => " Umsatzsteuerfrei " ),
array ( " key " => 7 , " value " => 0.00 , " name " => " UmsatzsteuerNichtErmittelbar " ),
array ( " key " => 11 , " value " => 19.00 , " name " => " Historischer allgemeiner Steuersatz (§ 12 Abs. 1 UStG) " ),
array ( " key " => 12 , " value " => 7.00 , " name " => " Historischer ermäßigter Steuersatz (§ 12 Abs. 2 UStG) " ),
array ( " key " => 21 , " value " => 16.00 , " name " => " Historischer allgemeiner Steuersatz (§ 12 Abs. 1 UStG) " ),
array ( " key " => 22 , " value " => 5.00 , " name " => " Historischer ermäßigter Steuersatz (§ 12 Abs. 2 UStG) " ),
);
return $taxes ;
}
public static function getTaxFromKey ( $pdo , $taxkey ) {
$taxes = self :: getTaxesArray ( $pdo );
foreach ( $taxes as $t ) {
if ( $taxkey == $t [ " key " ]) {
return $t [ " value " ];
}
}
return 0.0 ;
}
public static function getTaxDescriptionFromKey ( $pdo , $taxkey ) {
$taxes = self :: getTaxesArray ( $pdo );
foreach ( $taxes as $t ) {
if ( $taxkey == $t [ " key " ]) {
return $t [ " name " ];
}
}
return 0.0 ;
}
public static function getCurrencyAsIsoVal ( $pdo ) {
$currency = self :: getConfigValue ( $pdo , 'currency' , 'Euro' );
if ( in_array ( strtoupper ( $currency ), array ( " EURO " , " EUR " , " E " , " € " ))) {
return " EUR " ;
}
return $currency ;
}
2020-11-19 23:03:29 +01:00
}