2020-11-19 22:44:19 +01:00
< ? php
// Datenbank-Verbindungsparameter
require_once ( 'dbutils.php' );
require_once ( 'queuecontent.php' );
require_once ( 'commonutils.php' );
require_once ( 'utilities/userrights.php' );
class Reports {
var $dbutils ;
var $queue ;
var $commonUtils ;
var $userrights ;
2020-11-19 22:47:44 +01:00
static $sql_service_good = " " ;
static $sql_service_ok = " " ;
static $sql_service_bad = " " ;
static $sql_kitchen_good = " " ;
static $sql_kitchen_ok = " " ;
static $sql_kitchen_bad = " " ;
static $sql_ratings = " " ;
static $sql_remarks = " " ;
2020-11-19 22:44:19 +01:00
function __construct () {
$this -> dbutils = new DbUtils ();
$this -> queue = new QueueContent ();
$this -> commonUtils = new CommonUtils ();
$this -> userrights = new Userrights ();
2020-11-19 23:03:20 +01:00
}
public function createSqlPhrases () {
2020-11-19 22:47:44 +01:00
self :: $sql_service_good = " select COUNT(service) as count FROM %ratings% WHERE service='1' AND date between ? AND ? " ;
self :: $sql_service_ok = " select COUNT(service) as count FROM %ratings% WHERE service='2' AND date between ? AND ? " ;
self :: $sql_service_bad = " select COUNT(service) as count FROM %ratings% WHERE service='3' AND date between ? AND ? " ;
self :: $sql_kitchen_good = " select COUNT(kitchen) as count FROM %ratings% WHERE kitchen='1' AND date between ? AND ? " ;
self :: $sql_kitchen_ok = " select COUNT(kitchen) as count FROM %ratings% WHERE kitchen='2' AND date between ? AND ? " ;
self :: $sql_kitchen_bad = " select COUNT(kitchen) as count FROM %ratings% WHERE kitchen='3' AND date between ? AND ? " ;
self :: $sql_ratings = " select COUNT(id) as count FROM %ratings% WHERE date between ? AND ? " ;
2020-11-19 23:12:39 +01:00
self :: $sql_remarks = " SELECT DATE_FORMAT(date, '%e.%m.%Y %H:%i') AS date,remark FROM %ratings% WHERE CHAR_LENGTH(remark) > 2 AND date between ? AND ? " ;
2020-11-19 22:44:19 +01:00
}
function handleCommand ( $command ) {
2020-11-19 23:00:55 +01:00
header ( " Cache-Control: no-store, no-cache, must-revalidate, max-age=0 " );
header ( " Cache-Control: post-check=0, pre-check=0 " , false );
header ( " Pragma: no-cache " );
// canUserCallCommands($currentCmd, $cmdArray,$right)
$cmdArray = array ( 'getStats' );
if ( in_array ( $command , $cmdArray )) {
if ( ! ( $this -> userrights -> hasCurrentUserRight ( 'right_statistics' ))) {
echo " Benutzerrechte nicht ausreichend! " ;
return false ;
}
2020-11-19 22:44:19 +01:00
}
2020-11-19 23:12:46 +01:00
$this -> createSqlPhrases ();
$allDates = self :: getDates ();
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
switch ( $command ) {
case 'getStats' :
$this -> getStats ( $pdo );
break ;
case 'getUsersums' :
$values = $this -> getUsersums ( $pdo );
echo json_encode ( array ( " status " => " OK " , " msg " => $values ));
break ;
case 'getToday' :
$values = $this -> iterateForHours ( $pdo , $allDates [ 'todayDate' ], intval ( $allDates [ 'todayHour' ]) + 1 , false );
echo json_encode ( array ( " status " => " OK " , " msg " => $values ));
break ;
case 'getYesterday' :
$values = $this -> iterateForHours ( $pdo , $allDates [ 'yesterdayDate' ], 24 , true );
echo json_encode ( array ( " status " => " OK " , " msg " => $values ));
break ;
case 'getThismonth' :
$values = $this -> iterateForDays ( $pdo , $allDates [ 'monthAndYearOfThisMonth' ], intval ( $allDates [ 'currentDay' ]), true );
echo json_encode ( array ( " status " => " OK " , " msg " => $values ));
break ;
case 'getLastmonth' :
$values = $this -> iterateForDays ( $pdo , $allDates [ 'monthAndYearOfLastMonth' ], intval ( $allDates [ 'lastDayOfLastMonth' ]), true );
echo json_encode ( array ( " status " => " OK " , " msg " => $values ));
break ;
case 'getProds' :
2020-11-19 23:13:57 +01:00
$days = null ;
if ( isset ( $_GET [ 'days' ])) {
$days = intval ( $_GET [ 'days' ]);
}
$values = $this -> sumSortedByProducts ( $pdo , $allDates [ 'last30days' ][ 0 ], $allDates [ 'currentTimeStr' ], null , null , $days );
2020-11-19 23:12:46 +01:00
echo json_encode ( array ( " status " => " OK " , " msg " => $values ));
break ;
case 'getRatings' :
$values = $this -> getRatings ( $pdo , $allDates [ 'last30days' ], $allDates [ 'lastMonthComplete' ], $allDates [ 'currentTimeStr' ]);
echo json_encode ( array ( " status " => " OK " , " msg " => $values ));
break ;
case 'getMonthNames' :
echo json_encode ( array ( " status " => " OK " , " thismonth " => $allDates [ 'thisMonthName' ], " lastmonth " => $allDates [ 'lastMonthName' ]));
break ;
default :
echo " Command not supported. " ;
2020-11-19 22:44:19 +01:00
}
}
2020-11-19 23:12:46 +01:00
private function getStats ( $pdo = null ) {
if ( is_null ( $pdo )) {
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
}
2020-11-19 23:03:20 +01:00
$this -> createSqlPhrases ();
2020-11-19 22:47:44 +01:00
$alldates = self :: getDates ();
2020-11-19 22:44:19 +01:00
2020-11-19 23:03:20 +01:00
$this -> getReports ( $pdo , $alldates );
2020-11-19 22:44:19 +01:00
}
2020-11-19 23:03:20 +01:00
public function getStatsCore ( $pdo , $forDash = false ) {
$this -> createSqlPhrases ();
2020-11-19 22:47:44 +01:00
$alldates = self :: getDates ();
2020-11-19 23:03:20 +01:00
return ( $this -> getReportsCore ( $pdo , $alldates , $forDash ));
2020-11-19 22:47:44 +01:00
}
static private function getDates () {
2020-11-19 23:00:55 +01:00
date_default_timezone_set ( DbUtils :: getTimeZone ());
2020-11-19 22:44:19 +01:00
$currentTimeStr = date ( 'Y-m-d H:i:s' );
$curTime = strtotime ( $currentTimeStr );
$todayDate = date ( 'Y-m-d' );
$todayHour = date ( 'H' );
$yesterdayDate = date ( " Y-m-d " , strtotime ( " -1 day " , $curTime ));
// now for this month
$firstDayOfThisMonth = date ( " Y-m-01 " , strtotime ( $currentTimeStr ));
2020-11-19 23:00:55 +01:00
$currentDay = date ( " d " , strtotime ( $currentTimeStr )); // current day (4 if date is 4 Jan 2014)
2020-11-19 22:44:19 +01:00
$month = date ( " m " , strtotime ( $currentTimeStr ));
2020-11-19 22:47:44 +01:00
$monthName = self :: getMonthName ( $month );
2020-11-19 22:44:19 +01:00
$monthAndYearOfThisMonth = date ( " Y-m " , strtotime ( $currentTimeStr ));
2020-11-19 22:47:44 +01:00
$thisMonthName = self :: getMonthName ( $month );
2020-11-19 22:44:19 +01:00
// last month
2020-11-19 22:47:44 +01:00
$last_month_ini = new DateTime ( " first day of last month " );
$last_month_end = new DateTime ( " last day of last month " );
$firstDayOfLastMonth = $last_month_ini -> format ( 'Y-m-d' );
2020-11-19 23:00:55 +01:00
$lastDayOfLastMonth = $last_month_end -> format ( 'd' );
$iterations = intval ( $last_month_end -> format ( 'd' ));
2020-11-19 22:47:44 +01:00
$lastMonth = intval ( $last_month_ini -> format ( 'm' ));
$monthAndYearOfLastMonth = $last_month_end -> format ( 'Y-m' );
$lastMonthComplete = $last_month_ini -> format ( 'Y-m-d' ) . " 00:00:00 " ;
$lastMonthName = self :: getMonthName ( $lastMonth );
// last 30 days
$daysArr = array ();
for ( $i = 29 ; $i >= 0 ; $i -- ) {
$daysArr [] = date ( " Y-m-d " , strtotime ( '-' . $i . ' day' ) );
}
2020-11-19 22:44:19 +01:00
$retArray = array (
" todayDate " => $todayDate ,
" todayHour " => $todayHour ,
" currentTimeStr " => $currentTimeStr ,
" yesterdayDate " => $yesterdayDate ,
" monthAndYearOfLastMonth " => $monthAndYearOfLastMonth ,
2020-11-19 22:47:44 +01:00
" lastDayOfLastMonth " => $lastDayOfLastMonth ,
2020-11-19 22:44:19 +01:00
" lastMonthComplete " => $lastMonthComplete ,
" lastMonthName " => $lastMonthName ,
" currentDay " => $currentDay ,
" monthAndYearOfThisMonth " => $monthAndYearOfThisMonth ,
2020-11-19 22:47:44 +01:00
" thisMonthName " => $thisMonthName ,
" last30days " => $daysArr
);
2020-11-19 22:44:19 +01:00
return $retArray ;
}
2020-11-19 23:00:55 +01:00
public static function getMonthName ( $monthNo ) {
$mons = array (
1 => " Januar " ,
2 => " Februar " ,
3 => " März " ,
4 => " April " ,
5 => " Mai " ,
6 => " Juni " ,
7 => " Juli " ,
8 => " August " ,
9 => " September " ,
10 => " Oktober " ,
11 => " November " ,
12 => " Dezember " );
return ( $mons [ intval ( $monthNo )]);
2020-11-19 22:44:19 +01:00
}
2020-11-19 23:03:20 +01:00
private function getReports ( $pdo , $allDates ) {
$reports = $this -> getReportsCore ( $pdo , $allDates );
2020-11-19 22:47:44 +01:00
echo json_encode ( $reports );
}
2020-11-19 23:03:20 +01:00
private function getReportsCore ( $pdo , $allDates , $forDash = false ) {
2020-11-19 22:44:19 +01:00
$pdo -> beginTransaction ();
// bills of today independently of closing
$retArrayToday = $this -> iterateForHours ( $pdo , $allDates [ 'todayDate' ], intval ( $allDates [ 'todayHour' ]) + 1 , false );
// closed yesterday bills:
$retArrayYesterday = $this -> iterateForHours ( $pdo , $allDates [ 'yesterdayDate' ], 24 , true );
$retThisMonth = $this -> iterateForDays ( $pdo , $allDates [ 'monthAndYearOfThisMonth' ], intval ( $allDates [ 'currentDay' ]), true );
// closed of last month:
2020-11-19 22:47:44 +01:00
$retArrayLastMonth = $this -> iterateForDays ( $pdo , $allDates [ 'monthAndYearOfLastMonth' ], intval ( $allDates [ 'lastDayOfLastMonth' ]), true );
2020-11-19 22:44:19 +01:00
// products in the last 30 days:
2020-11-19 23:13:57 +01:00
$retArrayProds = $this -> sumSortedByProducts ( $pdo , $allDates [ 'last30days' ][ 0 ], $allDates [ 'currentTimeStr' ], null , null , null );
2020-11-19 22:47:44 +01:00
$retRatings = $this -> getRatings ( $pdo , $allDates [ 'last30days' ], $allDates [ 'lastMonthComplete' ], $allDates [ 'currentTimeStr' ]);
2020-11-19 22:51:21 +01:00
$usersums = $this -> getUserSums ( $pdo );
2020-11-19 22:44:19 +01:00
$pdo -> commit ();
$retArray = array ( " today " => $retArrayToday ,
" yesterday " => $retArrayYesterday ,
" thismonth " => $retThisMonth ,
" lastmonth " => $retArrayLastMonth ,
" prodsums " => $retArrayProds ,
" lastmonthname " => $allDates [ 'lastMonthName' ],
2020-11-19 22:47:44 +01:00
" thismonthname " => $allDates [ 'thisMonthName' ],
2020-11-19 22:51:21 +01:00
" ratings " => $retRatings ,
" usersums " => $usersums
2020-11-19 22:47:44 +01:00
);
2020-11-19 22:44:19 +01:00
2020-11-19 23:03:20 +01:00
if ( $forDash ) {
$retArray [ " tables " ] = self :: getOpenTables ( $pdo );
$retArray [ " prodscount " ] = self :: getMaxSoldProductsCount ( $pdo );
$retArray [ " prodssum " ] = self :: getMaxSoldProductsSum ( $pdo );
2020-11-19 23:03:23 +01:00
$retArray [ " durations " ] = self :: getGuestDuration ( $pdo );
2020-11-19 23:03:20 +01:00
}
2020-11-19 22:47:44 +01:00
return $retArray ;
2020-11-19 22:44:19 +01:00
}
/*
* returns an array :
* hour , sum
* hour , sum
*/
private function iterateForHours ( $pdo , $theDateStr , $noOfIterations , $mustBeClosed ) {
$retArray = array ();
$sumMax = 0.0 ;
2020-11-19 23:00:55 +01:00
for ( $i = 0 ; $i < $noOfIterations ; $i ++ ) {
$startDateTime = $theDateStr . " $i :00:00 " ;
2020-11-19 22:44:19 +01:00
$endDateTime = $theDateStr . " $i :59:59 " ;
$sum = $this -> sumBetween ( $pdo , $startDateTime , $endDateTime , $mustBeClosed );
2020-11-19 23:00:55 +01:00
if ( $sumMax < $sum ) {
$sumMax = $sum ;
}
$retArray [] = array ( " iter " => $i , " sum " => $sum );
2020-11-19 22:44:19 +01:00
}
return array ( " max " => $sumMax , " content " => $retArray );
}
/*
* returns an array wioth " content "
* day , sum with day 0. . 31 ,
* day , sum ...
*/
private function iterateForDays ( $pdo , $theMonthYearStr , $noOfIterations , $mustBeClosed ) {
$retArray = array ();
$sumMax = 0.0 ;
for ( $i = 1 ; $i < ( $noOfIterations + 1 ); $i ++ ) {
$dayInTwoDigists = sprintf ( '%02d' , $i );
$startDateTime = $theMonthYearStr . " - $dayInTwoDigists 00:00:00 " ;
$endDateTime = $theMonthYearStr . " - $dayInTwoDigists 23:59:59 " ;
$sum = $this -> sumBetween ( $pdo , $startDateTime , $endDateTime , $mustBeClosed );
if ( $sumMax < $sum ) {
$sumMax = $sum ;
}
$retArray [] = array ( " iter " => $i , " sum " => $sum );
}
return array ( " max " => $sumMax , " content " => $retArray );
}
private function sumBetween ( $pdo , $startDateTime , $endDateTime , $mustBeClosed ) {
$sql = " SELECT sum(brutto) as sumtotal FROM %bill% " ;
$sql .= " WHERE status is null " ; // no cash insert or take off, no stornos
$sql .= " AND billdate between ? AND ? " ;
2020-11-19 23:12:48 +01:00
$sql .= " AND paymentid <> '8' " ;
2020-11-19 22:44:19 +01:00
if ( $mustBeClosed ) {
$sql .= " AND closingid is not null " ; // and must be in a closing
}
2020-11-19 23:00:55 +01:00
$sum = 0.0 ;
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
$stmt -> execute ( array ( $startDateTime , $endDateTime ));
2020-11-19 22:44:19 +01:00
$row = $stmt -> fetchObject ();
2020-11-19 23:00:55 +01:00
if ( $row != null ) {
2020-11-19 22:44:19 +01:00
$theSqlSum = $row -> sumtotal ;
if ( $theSqlSum != null ) {
$sum = $theSqlSum ;
}
}
return $sum ;
}
2020-11-19 23:00:55 +01:00
function cmp ( $a , $b )
{
$asum = $a [ 'sum' ];
$bsum = $b [ 'sum' ];
if ( $asum == $bsum ) {
return 0 ;
}
return ( $asum < $bsum ) ? 1 : - 1 ;
2020-11-19 22:44:19 +01:00
}
2020-11-19 23:13:57 +01:00
public function sumSortedByProducts ( $pdo , $startDateTime , $endDateTime , $closidstart = null , $closidend = null , $days = null ) {
2020-11-19 22:44:19 +01:00
// first get all products and with their id and name
2020-11-19 23:13:57 +01:00
if ( ! is_null ( $days )) {
date_default_timezone_set ( DbUtils :: getTimeZone ());
$startDateTime = date ( " Y-m-d " , strtotime ( '-' . ( $days - 1 ) . ' day' ) );
}
2020-11-19 23:12:32 +01:00
if ( is_null ( $closidstart )) {
$sql = " SELECT DISTINCT productid from %queue%,%bill%,%products% " ;
$sql .= " WHERE %queue%.productid=%products%.id " ;
$sql .= " AND billid is not null AND %queue%.billid=%bill%.id " ;
$sql .= " AND billdate between ? AND ? " ;
$sql .= " AND %bill%.closingid is not null " ;
2020-11-19 23:12:48 +01:00
$sql .= " AND %bill%.status is null " ;
$sql .= " AND %bill%.paymentid <> '8' " ;
2020-11-19 23:12:32 +01:00
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $startDateTime , $endDateTime ));
} else {
$sql = " SELECT DISTINCT productid from %queue% Q,%bill% B,%products% P " ;
$sql .= " WHERE Q.productid=P.id " ;
$sql .= " AND billid is not null AND Q.billid=B.id " ;
$sql .= " AND B.closingid is not null " ;
$sql .= " AND B.closingid between ? AND ? " ;
2020-11-19 23:12:48 +01:00
$sql .= " AND B.status is null " ;
$sql .= " AND B.paymentid <> '8' " ;
2020-11-19 23:12:32 +01:00
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $closidstart , $closidend ));
}
2020-11-19 23:00:55 +01:00
$prods = array ();
2020-11-19 22:58:23 +01:00
$sql = " SELECT longname FROM %products% WHERE id=? " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
foreach ( $result as $aProd ) {
$stmt -> execute ( array ( $aProd [ " productid " ]));
$row = $stmt -> fetchObject ();
$prods [] = array ( " prodid " => $aProd [ 'productid' ], " prodname " => $row -> longname );
2020-11-19 22:44:19 +01:00
}
// now iterate over all prods
$sumMax = 0.0 ;
$prodinfos = array ();
foreach ( $prods as $aProd ) {
$aProdId = $aProd [ 'prodid' ];
2020-11-19 23:12:32 +01:00
if ( is_null ( $closidstart )) {
$sql = " SELECT sum(price) as sumprice, count(%queue%.id) as prodcount from %queue%,%bill%,%products% " ;
$sql .= " WHERE %queue%.productid=%products%.id " ;
$sql .= " AND billid is not null AND %queue%.billid=%bill%.id " ;
$sql .= " AND billdate between ? AND ? " ;
$sql .= " AND %bill%.closingid is not null " ;
$sql .= " AND productid=? " ;
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
$stmt -> execute ( array ( $startDateTime , $endDateTime , $aProdId ));
$row = $stmt -> fetchObject ();
} else {
$sql = " SELECT sum(price) as sumprice, count(%queue%.id) as prodcount from %queue%,%bill%,%products% " ;
$sql .= " WHERE %queue%.productid=%products%.id " ;
$sql .= " AND billid is not null AND %queue%.billid=%bill%.id " ;
$sql .= " AND %bill%.closingid is not null " ;
$sql .= " AND %bill%.closingid between ? AND ? " ;
$sql .= " AND productid=? " ;
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
$stmt -> execute ( array ( $closidstart , $closidend , $aProdId ));
$row = $stmt -> fetchObject ();
}
2020-11-19 23:00:55 +01:00
2020-11-19 22:44:19 +01:00
if ( $row != null ) {
$sumprice = $row -> sumprice ;
2020-11-19 23:00:55 +01:00
if ( $sumMax < $sumprice ) {
$sumMax = $sumprice ;
2020-11-19 22:44:19 +01:00
}
if ( $sumprice != null ) {
2020-11-19 23:00:55 +01:00
$prodinfo = $aProd [ 'prodname' ] . " ( " . $row -> prodcount . " x) " ;
$prodinfos [] = array ( " prodid " => $aProdId , " iter " => $prodinfo , " sum " => $sumprice );
2020-11-19 22:44:19 +01:00
}
}
}
uasort ( $prodinfos , array ( $this , 'cmp' ));
2020-11-19 23:00:55 +01:00
// due to a bug somehow the order is not kept when transformed to json - copy...
2020-11-19 22:44:19 +01:00
$prodInfoSorted = array ();
foreach ( $prodinfos as $prodinfo ) {
$prodInfoSorted [] = array ( " iter " => $prodinfo [ 'iter' ], " sum " => $prodinfo [ 'sum' ]);
}
return array ( " max " => $sumMax , " content " => $prodInfoSorted );
2020-11-19 22:47:44 +01:00
}
static function getRating ( $pdo , $sql , $start , $end ) {
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $start , $end ));
$row = $stmt -> fetchObject ();
if ( ! is_null ( $row )) {
return ( intval ( $row -> count ));
} else {
return 0 ;
}
}
static function getRelation ( $val1 , $val2 , $val3 , $maxPercent ) {
$total = $val1 + $val2 + $val3 ;
if ( $total == 0 ) {
return array ( 0.0 , 0.0 , 0.0 , $maxPercent );
} else {
$rel = $maxPercent / (( double ) $total );
return array ( $rel * $val1 , $rel * $val2 , $rel * $val3 , 0.0 );
}
}
static function getRatingOfDay ( $pdo , $aDay , $start , $end ) {
$serviceGood = self :: getRating ( $pdo , self :: $sql_service_good , $start , $end );
$serviceOK = self :: getRating ( $pdo , self :: $sql_service_ok , $start , $end );
$serviceBad = self :: getRating ( $pdo , self :: $sql_service_bad , $start , $end );
$serviceRel = self :: getRelation ( $serviceGood , $serviceOK , $serviceBad , 95 );
$kitchenGood = self :: getRating ( $pdo , self :: $sql_kitchen_good , $start , $end );
$kitchenOK = self :: getRating ( $pdo , self :: $sql_kitchen_ok , $start , $end );
$kitchenBad = self :: getRating ( $pdo , self :: $sql_kitchen_bad , $start , $end );
$kitchenRel = self :: getRelation ( $kitchenGood , $kitchenOK , $kitchenBad , 95 );
$totalRatings = self :: getRating ( $pdo , self :: $sql_ratings , $start , $end );
$date = new DateTime ( $aDay );
return array ( " day " => $date -> format ( 'd.m.Y' ), " service " => $serviceRel , " kitchen " => $kitchenRel , " total " => $totalRatings );
}
function getRatings ( $pdo , $last30days , $startPeriod , $endPeriod ){
$reports = array ();
foreach ( $last30days as $aDay ) {
$start = $aDay . " 00:00:00 " ;
$end = $aDay . " 23:59:59 " ;
$reports [] = self :: getRatingOfDay ( $pdo , $aDay , $start , $end );
}
$sql = self :: $sql_remarks ;
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
$stmt -> execute ( array ( $startPeriod , $endPeriod ));
$result = $stmt -> fetchAll ();
return array ( " statistics " => $reports , " remarks " => $result );
}
2020-11-19 22:51:21 +01:00
function getUserSums ( $pdo ) {
$sql = " SELECT userid,username as iter, " ;
$sql .= " ROUND(sum(brutto),2) as sum, " ;
$sql .= " ROUND(sum(if(paymentid='1',brutto,'0.00')),2) as sumonlybar, " ;
$sql .= " ROUND(sum(if(status = 'c',brutto,'0.00')),2) as sumcash " ;
2020-11-19 23:03:26 +01:00
$sql .= " FROM %bill%,%user% WHERE userid=%user%.id AND closingid is null GROUP BY userid,username " ;
2020-11-19 22:51:21 +01:00
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ();
$result = $stmt -> fetchAll ();
$sumMax = 0.0 ;
foreach ( $result as $a ) {
if ( $a [ " sum " ] > $sumMax ) {
$sumMax = $a [ " sum " ];
}
}
return array ( " max " => $sumMax , " content " => $result );
}
2020-11-19 23:03:20 +01:00
public static function getOpenTables ( $pdo ) {
$sql = " SELECT id,roomname FROM %room% WHERE removed is null " ;
$rooms = CommonUtils :: fetchSqlAll ( $pdo , $sql , null );
$tableCountTotal = 0 ;
$tableCountOpen = 0 ;
$sum = 0.0 ;
foreach ( $rooms as $aRoom ) {
$roomId = $aRoom [ " id " ];
$sql = " SELECT count(id) as countid FROM %resttables% WHERE %resttables%.roomid=? " ;
$howManyTables = CommonUtils :: getRowSqlObject ( $pdo , $sql , array ( $roomId ));
$tableCountTotal += $howManyTables -> countid ;
2020-11-19 23:14:48 +01:00
$sql = " SELECT %resttables%.id as id,%resttables%.tableno as name,COALESCE(SUM(IF(%queue%.toremove='0' AND %queue%.paidtime is null AND %queue%.isclosed is null,%queue%.price,0.00)),0.00) as pricesum FROM %resttables% " ;
2020-11-19 23:03:20 +01:00
$sql .= " LEFT OUTER JOIN %queue% ON %queue%.tablenr=%resttables%.id WHERE %resttables%.removed is null AND " ;
2020-11-19 23:03:26 +01:00
$sql .= " %resttables%.roomid=? GROUP BY %resttables%.id,name " ;
2020-11-19 23:03:20 +01:00
$tables = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $roomId ));
foreach ( $tables as $aTable ) {
$sum += $aTable [ " pricesum " ];
if ( $aTable [ " pricesum " ] != '0.00' ) {
$tableCountOpen ++ ;
}
}
}
return array ( " tablestotal " => $tableCountTotal , " opentables " => $tableCountOpen , " sum " => $sum );
}
public static function getMaxSoldProductsCount ( $pdo ) {
$sql = " SELECT longname,productid,count(productid) as value from %queue%,%bill%,%products% " ;
$sql .= " WHERE %queue%.productid=%products%.id " ;
$sql .= " AND productid=%products%.id " ;
$sql .= " AND billid is not null AND %queue%.billid=%bill%.id " ;
$sql .= " AND DATE(billdate) = CURDATE() " ;
$sql .= " AND %bill%.status is null " ;
2020-11-19 23:12:48 +01:00
$sql .= " AND %bill%.paymentid <> '8' " ;
2020-11-19 23:03:20 +01:00
$sql .= " GROUP BY longname,productid " ;
$sql .= " ORDER BY value DESC " ;
$sql .= " LIMIT 10 " ;
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , null );
return $result ;
}
public static function getMaxSoldProductsSum ( $pdo ) {
$sql = " SELECT longname,productid,sum(price) as value from %queue%,%bill%,%products% " ;
$sql .= " WHERE %queue%.productid=%products%.id " ;
$sql .= " AND productid=%products%.id " ;
$sql .= " AND billid is not null AND %queue%.billid=%bill%.id " ;
$sql .= " AND DATE(billdate) = CURDATE() " ;
$sql .= " AND %bill%.status is null " ;
2020-11-19 23:12:48 +01:00
$sql .= " AND %bill%.paymentid <> '8' " ;
2020-11-19 23:03:20 +01:00
$sql .= " GROUP BY longname,productid " ;
$sql .= " ORDER BY value DESC " ;
$sql .= " LIMIT 10 " ;
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , null );
return $result ;
}
2020-11-19 23:03:23 +01:00
public static function getGuestDuration ( $pdo ) {
date_default_timezone_set ( DbUtils :: getTimeZone ());
$currentHour = date ( 'H' );
$stat = array ();
2020-11-19 23:03:26 +01:00
$sql = " SELECT HOUR(paidtime) as hour,ROUND(AVG(TIME_TO_SEC(TIMEDIFF(paidtime,ordertime))/60)) as average " ;
2020-11-19 23:12:27 +01:00
$sql .= " FROM %queue% WHERE paidtime is not null AND %queue%.toremove='0' AND DATE(paidtime) = DATE(NOW()) AND HOUR(paidtime)=? GROUP BY hour " ;
2020-11-19 23:03:23 +01:00
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
for ( $hour = 0 ; $hour <= $currentHour ; $hour ++ ) {
$stmt -> execute ( array ( $hour ));
$result = $stmt -> fetchAll ();
if ( count ( $result ) > 0 ) {
$stat [] = array ( " hour " => $hour , " average " => $result [ 0 ][ " average " ]);
} else {
$stat [] = array ( " hour " => $hour , " average " => 0 );
}
}
return $stat ;
}
2020-11-19 22:44:19 +01:00
}