272 lines
8.5 KiB
PHP
272 lines
8.5 KiB
PHP
|
<?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;
|
||
|
|
||
|
function __construct() {
|
||
|
$this->dbutils = new DbUtils();
|
||
|
$this->queue = new QueueContent();
|
||
|
$this->commonUtils = new CommonUtils();
|
||
|
$this->userrights = new Userrights();
|
||
|
}
|
||
|
|
||
|
function handleCommand($command) {
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if($command == 'getStats') {
|
||
|
$this->getStats();
|
||
|
} else {
|
||
|
echo "Command not supported.";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private function getStats() {
|
||
|
$alldates = $this->getDates();
|
||
|
|
||
|
$this->getReports($alldates);
|
||
|
}
|
||
|
|
||
|
private function getDates() {
|
||
|
date_default_timezone_set('Europe/Berlin');
|
||
|
$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));
|
||
|
$currentDay = date("d",strtotime($currentTimeStr)); // current day (4 if date is 4 Jan 2014)
|
||
|
$month = date("m",strtotime($currentTimeStr));
|
||
|
$monthName = $this->getMonthName($month);
|
||
|
$monthAndYearOfThisMonth = date("Y-m",strtotime($currentTimeStr));
|
||
|
$thisMonthName = $this->getMonthName($month);
|
||
|
|
||
|
// last month
|
||
|
$firstDayOfLastMonth = date("Y-m-01", strtotime('-1 month') );
|
||
|
$lastDayOfLathMonth = date("t", strtotime('-1 month') );
|
||
|
$iterations = date("t", strtotime('-1 month') );
|
||
|
$lastMonth = date("m",strtotime('-1 month') );
|
||
|
$monthAndYearOfLastMonth = date("Y-m", strtotime('-1 month'));
|
||
|
$lastMonthComplete = date("Y-m-d",strtotime('-1 month') ) . " 00:00:00";
|
||
|
$lastMonthName = $this->getMonthName($lastMonth);
|
||
|
|
||
|
$retArray = array(
|
||
|
"todayDate" => $todayDate,
|
||
|
"todayHour" => $todayHour,
|
||
|
"currentTimeStr" => $currentTimeStr,
|
||
|
"yesterdayDate" => $yesterdayDate,
|
||
|
"monthAndYearOfLastMonth" => $monthAndYearOfLastMonth,
|
||
|
"lastDayOfLathMonth" => $lastDayOfLathMonth,
|
||
|
"lastMonthComplete" => $lastMonthComplete,
|
||
|
"lastMonthName" => $lastMonthName,
|
||
|
"currentDay" => $currentDay,
|
||
|
"monthAndYearOfThisMonth" => $monthAndYearOfThisMonth,
|
||
|
"thisMonthName" => $thisMonthName);
|
||
|
return $retArray;
|
||
|
}
|
||
|
|
||
|
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)]);
|
||
|
}
|
||
|
|
||
|
|
||
|
private function getReports ($allDates) {
|
||
|
$pdo = $this->dbutils->openDbAndReturnPdo();
|
||
|
$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:
|
||
|
$retArrayLastMonth = $this->iterateForDays($pdo, $allDates['monthAndYearOfLastMonth'],intval($allDates['lastDayOfLathMonth']),true);
|
||
|
|
||
|
// products in the last 30 days:
|
||
|
$retArrayProds = $this->sumSortedByProducts($pdo, $allDates['lastMonthComplete'], $allDates['currentTimeStr']);
|
||
|
$pdo->commit();
|
||
|
|
||
|
$retArray = array("today" => $retArrayToday,
|
||
|
"yesterday" => $retArrayYesterday,
|
||
|
"thismonth" => $retThisMonth,
|
||
|
"lastmonth" => $retArrayLastMonth,
|
||
|
"prodsums" => $retArrayProds,
|
||
|
"lastmonthname" => $allDates['lastMonthName'],
|
||
|
"thismonthname" => $allDates['thisMonthName']);
|
||
|
|
||
|
echo json_encode($retArray);
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* returns an array:
|
||
|
* hour, sum
|
||
|
* hour, sum
|
||
|
*/
|
||
|
private function iterateForHours($pdo,$theDateStr,$noOfIterations,$mustBeClosed) {
|
||
|
$retArray = array();
|
||
|
$sumMax = 0.0;
|
||
|
for ($i=0;$i<$noOfIterations;$i++) {
|
||
|
$startDateTime = $theDateStr . " $i:00:00";
|
||
|
$endDateTime = $theDateStr . " $i: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);
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* 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 ? ";
|
||
|
if ($mustBeClosed) {
|
||
|
$sql .= "AND closingid is not null"; // and must be in a closing
|
||
|
}
|
||
|
$sum = 0.0;
|
||
|
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
|
||
|
$stmt->execute(array($startDateTime,$endDateTime));
|
||
|
$row =$stmt->fetchObject();
|
||
|
|
||
|
if ($row != null) {
|
||
|
$theSqlSum = $row->sumtotal;
|
||
|
if ($theSqlSum != null) {
|
||
|
$sum = $theSqlSum;
|
||
|
}
|
||
|
}
|
||
|
return $sum;
|
||
|
}
|
||
|
|
||
|
function cmp($a, $b)
|
||
|
{
|
||
|
$asum = $a['sum'];
|
||
|
$bsum = $b['sum'];
|
||
|
if ($asum == $bsum) {
|
||
|
return 0;
|
||
|
}
|
||
|
return ($asum < $bsum) ? 1 : -1;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* returns a sorted by prices list:
|
||
|
* array("prodid" => $aProdId,"prodname" => $aProd['prodname'], "sum" => $sumprice);
|
||
|
* array("prodid" => $aProdId,"prodname" => $aProd['prodname'], "sum" => $sumprice);
|
||
|
* (...)
|
||
|
*/
|
||
|
private function sumSortedByProducts($pdo,$startDateTime,$endDateTime) {
|
||
|
// first get all products and with their id and name
|
||
|
|
||
|
$sql = "SELECT DISTINCT productid,productname 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";
|
||
|
|
||
|
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
|
||
|
$stmt->execute(array($startDateTime,$endDateTime));
|
||
|
$result = $stmt->fetchAll();
|
||
|
$prods = array();
|
||
|
|
||
|
foreach($result as $row) {
|
||
|
$prods[] = array("prodid" => $row['productid'],"prodname" => $row['productname']);
|
||
|
}
|
||
|
|
||
|
// now iterate over all prods
|
||
|
$sumMax = 0.0;
|
||
|
$prodinfos = array();
|
||
|
foreach ($prods as $aProd) {
|
||
|
$aProdId = $aProd['prodid'];
|
||
|
|
||
|
$sql = "SELECT sum(price) as sumprice 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();
|
||
|
|
||
|
if ($row != null) {
|
||
|
$sumprice = $row->sumprice;
|
||
|
if ($sumMax < $sumprice) {
|
||
|
$sumMax = $sumprice;
|
||
|
}
|
||
|
if ($sumprice != null) {
|
||
|
$prodinfos[] = array("prodid" => $aProdId,"iter" => $aProd['prodname'], "sum" => $sumprice);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
uasort($prodinfos, array($this,'cmp'));
|
||
|
|
||
|
// due to a bug somehow the order is not kept when transformed to json - copy...
|
||
|
$prodInfoSorted = array();
|
||
|
foreach($prodinfos as $prodinfo) {
|
||
|
$prodInfoSorted[] = array("iter" => $prodinfo['iter'],"sum" => $prodinfo['sum']);
|
||
|
}
|
||
|
|
||
|
return array("max" => $sumMax, "content" => $prodInfoSorted);
|
||
|
}
|
||
|
}
|
||
|
?>
|