2020-11-19 22:47:44 +01:00
< ? php
// Datenbank-Verbindungsparameter
2020-11-19 22:59:47 +01:00
require_once ( 'dbutils.php' );
2020-11-19 22:47:44 +01:00
require_once ( 'commonutils.php' );
require_once ( 'globals.php' );
require_once ( 'admin.php' );
2020-11-19 23:02:12 +01:00
require_once ( 'customers.php' );
2020-11-19 22:47:44 +01:00
require_once ( 'utilities/Emailer.php' );
class Closing {
var $dbutils ;
var $t ;
function __construct () {
$this -> dbutils = new DbUtils ();
require_once 'translations.php' ;
}
function handleCommand ( $command ) {
2020-11-19 22:59:47 +01:00
// all commands require closing,manager or admin rights
2020-11-19 23:03:29 +01:00
if ( ! ( $this -> hasCurrentUserManagerOrAdminRights ()) && ( $command != 'createClosing' ) && ( $command != 'getClosings' ) && ( $command != 'remotecreateclosing' )) {
2020-11-19 22:47:44 +01:00
if ( $command != 'exportCsv' ) {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_MANAGER_NOT_AUTHOTRIZED , " msg " => ERROR_MANAGER_NOT_AUTHOTRIZED_MSG ));
} else {
// exception - result is not handled on HTML/JS side
2020-11-19 23:12:59 +01:00
echo json_encode ( array ( " status " => " ERROR " , " msg " => " Fehlende Benutzerrechte " ));
2020-11-19 22:47:44 +01:00
}
return ;
}
// user has manager rights
if ( $command == 'createClosing' ) {
2020-11-19 23:14:48 +01:00
$this -> createClosing ( $_POST [ 'remark' ], $_POST [ 'print' ], $_POST [ 'counting' ], $_POST [ 'coinscount' ], $_POST [ 'notescount' ], $_POST [ 'counted' ]);
2020-11-19 23:03:29 +01:00
} else if ( $command == 'remotecreateclosing' ) {
if ( isset ( $_POST [ 'remoteaccesscode' ])) {
if ( isset ( $_POST [ 'remark' ])) {
$this -> remotecreateclosing ( $_POST [ 'remoteaccesscode' ], $_POST [ 'remark' ]);
} else {
$this -> remotecreateclosing ( $_POST [ 'remoteaccesscode' ], '' );
}
} else {
echo json_encode ( " Remote access code not given " );
}
return ;
2020-11-19 22:47:44 +01:00
} else if ( $command == 'getClosings' ) {
$this -> getClosings ( $_GET [ 'month' ], $_GET [ 'year' ]);
2020-11-19 22:59:47 +01:00
} else if ( $command == 'exportCsv' ) {
2020-11-19 22:47:44 +01:00
$this -> exportCsv ( $_GET [ 'closingid' ]);
2020-11-19 23:02:12 +01:00
} else if ( $command == 'exportGuestCsv' ) {
$this -> exportGuestCsv ( $_GET [ 'closingid' ]);
2020-11-19 22:47:44 +01:00
} else if ( $command == 'emailCsv' ) {
$this -> emailCsv ( $_GET [ 'closingid' ], $_GET [ 'emailaddress' ], $_GET [ 'topic' ]);
} else if ( $command == 'getClosing' ) {
$this -> getClosing ( $_GET [ 'closingid' ]);
} else if ( $command == 'getClosingSummary' ) {
$this -> getClosingSummary ( $_GET [ 'closingid' ], null , true );
2020-11-19 23:11:49 +01:00
} else if ( $command == 'htmlreport' ) {
$this -> htmlreport ( $_GET [ " closid " ]);
2020-11-19 23:12:32 +01:00
} else if ( $command == 'getClosingsListOfMonthYear' ) {
$this -> getClosingsListOfMonthYear ( $_GET [ " month " ], $_GET [ " year " ]);
2020-11-19 23:12:59 +01:00
} else if ( $command == 'getFirstAndLastClosing' ) {
self :: getFirstAndLastClosing ();
2020-11-19 23:14:51 +01:00
} else if ( $command == 'getCashSumsForNextClosing' ) {
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
self :: getCashSumsForNextClosing ( $pdo );
2020-11-19 22:47:44 +01:00
} else {
echo " Command not supported. " ;
}
}
2020-11-19 23:02:57 +01:00
private function hasCurrentUserManagerOrAdminRights () {
2020-11-19 22:59:47 +01:00
session_start ();
if ( ! isset ( $_SESSION [ 'angemeldet' ]) || ! $_SESSION [ 'angemeldet' ]) {
// no user logged in
return false ;
} else {
2020-11-19 23:02:57 +01:00
return ( $_SESSION [ 'right_manager' ] || $_SESSION [ 'is_admin' ]);
2020-11-19 22:59:47 +01:00
}
2020-11-19 22:47:44 +01:00
}
2020-11-19 22:59:47 +01:00
private function getDecPoint () {
$sql = " SELECT name,setting FROM %config% WHERE name=? " ;
$pdo = $this -> dbutils -> openDbAndReturnPdo ();
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
$stmt -> execute ( array ( " decpoint " ));
$row = $stmt -> fetchObject ();
return ( $row -> setting );
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:02:49 +01:00
private function saveLastClosingCreation ( $pdo ) {
date_default_timezone_set ( DbUtils :: getTimeZone ());
$date = new DateTime ();
$unixTimeStamp = $date -> getTimestamp ();
$sql = " SELECT count(id) as countid FROM %work% WHERE item=? " ;
$row = CommonUtils :: getRowSqlObject ( $pdo , $sql , array ( 'lastclosing' ));
if ( $row -> countid == 0 ) {
$sql = " INSERT INTO %work% (item,value,signature) VALUES(?,?,?) " ;
CommonUtils :: execSql ( $pdo , $sql , array ( 'lastclosing' , $unixTimeStamp , null ));
} else {
$sql = " UPDATE %work% SET value=? WHERE item=? " ;
CommonUtils :: execSql ( $pdo , $sql , array ( $unixTimeStamp , 'lastclosing' ));
}
}
private function isClosingAllowed ( $pdo ) {
$TIMEOUT = 120 ;
$sql = " SELECT count(id) as countid FROM %work% WHERE item=? " ;
$row = CommonUtils :: getRowSqlObject ( $pdo , $sql , array ( 'lastclosing' ));
if ( $row -> countid == 0 ) {
return true ;
} else {
$sql = " SELECT value FROM %work% WHERE item=? " ;
$row = CommonUtils :: getRowSqlObject ( $pdo , $sql , array ( 'lastclosing' ));
$lastaccess = $row -> value ;
date_default_timezone_set ( DbUtils :: getTimeZone ());
$date = new DateTime ();
$currentTimeStamp = $date -> getTimestamp ();
if (( $currentTimeStamp - $lastaccess ) > $TIMEOUT ) {
return true ;
} else {
return false ;
}
}
}
2020-11-19 23:03:29 +01:00
private function remotecreateclosing ( $remoteaccesscode , $remark ) {
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
$code = CommonUtils :: getConfigValue ( $pdo , 'remoteaccesscode' , null );
if ( is_null ( $code ) || ( $code == '' )) {
echo json_encode ( " Remote access code was not configured! " );
} else {
if ( md5 ( $remoteaccesscode ) == $code ) {
2020-11-19 23:14:48 +01:00
echo json_encode ( $this -> createClosing ( $remark , 0 , 0 , 0 , 0 , 0.0 ));
2020-11-19 23:03:29 +01:00
} else {
echo json_encode ( " Remote access code not correct! " );
}
}
}
2020-11-19 23:14:48 +01:00
private function createClosing ( $remark , $doPrint , $counting , $coinscount , $notescount , $counted ) {
2020-11-19 23:12:46 +01:00
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
date_default_timezone_set ( DbUtils :: getTimeZone ());
if ( ! $this -> isClosingAllowed ( $pdo )) {
2020-11-19 23:14:48 +01:00
if ( ! CommonUtils :: startsWith ( $remark , " Test " )) {
// for auto-tests do not require that time in between
2020-11-19 23:13:59 +01:00
echo json_encode ( array ( " status " => " ERROR " , " msg " => " Time between closings too short " , " code " => ERROR_CLOSING_TIME_LIMIT ));
return ;
2020-11-19 23:12:46 +01:00
}
2020-11-19 23:14:48 +01:00
}
2020-11-19 23:12:46 +01:00
$closingTime = date ( 'Y-m-d H:i:s' );
2020-11-19 23:14:48 +01:00
$result = $this -> createClosingCore ( $pdo , $remark , $doPrint , $closingTime , true , $counting , $coinscount , $notescount , $counted );
2020-11-19 23:12:46 +01:00
echo json_encode ( $result );
}
2020-11-19 23:14:48 +01:00
private static function insertCounting ( $pdo , $clsid , $coinscount , $notescount ) {
self :: insertCountingOfType ( $pdo , $clsid , $coinscount , 1 );
self :: insertCountingOfType ( $pdo , $clsid , $notescount , 0 );
}
private static function insertCountingOfType ( $pdo , $clsid , $counts , $type ) {
$sql = " INSERT INTO %counting% (clsid,value,count,iscoin) VALUES(?,?,?,?) " ;
foreach ( $counts as $c ) {
$value = $c [ " value " ];
$count = $c [ " count " ];
CommonUtils :: execSql ( $pdo , $sql , array ( $clsid , $value , $count , $type ));
}
}
public function createClosingCore ( $pdo , $remark , $doPrint , $closingTime , $checkForNewVersionAvailable , $counting , $coinscount , $notescount , $counted ) {
2020-11-19 23:10:06 +01:00
set_time_limit ( 60 * 60 );
2020-11-19 22:47:44 +01:00
if ( is_null ( $remark )) {
$remark = " " ;
}
$decpoint = $this -> getDecPoint ();
// first create a closing entry
2020-11-19 22:59:47 +01:00
date_default_timezone_set ( DbUtils :: getTimeZone ());
2020-11-19 23:02:49 +01:00
2020-11-19 22:47:44 +01:00
$pdo -> beginTransaction ();
2020-11-19 22:59:47 +01:00
2020-11-19 23:02:49 +01:00
$this -> saveLastClosingCreation ( $pdo );
2020-11-19 23:00:05 +01:00
if ( CommonUtils :: callPlugin ( $pdo , " createClosing " , " replace " )) {
return ;
}
CommonUtils :: callPlugin ( $pdo , " createClosing " , " before " );
2020-11-19 23:10:26 +01:00
CommonUtils :: execSql ( $pdo , 'DELETE FROM %recordsqueue%' , null );
CommonUtils :: execSql ( $pdo , 'DELETE FROM %records%' , null );
2020-11-19 23:12:32 +01:00
$sql = " SELECT MAX(id) as maxid FROM %closing% " ;
$maxIdRes = CommonUtils :: fetchSqlAll ( $pdo , $sql );
$maxId = 0 ;
if ( ! is_null ( $maxIdRes )) {
2020-11-19 23:14:48 +01:00
$prevClsId = $maxIdRes [ 0 ][ " maxid " ];
if ( ! is_null ( $prevClsId )) {
$maxId = intval ( $prevClsId );
2020-11-19 23:12:32 +01:00
}
}
2020-11-19 23:14:48 +01:00
$newClosingId = $prevClsId + 1 ;
$dsfinvkversion = CommonUtils :: getConfigValue ( $pdo , 'dsfinvkversion' , null );
$dsfinvk_name = CommonUtils :: getConfigValue ( $pdo , 'dsfinvk_name' , null );
$dsfinvk_street = CommonUtils :: getConfigValue ( $pdo , 'dsfinvk_street' , null );
$dsfinvk_postalcode = CommonUtils :: getConfigValue ( $pdo , 'dsfinvk_postalcode' , null );
$dsfinvk_city = CommonUtils :: getConfigValue ( $pdo , 'dsfinvk_city' , null );
$dsfinvk_country = CommonUtils :: getConfigValue ( $pdo , 'dsfinvk_country' , null );
$dsfinvk_stnr = CommonUtils :: getConfigValue ( $pdo , 'dsfinvk_stnr' , null );
$dsfinvk_ustid = CommonUtils :: getConfigValue ( $pdo , 'dsfinvk_ustid' , null );
$version = CommonUtils :: getConfigValue ( $pdo , 'version' , '' );
$taxset1 = CommonUtils :: getConfigValue ( $pdo , 'tax' , 19.00 );
$taxset2 = CommonUtils :: getConfigValue ( $pdo , 'togotax' , 7.00 );
2020-11-19 23:12:32 +01:00
2020-11-19 23:14:48 +01:00
$terminalInfo = Terminals :: getTerminalInfo ();
$terminalEntryId = Terminals :: createOrReferenceTerminalDbEntry ( $pdo , $terminalInfo );
$dsfinvkCols = " `dsfinvkversion`,`dsfinvk_name`,`dsfinvk_street`,`dsfinvk_postalcode`,`dsfinvk_city`,`dsfinvk_country`,`dsfinvk_stnr`,`dsfinvk_ustid`,`terminalid` " ;
$dsfinvkQuests = " ?,?,?,?,?,?,?,?,? " ;
$closingEntrySql = " INSERT INTO `%closing%` (`id`,`closingdate`,`remark`,`billcount`,`billsum`,`signature`,`counting`,`counted`, $dsfinvkCols ,`version`,`taxset1`,`taxset2`) VALUES (?,?,?,?,?,?,?,?, $dsfinvkQuests ,?,?,?) " ;
CommonUtils :: execSql ( $pdo , $closingEntrySql , array ( $newClosingId , $closingTime , $remark , 0 , 0.0 , null , $counting , $counted ,
$dsfinvkversion , $dsfinvk_name , $dsfinvk_street , $dsfinvk_postalcode , $dsfinvk_city , $dsfinvk_country , $dsfinvk_stnr , $dsfinvk_ustid , $terminalEntryId , $version , $taxset1 , $taxset2 ));
if ( $counting == 1 ) {
self :: insertCounting ( $pdo , $newClosingId , $coinscount , $notescount );
}
2020-11-19 22:47:44 +01:00
2020-11-19 23:13:57 +01:00
set_time_limit ( 60 * 60 );
2020-11-19 22:47:44 +01:00
$sql = " SELECT id FROM %bill% WHERE closingid is null AND (tableid >= '0' OR status='c') " ;
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
$stmt -> execute ();
2020-11-19 22:59:47 +01:00
$result = $stmt -> fetchAll ();
$utils = new CommonUtils ();
2020-11-19 22:47:44 +01:00
2020-11-19 22:59:47 +01:00
$ok = true ;
foreach ( $result as $row ) {
2020-11-19 22:47:44 +01:00
$aBillId = $row [ 'id' ];
if ( ! $utils -> verifyBill ( $pdo , $aBillId )) {
$ok = false ;
break ;
2020-11-19 22:59:47 +01:00
}
}
2020-11-19 22:47:44 +01:00
if ( ! $ok ) {
2020-11-19 23:12:46 +01:00
return ( array ( " status " => " ERROR " , " code " => ERROR_INCONSISTENT_DB , " msg " => ERROR_INCONSISTENT_DB_MSG ));
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:13:57 +01:00
set_time_limit ( 60 * 60 );
2020-11-19 23:12:48 +01:00
$sql = " UPDATE %bill% SET closingid=' $newClosingId ' WHERE closingid is null AND (tableid >= '0' OR status='c') AND (paymentid <> ?) " ;
CommonUtils :: execSql ( $pdo , $sql , array ( 8 ));
2020-11-19 22:47:44 +01:00
2020-11-19 23:13:57 +01:00
set_time_limit ( 60 * 60 );
2020-11-19 23:12:48 +01:00
$sql = " SELECT count(id) as billstotake FROM %bill% WHERE closingid=? AND (tableid >= '0' OR status='c') " ;
2020-11-19 22:59:47 +01:00
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
2020-11-19 22:47:44 +01:00
$stmt -> execute ( array ( $newClosingId ));
2020-11-19 22:59:47 +01:00
$row = $stmt -> fetchObject ();
2020-11-19 22:47:44 +01:00
$billsToTake = $row -> billstotake ;
$pricesum = null ;
// now calculate the sum of the prices of this closing
if ( $billsToTake > 0 ) {
2020-11-19 22:59:47 +01:00
$sql = " SELECT sum(brutto) as pricesum FROM %bill% WHERE closingid=? AND (tableid >= '0' OR status='c') " ;
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
$stmt -> execute ( array ( $newClosingId ));
2020-11-19 22:47:44 +01:00
$row = $stmt -> fetchObject ();
$pricesum = $row -> pricesum ;
}
if ( is_null ( $pricesum )) {
$pricesum = 0 ;
}
$prevClosingDate = self :: getDateOfPreviousClosing ( $pdo , $newClosingId );
if ( is_null ( $prevClosingDate )) {
$prevClosingDate = " " ;
}
$pricesumstr = number_format ( $pricesum , 2 , " . " , '' );
$data = " I( $newClosingId )-S( $prevClosingDate )-E( $closingTime )-D( $billsToTake )-S( $pricesumstr ) " ;
2020-11-19 23:12:07 +01:00
$signature = md5 ( $data );
2020-11-19 22:47:44 +01:00
2020-11-19 23:13:57 +01:00
set_time_limit ( 60 * 60 );
2020-11-19 22:47:44 +01:00
$sql = " UPDATE %closing% SET billcount=?, billsum=?,signature=? WHERE id=? " ;
2020-11-19 23:10:48 +01:00
CommonUtils :: execSql ( $pdo , $sql , array ( $billsToTake , $pricesum , $signature , $newClosingId ));
2020-11-19 23:13:57 +01:00
set_time_limit ( 60 * 60 );
2020-11-19 23:12:32 +01:00
$sql = " SELECT value as val FROM %work% WHERE item=? " ;
$indexunclosedqueue = 0 ;
$r = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( 'indexunclosedqueue' ));
if ( count ( $r ) > 0 ) {
$rval = $r [ 0 ][ " val " ];
if ( ! is_null ( $rval )) {
$indexunclosedqueue = intval ( $rval );
}
}
2020-11-19 23:13:57 +01:00
set_time_limit ( 60 * 60 );
2020-11-19 23:14:13 +01:00
$sql = " UPDATE %queue% Q SET Q.clsid=? WHERE Q.id > ? AND Q.clsid is null " ;
2020-11-19 23:12:32 +01:00
CommonUtils :: execSql ( $pdo , $sql , array ( $newClosingId , $indexunclosedqueue ));
2020-11-19 22:47:44 +01:00
2020-11-19 23:12:27 +01:00
2020-11-19 23:12:32 +01:00
2020-11-19 23:13:57 +01:00
set_time_limit ( 60 * 60 );
2020-11-19 23:12:32 +01:00
$sql = " UPDATE %queue% SET toremove='1' WHERE billid is null AND clsid=? " ;
CommonUtils :: execSql ( $pdo , $sql , array ( $newClosingId ));
2020-11-19 22:47:44 +01:00
2020-11-19 23:13:57 +01:00
set_time_limit ( 60 * 60 );
2020-11-19 22:47:44 +01:00
$sql = " UPDATE %queue% set paidtime=?,delivertime=? WHERE billid is not null AND paidtime is null " ;
2020-11-19 23:10:48 +01:00
CommonUtils :: execSql ( $pdo , $sql , array ( $closingTime , $closingTime ));
2020-11-19 22:47:44 +01:00
2020-11-19 23:00:46 +01:00
$sql = " UPDATE %queue% set delivertime=?,workprinted=? WHERE billid is not null AND delivertime IS NULL " ;
2020-11-19 23:10:48 +01:00
CommonUtils :: execSql ( $pdo , $sql , array ( $closingTime , 1 ));
2020-11-19 22:47:44 +01:00
$sql = " DELETE FROM %printjobs% " ;
2020-11-19 23:10:48 +01:00
CommonUtils :: execSql ( $pdo , $sql , null );
2020-11-19 23:11:42 +01:00
CommonUtils :: execSql ( $pdo , " OPTIMIZE TABLE %printjobs% " , null );
2020-11-19 22:47:44 +01:00
2020-11-19 23:12:22 +01:00
$sql = " DELETE FROM %work% WHERE item=? " ;
CommonUtils :: execSql ( $pdo , $sql , array ( " sumuphash " ));
2020-11-19 23:13:57 +01:00
set_time_limit ( 60 * 60 );
2020-11-19 22:50:09 +01:00
$sql = " UPDATE %queue% SET isclosed=? " ;
2020-11-19 23:10:48 +01:00
CommonUtils :: execSql ( $pdo , $sql , array ( 1 ));
2020-11-19 22:50:09 +01:00
2020-11-19 23:14:48 +01:00
if ( $counting == 0 ) {
$sql = " SELECT counted FROM %closing% WHERE id=? " ;
$previousCounted = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $prevClsId ));
$prevCountedVal = doubleVal ( $previousCounted [ 0 ][ " counted " ]);
$sql = " SELECT COALESCE(SUM(B.brutto),'0.00') as sumbrutto FROM %bill% B,%closing% C WHERE B.paymentid='1' AND C.id=? AND C.id=B.closingid AND B.intguestid is null " ;
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $newClosingId ));
$salesAndCashOps = doubleVal ( $result [ 0 ][ 'sumbrutto' ]);
2020-11-19 23:14:58 +01:00
$counted = $prevCountedVal + $salesAndCashOps ;
2020-11-19 23:14:48 +01:00
$sql = " UPDATE %closing% SET counted=? WHERE id=? " ;
2020-11-19 23:14:58 +01:00
CommonUtils :: execSql ( $pdo , $sql , array ( $counted , $newClosingId ));
2020-11-19 23:14:48 +01:00
}
$sql = " SELECT FORMAT(COALESCE(SUM(brutto),'0.00'),2) as cashsum FROM %closing% C " ;
$sql .= " INNER JOIN %bill% B ON B.closingid=C.id " ;
$sql .= " WHERE C.id=? and B.paymentid=? " ;
$cashsumresult = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $newClosingId , 1 ));
$sql = " UPDATE %closing% SET cashsum=? WHERE id=? " ;
CommonUtils :: execSql ( $pdo , $sql , array ( $cashsumresult [ 0 ][ 'cashsum' ], $newClosingId ));
2020-11-19 23:03:29 +01:00
$dblogging = CommonUtils :: getConfigValue ( $pdo , 'dblog' , 1 );
if ( $dblogging == 0 ) {
$sql = " DELETE FROM %log% " ;
CommonUtils :: execSql ( $pdo , $sql , null );
2020-11-19 23:11:42 +01:00
CommonUtils :: execSql ( $pdo , " OPTIMIZE TABLE %log% " , null );
2020-11-19 23:03:29 +01:00
}
2020-11-19 23:03:48 +01:00
workreceipts :: resetWorkReceiptId ( $pdo );
2020-11-19 23:12:48 +01:00
$sql = " UPDATE %customerlog% SET clsid=? WHERE clsid is null " ;
CommonUtils :: execSql ( $pdo , $sql , array ( $newClosingId ));
2020-11-19 23:14:10 +01:00
$sql = " UPDATE %hist% SET clsid=? WHERE clsid IS NULL " ;
CommonUtils :: execSql ( $pdo , $sql , array ( $newClosingId ));
2020-11-19 23:14:48 +01:00
self :: signValueByTseAndUpdateClosing ( $pdo , $newClosingId , " Kassenabschluss $newClosingId - $closingTime " );
2020-11-19 23:14:58 +01:00
CommonUtils :: execSql ( $pdo , " UPDATE %operations% SET clsid=? WHERE clsid is null " , array ( $newClosingId ));
2020-11-19 22:47:44 +01:00
// commit must before email, because there direct access to db happens
$pdo -> commit ();
// now send the email
$toEmail = $this -> getGeneralItemFromDbWithPdo ( $pdo , " receiveremail " );
if (( $toEmail != '' ) && ( strpos ( $toEmail , '@' ) !== false )) {
$theSum = number_format ( $pricesum , 2 , $decpoint , '' );
$this -> emailCsvCore ( $pdo , $newClosingId , $toEmail , " Tagesabschluss " , $prevClosingDate , $closingTime , $theSum , $billsToTake );
}
$admin = new Admin ();
$versionInfo = $admin -> getEnv ( $pdo );
$content = array ( " env " => $versionInfo , " result " => $pricesum , " closingid " => $newClosingId );
2020-11-19 23:12:46 +01:00
if ( $checkForNewVersionAvailable ) {
2020-11-19 22:47:44 +01:00
// check if new version is evailable
// (do not inform user if last install or update is right before new version - let new version mature a bit..)
$url = " http://www.ordersprinter.de/version/checkversion.php? " ;
$url .= " v= " . $versionInfo [ " version " ] . " &i= " . $versionInfo [ " installdate " ] . " l= " . $versionInfo [ " lastupdate " ];
$ctx = stream_context_create ( array ( 'http' =>
array (
'timeout' => 5 , // 5 seconds
)
));
2020-11-19 23:14:48 +01:00
if ( ! CommonUtils :: startsWith ( $remark , " Test " )) {
// for auto-tests do not check for new available versions
2020-11-19 22:54:12 +01:00
$newversionavailable = @ file_get_contents ( $url , false , $ctx );
2020-11-19 23:14:48 +01:00
}
2020-11-19 22:47:44 +01:00
// TODO: has to be forwarded to user to inform him
2020-11-19 23:12:46 +01:00
}
2020-11-19 22:47:44 +01:00
2020-11-19 23:00:18 +01:00
CommonUtils :: keepOnlyLastLog ( $pdo );
// call plugin after completion of closing
2020-11-19 23:00:05 +01:00
CommonUtils :: callPlugin ( $pdo , " createClosing " , " after " );
2020-11-19 23:14:58 +01:00
return ( array ( " status " => " OK " , " msg " => $content , " print " => $doPrint , " counting " => $counting , " counted " => $counted ));
2020-11-19 22:47:44 +01:00
}
2020-11-19 22:54:51 +01:00
private function getSumOfBillsWithClosingId ( $pdo , $closingid , $onlyBar ) {
2020-11-19 23:00:35 +01:00
$sql = " SELECT count(id) as countid FROM %bill% WHERE closingid=? " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $closingid ));
$row = $stmt -> fetchObject ();
if ( $row -> countid == 0 ) {
return 0.0 ;
}
$sql = " SELECT sum(brutto) as billsum FROM %bill% WHERE closingid=? " ;
2020-11-19 22:47:44 +01:00
if ( $onlyBar ) {
$sql .= " AND paymentid='1' " ;
}
2020-11-19 22:54:51 +01:00
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $closingid ));
$row = $stmt -> fetchObject ();
$sum = floatval ( $row -> billsum );
2020-11-19 23:00:35 +01:00
2020-11-19 22:47:44 +01:00
return $sum ;
}
2020-11-19 22:51:21 +01:00
private function getUserGroupedSumOfClosing ( $pdo , $closingid ) {
$sql = " SELECT userid,username, " ;
$sql .= " ROUND(sum(brutto),2) as billsumall, " ;
$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=? GROUP BY userid,username " ;
2020-11-19 22:51:21 +01:00
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $closingid ));
$result = $stmt -> fetchAll ();
return $result ;
}
2020-11-19 22:58:30 +01:00
private function getTaxesGroupedOfClosing ( $pdo , $closingid ) {
$sql = " SELECT %queue%.tax as tax,SUM(price) as brutto,ROUND(SUM(price)/(1 + %queue%.tax/100.0),2) as netto FROM %queue%,%bill%,%closing% " ;
$sql .= " WHERE billid=%bill%.id AND %bill%.closingid=%closing%.id AND closingid=? GROUP BY tax " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $closingid ));
return ( $stmt -> fetchAll ( PDO :: FETCH_OBJ ));
}
private function getCashOpsOfClosing ( $pdo , $closingid ) {
$sql = " SELECT SUM(brutto) as cashsum FROM %bill%,%closing% WHERE status=? AND closingid=%closing%.id AND closingid=? " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( 'c' , $closingid ));
$row = $stmt -> fetchObject ();
return ( $row -> cashsum );
}
2020-11-19 23:11:27 +01:00
private function getCategoriasBruttoOfClosing ( $pdo , $closingid ) {
$sql = " SELECT SUM(price) as brutto,kind FROM %queue% Q,%bill%,%closing%,%prodtype% T,%products% P " ;
$sql .= " WHERE billid=%bill%.id AND %bill%.closingid=%closing%.id AND closingid=? " ;
$sql .= " AND Q.productid=P.id AND P.category=T.id " ;
$sql .= " GROUP BY kind " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $closingid ));
return ( $stmt -> fetchAll ( PDO :: FETCH_OBJ ));
}
2020-11-19 23:12:59 +01:00
private static function getFirstAndLastClosing () {
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
$sql = " select id,closingdate from %closing% ORDER BY closingdate LIMIT 1 " ;
$firstClosing = CommonUtils :: fetchSqlAll ( $pdo , $sql );
$sql = " select id,closingdate from %closing% ORDER BY closingdate DESC LIMIT 1 " ;
$lastClosing = CommonUtils :: fetchSqlAll ( $pdo , $sql );
echo json_encode ( array ( " status " => " OK " , " firstclosing " => self :: closingSqlDateToText ( $firstClosing ), " lastclosing " => self :: closingSqlDateToText ( $lastClosing )));
}
private static function closingSqlDateToText ( $closdate ) {
if ( count ( $closdate ) > 0 ) {
return $closdate [ 0 ][ " closingdate " ];
} else {
return " " ;
}
}
2020-11-19 23:12:32 +01:00
private function getClosingsListOfMonthYear ( $month , $year ) {
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
date_default_timezone_set ( DbUtils :: getTimeZone ());
$monthText = $month ;
if ( $month < 10 ) {
$monthText = " 0 " . $month ;
}
$lastDayInMonth = date ( " t " , mktime ( 0 , 0 , 0 , $month , 1 , $year ));
$dateStart = $year . $monthText . " 01 " ;
$dateEnd = $year . $monthText . $lastDayInMonth ;
2020-11-19 23:14:48 +01:00
$sql = " SELECT id,COALESCE(remark,'') as remark,DATE_FORMAT(closingdate,'%w') as dayofweek,DATE_FORMAT(closingdate, '%d.%m.%Y %k:%i') as closdate from %closing% WHERE DATE(closingdate) BETWEEN ? AND ? ORDER BY id " ;
2020-11-19 23:12:32 +01:00
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $dateStart , $dateEnd ));
echo json_encode ( array ( " status " => " OK " , " msg " => $result ));
}
2020-11-19 22:47:44 +01:00
/*
* Get all closings that are requested :
* if month and year is null or empty ==> last 30 closings
* otherwise query by date
*/
private function getClosings ( $month , $year ) {
2020-11-19 22:51:21 +01:00
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
2020-11-19 23:14:48 +01:00
2020-11-19 23:03:59 +01:00
date_default_timezone_set ( DbUtils :: getTimeZone ());
2020-11-19 22:47:44 +01:00
$monthText = $month ;
if ( $month < 10 ) {
$monthText = " 0 " . $month ;
}
$lastDayInMonth = date ( " t " , mktime ( 0 , 0 , 0 , $month , 1 , $year ));
$dateStart = $year . $monthText . " 01 " ;
2020-11-19 22:59:47 +01:00
$dateEnd = $year . $monthText . $lastDayInMonth ;
2020-11-19 23:14:48 +01:00
$sql = " SELECT id,closingdate,remark,counting,counted FROM %closing% WHERE DATE(closingdate) BETWEEN ? AND ? ORDER BY closingdate DESC " ;
2020-11-19 23:12:32 +01:00
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $dateStart , $dateEnd ));
2020-11-19 22:54:51 +01:00
$resultarray = array ();
foreach ( $result as $zeile ) {
2020-11-19 23:14:48 +01:00
$theId = $zeile [ 'id' ];
$closingDate = $zeile [ 'closingdate' ];
2020-11-19 22:47:44 +01:00
$remark = $zeile [ 'remark' ];
2020-11-19 23:14:48 +01:00
$counting = $zeile [ 'counting' ];
$counted = $zeile [ 'counted' ];
$masterData = CommonUtils :: getMasterDataAtCertainDateTime ( $pdo , $closingDate , " clstemplate " );
$coinname = $masterData [ 'coinvalname' ];
$notename = $masterData [ 'notevalname' ];
$barTotalBeforeTE = self :: getBarTotalBeforeTE ( $pdo , $theId , '.' );
$barTotalAfterTE = self :: getBarTotalAfterTE ( $pdo , $theId , '.' );
$diffSollIst = 0.0 ;
if ( $counting == 1 ) {
$sql = " SELECT id,value,count,iscoin,IF(iscoin='1','Münzen','Banknoten') as coinsornotes,FORMAT((count*value*IF(iscoin='1',1,100))/100.0,2) as sum,IF(iscoin='1',1,100) as factor FROM %counting% WHERE clsid=? ORDER BY iscoin DESC,value " ;
$countingprotocol = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $theId ));
$diffSollIst = $barTotalAfterTE - $counted ;
} else {
$countingprotocol = array ();
}
2020-11-19 22:54:51 +01:00
$totalSum = $this -> getSumOfBillsWithClosingId ( $pdo , $theId , false );
$cashSum = $this -> getSumOfBillsWithClosingId ( $pdo , $theId , true );
2020-11-19 22:51:21 +01:00
$userSums = $this -> getUserGroupedSumOfClosing ( $pdo , $theId );
2020-11-19 22:58:30 +01:00
$taxessums = $this -> getTaxesGroupedOfClosing ( $pdo , $theId );
2020-11-19 23:11:27 +01:00
$categorysums = $this -> getCategoriasBruttoOfClosing ( $pdo , $theId );
2020-11-19 22:58:30 +01:00
$cashops = $this -> getCashOpsOfClosing ( $pdo , $theId );
2020-11-19 23:10:46 +01:00
$daynameno = date ( 'N' , strtotime ( $closingDate ));
2020-11-19 23:14:48 +01:00
$closingEntry = array (
" id " => $theId ,
" closingDate " => $closingDate ,
" daynameno " => $daynameno ,
" remark " => $remark ,
" totalsum " => $totalSum ,
" cashsum " => $cashSum ,
" usersums " => $userSums ,
" taxessums " => $taxessums ,
" categorysums " => $categorysums ,
" cashops " => $cashops ,
" counted " => $counted ,
" coinname " => $coinname ,
" notename " => $notename ,
" countingprotocol " => $countingprotocol ,
" barTotalBeforeTE " => $barTotalBeforeTE ,
" barTotalAfterTE " => $barTotalAfterTE ,
" diffsollist " => $diffSollIst );
2020-11-19 22:47:44 +01:00
$resultarray [] = $closingEntry ;
2020-11-19 22:59:47 +01:00
}
2020-11-19 22:47:44 +01:00
echo json_encode ( array ( " status " => " OK " , " msg " => $resultarray ));
}
2020-11-19 23:14:48 +01:00
private static function getBarTotalBeforeTE ( $pdo , $clsid , $decpoint ) {
// TODO: include guest payments!
if ( $clsid == 1 ) {
return " 0.00 " ;
}
$sqlPrevClsId = " SELECT IFNULL(MAX(id),'0') FROM %closing% C where id<? " ;
$sql = " SELECT REPLACE(COALESCE(counted,'0.00'),'.',' $decpoint ') as counted FROM %closing% WHERE id=( $sqlPrevClsId ) " ;
$countedSql = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $clsid ));
return $countedSql [ 0 ][ 'counted' ];
}
private static function getBarTotalAfterTE ( $pdo , $clsid , $decpoint ) {
// TODO: include guest payments!
$previousCash = doubleVal ( self :: getBarTotalBeforeTE ( $pdo , $clsid , '.' ));
$sql = " SELECT COALESCE(SUM(B.brutto),'0.00') as sumbrutto FROM %bill% B,%closing% C WHERE B.paymentid='1' AND C.id=? AND C.id=B.closingid AND B.intguestid is null " ;
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $clsid ));
$salesAndCashOps = doubleval ( $result [ 0 ][ 'sumbrutto' ]);
return number_format ( $salesAndCashOps + $previousCash , 2 , $decpoint , '' );
}
2020-11-19 22:54:51 +01:00
private function getPaymentArray ( $pdo ) {
2020-11-19 22:47:44 +01:00
$sql = " SELECT id,name FROM %payment% " ;
2020-11-19 22:54:51 +01:00
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ();
$result = $stmt -> fetchAll ();
2020-11-19 22:47:44 +01:00
$retArray = array ();
2020-11-19 22:54:51 +01:00
foreach ( $result as $zeile ) {
2020-11-19 22:47:44 +01:00
$retArray [ $zeile [ 'id' ]] = $zeile [ 'name' ];
}
return $retArray ;
}
private function getClosing ( $closingid ) {
2020-11-19 22:54:51 +01:00
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
$this -> retrieveClosingFromDb ( $pdo , $closingid , false , false );
2020-11-19 22:47:44 +01:00
}
private function exportCsv ( $closingid ) {
2020-11-19 22:54:51 +01:00
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
$this -> retrieveClosingFromDb ( $pdo , $closingid , true , false );
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:02:12 +01:00
private function exportGuestCsv ( $closingid ) {
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
2020-11-19 23:12:48 +01:00
Customers :: exportLogOfOneClosing ( $pdo , $closingid );
2020-11-19 23:02:12 +01:00
}
2020-11-19 23:03:35 +01:00
2020-11-19 22:47:44 +01:00
private function emailCsvCore ( $pdo , $closingid , $toEmail , $topic , $startdate , $enddate , $billsum , $billcount ) {
2020-11-19 23:03:35 +01:00
$msg = $this -> getClosingByTaxAndUser ( $pdo , $closingid );
$msg .= $this -> retrieveClosingFromDb ( $pdo , $closingid , false , true );
2020-11-19 22:59:47 +01:00
$msg = " Zeitraum: $startdate - $enddate\nBrutto -Summe: $billsum\nEnthaltene Bons: $billcount\n\n " . $msg ;
$msg = str_replace ( " \n " , " \r \n " , $msg );
2020-11-19 22:47:44 +01:00
$topictxt = $topic . " " . $closingid . " \r \n " ;
2020-11-19 22:59:47 +01:00
if ( Emailer :: sendEmail ( $pdo , $msg , $toEmail , $topictxt )) {
return true ;
} else {
return false ;
2020-11-19 22:47:44 +01:00
}
}
private function emailCsv ( $closingid , $toEmail , $topic ) {
// additional info to insert into email
$decpoint = $this -> getDecPoint ();
$pdo = $this -> dbutils -> openDbAndReturnPdo ();
2020-11-19 22:59:47 +01:00
$prevClosingDate = self :: getDateOfPreviousClosing ( $pdo , $closingid );
if ( is_null ( $prevClosingDate )) {
$prevClosingDate = " " ;
2020-11-19 22:47:44 +01:00
}
$sql = " SELECT closingdate, billcount, billsum FROM %closing% WHERE id=? " ;
2020-11-19 22:59:47 +01:00
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
$stmt -> execute ( array ( $closingid ));
2020-11-19 22:47:44 +01:00
$row = $stmt -> fetchObject ();
$billsum = number_format ( $row -> billsum , 2 , $decpoint , '' );
$billcount = $row -> billcount ;
$closdate = $row -> closingdate ;
if ( $this -> emailCsvCore ( $pdo , $closingid , $toEmail , $topic , $prevClosingDate , $closdate , $billsum , $billcount )) {
2020-11-19 22:59:47 +01:00
echo json_encode ( array ( " status " => " OK " ));
} else {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_EMAIL_FAILURE , " msg " => ERROR_EMAIL_FAILURE_MSG ));
}
2020-11-19 22:47:44 +01:00
}
2020-11-19 22:59:47 +01:00
private function getGeneralItemFromDb ( $field ) {
$pdo = $this -> dbutils -> openDbAndReturnPdo ();
$this -> getGeneralItemFromDbWithPdo ( $pdo , $field );
2020-11-19 22:47:44 +01:00
}
private function getGeneralItemFromDbWithPdo ( $pdo , $field ) {
2020-11-19 22:59:47 +01:00
if ( is_null ( $pdo )) {
2020-11-19 22:47:44 +01:00
$pdo = $this -> dbutils -> openDbAndReturnPdo ();
2020-11-19 22:59:47 +01:00
}
$aValue = " " ;
$sql = " SELECT setting FROM %config% where name=' $field ' " ;
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
$stmt -> execute ();
$row = $stmt -> fetchObject ();
if ( $row != null ) {
$aValue = $row -> setting ;
}
return $aValue ;
2020-11-19 22:47:44 +01:00
}
public static function getDateOfPreviousClosing ( $pdoval , $closingid ) {
if ( is_null ( $pdoval )) {
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
} else {
$pdo = $pdoval ;
}
// ids can be generated but not used in case of rollback
$sql = " SELECT MAX(id) as previousid FROM %closing% WHERE id<? " ;
2020-11-19 22:59:47 +01:00
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $closingid ));
$row = $stmt -> fetchObject ();
if ( $row != null ) {
2020-11-19 22:47:44 +01:00
$previousId = intval ( $row -> previousid );
$sql = " SELECT closingdate FROM %closing% WHERE id=? " ;
2020-11-19 22:59:47 +01:00
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
2020-11-19 22:47:44 +01:00
$stmt -> execute ( array ( $previousId ));
$row = $stmt -> fetchObject ();
if ( $row != null ) {
return $row -> closingdate ;
} else {
return null ;
2020-11-19 22:59:47 +01:00
}
2020-11-19 22:47:44 +01:00
} else {
return null ;
2020-11-19 22:59:47 +01:00
}
2020-11-19 22:47:44 +01:00
}
private function returnErrorInconsDB ( $doCsvExport , $onlyresultreturn ) {
2020-11-19 22:59:47 +01:00
if ( $doCsvExport ) {
echo " ERROR - signatures do not fit " ;
} else if ( $onlyresultreturn ) {
return " Tagesabschluss-Datum: $closingdate\nBemerkung : $remark\nStatus : Inkonsistente Datenbank \n \n csv-Daten: \n " . $csv ;
} else {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_INCONSISTENT_DB , " msg " => ERROR_INCONSISTENT_DB_MSG ));
2020-11-19 22:47:44 +01:00
}
}
2020-11-19 23:03:35 +01:00
private function getClosingByTaxAndUser ( $pdo , $closingid ) {
$sql = " SELECT sum(price) as sumprice,%queue%.tax as thetax,username " ;
$sql .= " FROM %bill%,%billproducts%,%queue%,%user% " ;
$sql .= " WHERE %billproducts%.billid=%bill%.id AND %bill%.closingid=? AND %bill%.userid=%user%.id AND %billproducts%.queueid=%queue%.id " ;
2020-11-19 23:03:43 +01:00
$sql .= " AND (%bill%.status is null OR %bill%.status != (? OR ? OR ?)) " ;
2020-11-19 23:03:35 +01:00
$sql .= " GROUP BY username,thetax " ;
$decpoint = CommonUtils :: getConfigValue ( $pdo , " decpoint " , " , " );
2020-11-19 23:03:43 +01:00
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $closingid , 'c' , 'x' , 's' ));
2020-11-19 23:03:35 +01:00
$count = count ( $result );
if ( $count == 0 ) {
return " " ;
} else {
2020-11-19 23:10:09 +01:00
$msg = " Umsätze aufgeschlüsselt nach Benutzer und Steuersatz (ohne Ein-/Auslagen): \n \n " ;
2020-11-19 23:03:35 +01:00
$msg .= " Benutzer;Steuersatz;Umsatz (Brutto) \n " ;
foreach ( $result as $res ) {
// sumprice | thetax | username
$tax = str_replace ( '.' , $decpoint , $res [ 'thetax' ]);
$sumprice = str_replace ( '.' , $decpoint , $res [ 'sumprice' ]);
$msg .= $res [ 'username' ] . " ; $tax ; $sumprice\n " ;
}
return $msg . " \n " ;
}
}
2020-11-19 23:10:09 +01:00
2020-11-19 22:54:51 +01:00
private function retrieveClosingFromDb ( $pdo , $closingid , $doCsvExport , $onlyresultreturn ) {
2020-11-19 22:59:47 +01:00
if ( session_id () == '' ) {
session_start ();
2020-11-19 22:47:44 +01:00
}
2020-11-19 22:59:47 +01:00
2020-11-19 22:47:44 +01:00
$l = $_SESSION [ 'language' ];
2020-11-19 22:59:47 +01:00
$commonUtils = new CommonUtils ();
2020-11-19 22:47:44 +01:00
$currency = $commonUtils -> getCurrency ();
$decpoint = $this -> getDecPoint ();
2020-11-19 22:54:51 +01:00
$paymentArray = $this -> getPaymentArray ( $pdo );
2020-11-19 22:47:44 +01:00
$previousClosingDate = self :: getDateOfPreviousClosing ( null , $closingid );
$csv = " " ;
if ( $doCsvExport || $onlyresultreturn ) {
2020-11-19 22:59:47 +01:00
$file_name = " tagesabschluss.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 " );
2020-11-19 22:47:44 +01:00
header ( " Expires: 0 " );
2020-11-19 23:11:27 +01:00
$csv .= $this -> t [ 'ID' ][ $l ] . " ; " . $this -> t [ 'Date' ][ $l ] . " ; " . $this -> t [ 'Tablename' ][ $l ] . " ; " . $this -> t [ 'Prod' ][ $l ] . " ; " . $this -> t [ 'Category' ][ $l ] . " ; " . $this -> t [ 'Option' ][ $l ] . " ; " . $this -> t [ 'Brutto' ][ $l ] . " ( $currency ); " ;
2020-11-19 22:59:47 +01:00
$csv .= $this -> t [ 'Netto' ][ $l ] . " ( $currency ); " ;
2020-11-19 22:47:44 +01:00
$csv .= $this -> t [ 'Tax' ][ $l ] . " ; " ;
$csv .= $this -> t [ 'PayWay' ][ $l ] . " ; " ;
$csv .= $this -> t [ 'Userid' ][ $l ] . " ; " ;
$csv .= $this -> t [ 'User' ][ $l ] . " ; " ;
$csv .= $this -> t [ 'State' ][ $l ] . " ; " ;
$csv .= $this -> t [ 'Ref' ][ $l ] . " \n " ;
2020-11-19 22:59:47 +01:00
2020-11-19 22:47:44 +01:00
}
2020-11-19 22:54:51 +01:00
$sql = " SELECT closingdate,remark,signature,billsum,billcount FROM %closing% WHERE id=? " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $closingid ));
$row = $stmt -> fetchObject ();
$closingdate = $row -> closingdate ;
$remark = $row -> remark ;
$billsum = $row -> billsum ;
$billcount = $row -> billcount ;
$signature = $row -> signature ;
2020-11-19 23:14:21 +01:00
$cashSqlReadable = Bill :: getTranslateSqlCashTypeToReadable ();
$sql = " SELECT %bill%.id as id,IF(tableid > '0',(SELECT tableno FROM %resttables% WHERE id=tableid),'') as tablename,paymentid,billdate,userid,ref,username,status,brutto,netto,IF(tax is not null, tax, '0.00') as tax, $cashSqlReadable FROM %bill%,%user% WHERE closingid=? AND %bill%.userid = %user%.id ORDER BY billdate " ;
2020-11-19 22:54:51 +01:00
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $closingid ));
$billIdsAndPaymentsForThatClosing = $stmt -> fetchAll ();
2020-11-19 22:47:44 +01:00
$foundBillCount = count ( $billIdsAndPaymentsForThatClosing );
2020-11-19 22:59:47 +01:00
if ( is_null ( $previousClosingDate )) {
$startDate = " " ;
2020-11-19 22:47:44 +01:00
} else {
$startDate = $previousClosingDate ;
}
2020-11-19 22:59:47 +01:00
$billsumstr = number_format ( $billsum , 2 , " . " , '' );
2020-11-19 22:47:44 +01:00
$data = " I( $closingid )-S( $startDate )-E( $closingdate )-D( $billcount )-S( $billsumstr ) " ;
2020-11-19 23:12:07 +01:00
$md5ofData = md5 ( $data );
$ok = 1 ;
if ( $signature != $md5ofData ) {
$ok = 0 ;
}
2020-11-19 22:47:44 +01:00
if (( $ok == 0 ) || ( $billcount <> $foundBillCount )) {
// something went wrong!
$this -> returnErrorInconsDB ( $doCsvExport , $onlyresultreturn );
2020-11-19 22:59:47 +01:00
return ;
}
2020-11-19 22:47:44 +01:00
$retValues = array ();
2020-11-19 22:59:47 +01:00
for ( $index = 0 ; $index < count ( $billIdsAndPaymentsForThatClosing ); $index ++ ) {
2020-11-19 22:47:44 +01:00
$aBillId = $billIdsAndPaymentsForThatClosing [ $index ][ 'id' ];
if ( ! $commonUtils -> verifyBill ( $pdo , $aBillId )) {
2020-11-19 22:59:47 +01:00
$this -> returnErrorInconsDB ( $doCsvExport , $onlyresultreturn );
2020-11-19 22:47:44 +01:00
return ;
}
2020-11-19 23:10:09 +01:00
$tablename = $billIdsAndPaymentsForThatClosing [ $index ][ 'tablename' ];
2020-11-19 22:47:44 +01:00
$billdate = $billIdsAndPaymentsForThatClosing [ $index ][ 'billdate' ];
2020-11-19 22:58:27 +01:00
$paymentid = $billIdsAndPaymentsForThatClosing [ $index ][ 'paymentid' ];
2020-11-19 22:47:44 +01:00
$userid = $billIdsAndPaymentsForThatClosing [ $index ][ 'userid' ];
$username = $billIdsAndPaymentsForThatClosing [ $index ][ 'username' ];
$status = $billIdsAndPaymentsForThatClosing [ $index ][ 'status' ];
$brutto = $billIdsAndPaymentsForThatClosing [ $index ][ 'brutto' ];
$netto = $billIdsAndPaymentsForThatClosing [ $index ][ 'netto' ];
$tax = $billIdsAndPaymentsForThatClosing [ $index ][ 'tax' ];
$ref = $billIdsAndPaymentsForThatClosing [ $index ][ 'ref' ];
$ref = ( $ref == null ? " " : $ref );
2020-11-19 22:59:47 +01:00
if ( $status == 'c' ) {
2020-11-19 23:14:21 +01:00
$statusTxt = $this -> t [ 'cashact' ][ $l ] . $billIdsAndPaymentsForThatClosing [ $index ][ 'cashtype' ]; // "Bareinlage/-entnahme";
2020-11-19 22:47:44 +01:00
$brutto = number_format ( $brutto , 2 , $decpoint , '' );
$netto = number_format ( $netto , 2 , $decpoint , '' );
2020-11-19 22:59:47 +01:00
$tax = number_format ( $tax , 2 , $decpoint , '' );
$retValues [] = array (
" billid " => $aBillId ,
2020-11-19 23:10:09 +01:00
" tablename " => '' ,
2020-11-19 22:59:47 +01:00
" paidtime " => $billdate ,
" productname " => $this -> t [ 'cashaction' ][ $l ], // Kassenaktion
2020-11-19 23:11:27 +01:00
" kind " => " - " ,
2020-11-19 23:10:26 +01:00
" option " => '' ,
2020-11-19 22:47:44 +01:00
" price " => $brutto ,
" netto " => $netto ,
2020-11-19 22:59:47 +01:00
" tax " => number_format ( 0.00 , 2 , $decpoint , '' ),
" payment " => $paymentArray [ $paymentid ],
" userid " => $userid ,
" username " => $username ,
" status " => $statusTxt ,
" ref " => $ref );
2020-11-19 22:47:44 +01:00
if ( $doCsvExport || $onlyresultreturn ) {
2020-11-19 23:11:27 +01:00
$csv .= " $aBillId ; \" $billdate\ " ; \ " $tablename\ " ; \ " " . $this -> t [ 'cashaction' ][ $l ] . " \" ; - ; \" \" ; \" $brutto\ " ; \ " $netto\ " ; \ " $tax\ " ; \ " $paymentArray[$paymentid] \" ; $userid ; \" $username\ " ; \ " $statusTxt\ " ; $ref\n " ;
2020-11-19 22:59:47 +01:00
}
2020-11-19 22:47:44 +01:00
} else {
2020-11-19 23:12:35 +01:00
$unit = CommonUtils :: caseOfSqlUnitSelection ( $pdo );
$sql = " SELECT DISTINCT CONCAT( $unit ,productname) as productname,price,Q.tax as tax,anoption,kind FROM %queue% Q,%billproducts%,%prodtype%,%products% P WHERE %billproducts%.billid=? AND %billproducts%.queueid=Q.id AND Q.productid=P.id AND P.category=%prodtype%.id " ;
2020-11-19 22:59:47 +01:00
if ( $status == 'x' ) {
$statusTxt = $this -> t [ " laterCancelled " ][ $l ];
} else if ( $status == 's' ) {
$statusTxt = $this -> t [ " storno " ][ $l ];
} else {
2020-11-19 22:47:44 +01:00
$statusTxt = " " ;
2020-11-19 23:12:35 +01:00
$sql = " SELECT CONCAT( $unit ,productname) as productname,anoption,paidtime,price,Q.tax as tax,kind FROM %queue% Q,%prodtype%,%products% P WHERE billid=? AND Q.productid=P.id AND P.category=%prodtype%.id " ;
2020-11-19 22:59:47 +01:00
}
2020-11-19 22:54:51 +01:00
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $aBillId ));
$result = $stmt -> fetchAll ();
2020-11-19 22:59:47 +01:00
foreach ( $result as $zeile ) {
2020-11-19 22:47:44 +01:00
$productname = $zeile [ 'productname' ];
2020-11-19 23:10:26 +01:00
$option = $zeile [ 'anoption' ];
2020-11-19 22:47:44 +01:00
$tax = $zeile [ 'tax' ];
2020-11-19 23:11:27 +01:00
$kind = " Speise " ;
if ( $zeile [ 'kind' ] == 1 ) {
$kind = " Getraenk " ;
}
2020-11-19 22:59:47 +01:00
$paidtime = ( $billdate == null ? " " : $billdate ) ;
2020-11-19 22:47:44 +01:00
$price = ( $status == 's' ? 0.0 - floatval ( $zeile [ 'price' ]) : $zeile [ 'price' ]);
$netto = $price / ( 1 + $tax / 100.0 );
$netto = number_format ( $netto , 2 , $decpoint , '' );
$price = number_format ( $price , 2 , $decpoint , '' );
$formattedtax = number_format ( $tax , 2 , $decpoint , '' );
$retValues [] = array (
" billid " => $aBillId ,
2020-11-19 23:10:09 +01:00
" tablename " => $tablename ,
2020-11-19 22:47:44 +01:00
" paidtime " => $paidtime ,
" productname " => $productname ,
2020-11-19 23:11:27 +01:00
" kind " => $kind ,
2020-11-19 23:10:26 +01:00
" option " => $option ,
2020-11-19 22:47:44 +01:00
" price " => $price ,
" netto " => $netto ,
" tax " => $formattedtax ,
" payment " => $paymentArray [ $paymentid ],
" userid " => $userid ,
" username " => $username ,
" status " => $statusTxt ,
" ref " => $ref );
$productname = str_replace ( '"' , '""' , $productname );
2020-11-19 23:10:26 +01:00
$option = str_replace ( '"' , '""' , $option );
2020-11-19 22:47:44 +01:00
if ( $doCsvExport || $onlyresultreturn ) {
2020-11-19 23:11:27 +01:00
$csv .= " $aBillId ; \" $paidtime\ " ; \ " $tablename\ " ; \ " $productname\ " ; \ " $kind\ " ; \ " $option\ " ; \ " $price\ " ; \ " $netto\ " ; \ " $formattedtax\ " ; \ " $paymentArray[$paymentid] \" ; $userid ; \" $username\ " ; \ " $statusTxt\ " ; $ref\n " ;
2020-11-19 22:47:44 +01:00
}
2020-11-19 22:59:47 +01:00
}
}
2020-11-19 22:47:44 +01:00
}
if ( $doCsvExport ) {
echo $csv ;
} else if ( $onlyresultreturn ) {
return " Tagesabschluss-Datum: $closingdate\nBemerkung : $remark\n\ncsv -Daten: \n " . $csv ;
} else {
echo json_encode ( array ( " status " => " OK " , " msg " => $retValues , " closingid " => $closingid , " closingdate " => $closingdate , " previousClosingDate " => $previousClosingDate ));
}
}
2020-11-19 23:14:48 +01:00
public function getClosingSummaryWoSign ( $closingid , $pdo , $fromWeb , $replacedecpoint , $fl = 0 ) {
return $this -> getClosingSummaryCore ( $closingid , $pdo , $fromWeb , false , $replacedecpoint , $fl );
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:00:05 +01:00
public function getClosingSummary ( $closingid , $pdo , $fromWeb , $fl = 0 ) {
2020-11-19 23:14:48 +01:00
return $this -> getClosingSummaryCore ( $closingid , $pdo , $fromWeb , true , false , $fl );
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:12:07 +01:00
public static function signAllClosings ( $pdo ) {
$sql = " select id,closingdate,billcount,billsum,remark,signature from %closing% " ;
$r = CommonUtils :: fetchSqlAll ( $pdo , $sql );
$sql = " UPDATE %closing% SET signature=? WHERE id=? " ;
foreach ( $r as $c ) {
$closingid = $c [ " id " ];
$previousClosingDate = self :: getDateOfPreviousClosing ( $pdo , $closingid );
if ( is_null ( $previousClosingDate )) {
$startDate = " " ;
} else {
$startDate = $previousClosingDate ;
}
$billsumstr = number_format ( $c [ " billsum " ], 2 , " . " , '' );
$billcount = $c [ " billcount " ];
$closingdate = $c [ " closingdate " ];
$data = " I( $closingid )-S( $startDate )-E( $closingdate )-D( $billcount )-S( $billsumstr ) " ;
$md5ofdata = md5 ( $data );
CommonUtils :: execSql ( $pdo , $sql , array ( $md5ofdata , $closingid ));
}
}
2020-11-19 22:47:44 +01:00
public static function checkForClosingConsistency ( $pdo , $closingid ) {
$sql = " select id,closingdate,billcount,billsum,remark,signature from %closing% where id=? " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $closingid ));
$closingpart = $stmt -> fetchObject ();
$previousClosingDate = self :: getDateOfPreviousClosing ( $pdo , $closingid );
if ( is_null ( $previousClosingDate )) {
$startDate = " " ;
} else {
$startDate = $previousClosingDate ;
}
$billsumstr = number_format ( $closingpart -> billsum , 2 , " . " , '' );
$billcount = $closingpart -> billcount ;
$closingdate = $closingpart -> closingdate ;
$data = " I( $closingid )-S( $startDate )-E( $closingdate )-D( $billcount )-S( $billsumstr ) " ;
2020-11-19 23:12:07 +01:00
$md5ofdata = md5 ( $data );
$ok = 1 ;
if (( $closingpart -> signature ) != $md5ofdata ) {
$ok = 0 ;
}
2020-11-19 22:47:44 +01:00
return $ok ;
}
2020-11-19 23:14:48 +01:00
private static function getClosingMasterDataAtClsTime ( $pdo , $closid ) {
$sql = " SELECT closingdate FROM %closing% WHERE id=? " ;
$res = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $closid ));
$clsdate = $res [ 0 ][ " closingdate " ];
return CommonUtils :: getMasterDataAtCertainDateTime ( $pdo , $clsdate , " clstemplate " );
}
2020-11-19 23:14:51 +01:00
public static function getCashSumsForNextClosing ( $pdo ) {
$decpoint = CommonUtils :: getConfigValue ( $pdo , " decpoint " , " . " );
$sql = " SELECT MAX(id) as maxid FROM %closing% " ;
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql );
$maxid = $result [ 0 ][ " maxid " ];
if ( is_null ( $maxid )) {
$origCashInDeskFloat = 0.00 ;
} else {
$sql = " SELECT counted FROM %closing% WHERE id=? " ;
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $maxid ));
$origCashInDeskFloat = doubleval ( $result [ 0 ][ " counted " ]);
}
$sql = " SELECT ROUND(COALESCE(SUM(brutto),0.00),2) as sumbrutto FROM %bill% WHERE (status is null OR (status='x' OR status='s' OR status='c')) AND paymentid=? AND closingid is null " ;
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( 1 ));
$cashedNotInClosing = doubleval ( $result [ 0 ][ " sumbrutto " ]);
$cashInDeskFloat = $origCashInDeskFloat + $cashedNotInClosing ;
$cashInDesk = number_format ( $cashInDeskFloat , 2 , $decpoint , '' );
$cashedNotInClosing = number_format ( $cashedNotInClosing , 2 , $decpoint , '' );
echo json_encode ( array ( " status " => " OK " , " msg " => array ( " cashindesk " => $cashInDesk , " unclosedsalesincash " => $cashedNotInClosing )));
}
2020-11-19 23:14:48 +01:00
public function getClosingSummaryCore ( $closingid , $pdo , $fromWeb , $exportSignature , $replacedecpoint , $fl = 0 ) {
2020-11-19 22:47:44 +01:00
if ( is_null ( $pdo )) {
2020-11-19 23:14:48 +01:00
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
2020-11-19 22:47:44 +01:00
};
2020-11-19 23:14:48 +01:00
$currency = CommonUtils :: getConfigValue ( $pdo , 'currency' , " Euro " );
if ( $replacedecpoint ) {
$decpoint = CommonUtils :: getConfigValue ( $pdo , 'decpoint' , " . " );
} else {
$decpoint = " . " ;
}
$masterData = self :: getClosingMasterDataAtClsTime ( $pdo , $closingid );
2020-11-19 23:14:58 +01:00
$sql = " select id,closingdate,billcount,REPLACE(billsum,'.',' $decpoint ') as billsum,remark,signature,counting,REPLACE(counted,'.',' $decpoint ') as counted,counted as countedorig from %closing% where id=? " ;
2020-11-19 22:47:44 +01:00
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $closingid ));
$closingpart = $stmt -> fetchObject ();
$ok = self :: checkForClosingConsistency ( $pdo , $closingid );
if (( $ok == 0 )) {
if ( $fromWeb ) {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_INCONSISTENT_DB , " msg " => ERROR_INCONSISTENT_DB_MSG ));
return ;
} else {
return null ;
}
}
2020-11-19 23:14:58 +01:00
$barTotalAfterTEPointDecpoint = self :: getBarTotalAfterTE ( $pdo , $closingid , " . " );
2020-11-19 23:14:48 +01:00
$barTotalBeforeTE = self :: getBarTotalBeforeTE ( $pdo , $closingid , $decpoint );
$barTotalAfterTE = self :: getBarTotalAfterTE ( $pdo , $closingid , $decpoint );
$diffSollIst = 0.0 ;
$counting = $closingpart -> counting ;
$counted = $closingpart -> counted ;
2020-11-19 23:14:58 +01:00
$countedWithPoint = $closingpart -> countedorig ;
2020-11-19 23:14:48 +01:00
if ( $counting == 1 ) {
$sql = " SELECT id,value,count,iscoin,IF(iscoin='1','Münzen','Banknoten') as coinsornotes, " ;
$sql .= " REPLACE(FORMAT((count*value*IF(iscoin='1',1,100))/100.0,2),'.',' $decpoint ') as sum,IF(iscoin='1',1,100) as factor " ;
$sql .= " FROM %counting% WHERE clsid=? ORDER BY iscoin DESC,value " ;
$countingprotocol = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $closingid ));
2020-11-19 23:14:58 +01:00
$diffSollIst = number_format ( $barTotalAfterTEPointDecpoint - $countedWithPoint , 2 , $decpoint , '' );
2020-11-19 23:12:32 +01:00
} else {
2020-11-19 23:14:48 +01:00
$countingprotocol = array ();
}
2020-11-19 23:12:32 +01:00
2020-11-19 23:14:48 +01:00
$statusOfPaymentSql = " CASE WHEN B.status='x' THEN 'VERK' WHEN B.status is null THEN 'VERK' WHEN B.status='s' THEN 'BSTR' WHEN B.status='c' THEN 'EINL' END " ;
$sql = " SELECT REPLACE(SUM(B.brutto),'.',' $decpoint ') as sum,REPLACE(round(sum(B.netto),2),'.',' $decpoint ') as netto,P.name,IF(B.status='x',null,B.status) as status, " ;
$sql .= " ( $statusOfPaymentSql ) as process " ;
$sql .= " from %bill% B,%payment% P WHERE " ;
2020-11-19 23:12:32 +01:00
$sql .= " B.closingid=? AND B.paymentid=P.id AND " ;
$sql .= " (B.status <> 's' OR B.status is null) " ;
2020-11-19 23:14:48 +01:00
$sql .= " GROUP BY B.tax,P.name,IF(B.status='x',null,B.status) " ;
2020-11-19 23:12:32 +01:00
$sql .= " HAVING SUM(B.brutto) is not null " ;
$sql .= " UNION ALL " ;
2020-11-19 23:14:48 +01:00
$sql .= " SELECT REPLACE(sum(B.brutto),'.',' $decpoint ') as sum,REPLACE(round(sum(B.netto),2),'.',' $decpoint ') as netto,'',B.status, " ;
$sql .= " ( $statusOfPaymentSql ) as process " ;
$sql .= " FROM %bill% B WHERE " ;
2020-11-19 23:12:32 +01:00
$sql .= " B.closingid=? AND B.status='s' " ;
$sql .= " HAVING sum(B.brutto) is not null " ;
$sql .= " UNION ALL " ;
2020-11-19 23:14:48 +01:00
$sql .= " SELECT REPLACE(sum(Q.price),'.',' $decpoint ') as sum,REPLACE(TRUNCATE(sum(Q.price / (1 + Q.tax * 0.01)),2),'.',' $decpoint ') as netto,'','d' as status,'PSTR' as process from %queue% Q WHERE Q.toremove='1' AND Q.clsid=? " ;
2020-11-19 23:12:32 +01:00
$sql .= " HAVING sum(Q.price) is not null " ;
2020-11-19 23:14:48 +01:00
2020-11-19 23:12:32 +01:00
$overview = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $closingid , $closingid , $closingid ));
2020-11-19 22:47:44 +01:00
2020-11-19 23:14:48 +01:00
$sql = " select REPLACE(%queue%.tax,'.',' $decpoint ') as t,REPLACE(SUM(%queue%.price),'.',' $decpoint ') as bruttosum,REPLACE(ROUND(SUM(%queue%.price)/(1 + %queue%.tax/100.0),2),'.',' $decpoint ') as nettosum " ;
2020-11-19 23:00:05 +01:00
$sql .= " FROM %bill%,%queue% " ;
$sql .= " WHERE %bill%.closingid=? AND %queue%.billid=%bill%.id GROUP BY t " ;
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
$stmt -> execute ( array ( $closingid ));
$taxessum = $stmt -> fetchAll ( PDO :: FETCH_ASSOC );
2020-11-19 23:02:57 +01:00
$sql = " SELECT DISTINCT paymentid,name FROM %bill%,%payment% WHERE %bill%.closingid=? AND %bill%.paymentid=%payment%.id " ;
$payments = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $closingid ));
$paymenttaxes = array ();
foreach ( $payments as $aPayment ) {
2020-11-19 23:14:48 +01:00
$sql = " select REPLACE(%queue%.tax,'.',' $decpoint ') as t,REPLACE(SUM(%queue%.price),'.',' $decpoint ') as bruttosum,REPLACE(ROUND(SUM(%queue%.price)/(1 + %queue%.tax/100.0),2),'.',' $decpoint ') as nettosum " ;
2020-11-19 23:02:57 +01:00
$sql .= " FROM %bill%,%queue% " ;
$sql .= " WHERE %bill%.closingid=? AND %queue%.billid=%bill%.id AND %bill%.paymentid=? GROUP BY t " ;
2020-11-19 23:12:32 +01:00
$paymenttaxessum = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $closingid , $aPayment [ " paymentid " ]));
if ( count ( $paymenttaxessum ) > 0 ) {
2020-11-19 23:02:57 +01:00
$paymenttaxes [] = array ( " payment " => $aPayment [ " name " ], " paymenttaxessum " => $paymenttaxessum );
}
2020-11-19 23:12:32 +01:00
}
2020-11-19 23:02:57 +01:00
2020-11-19 23:14:48 +01:00
$sql = " select count(%queue%.productname) as count,%queue%.productname,REPLACE(%queue%.price,'.',' $decpoint ') as price,REPLACE(%queue%.tax,'.',' $decpoint ') as tax,REPLACE(sum(%queue%.price),'.',' $decpoint ') as sumprice " ;
2020-11-19 22:47:44 +01:00
$sql .= " from %queue%,%bill% where " ;
$sql .= " %queue%.billid=%bill%.id AND %bill%.closingid=? AND " ;
$sql .= " %bill%.status is null " ;
2020-11-19 22:58:20 +01:00
$sql .= " group by %queue%.productname,%queue%.tax,%queue%.price " ;
2020-11-19 22:47:44 +01:00
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
$stmt -> execute ( array ( $closingid ));
$details = $stmt -> fetchAll ( PDO :: FETCH_ASSOC );
// -> returns something like this:
if ( ! $exportSignature || $fromWeb ) {
unset ( $closingpart -> signature );
}
2020-11-19 23:14:48 +01:00
2020-11-19 23:02:57 +01:00
$closshowci = CommonUtils :: getConfigValue ( $pdo , 'closshowci' , 1 );
$closshowpaytaxes = CommonUtils :: getConfigValue ( $pdo , 'closshowpaytaxes' , 1 );
$closshowprods = CommonUtils :: getConfigValue ( $pdo , 'closshowprods' , 1 );
2020-11-19 23:14:10 +01:00
$companyinfo = CommonUtils :: getConfigValueAtClosingTime ( $pdo , 'companyinfo' , '' , $closingid );
2020-11-19 23:14:48 +01:00
$retVal = array ( " closing " => $closingpart ,
" overview " => $overview ,
" details " => $details ,
" taxessum " => $taxessum ,
2020-11-19 23:02:57 +01:00
" companyinfo " => $companyinfo ,
" paymenttaxessum " => $paymenttaxes ,
" closshowci " => $closshowci ,
" closshowpaytaxes " => $closshowpaytaxes ,
2020-11-19 23:14:48 +01:00
" closshowprods " => $closshowprods ,
" countingprotocol " => $countingprotocol ,
" barTotalBeforeTE " => $barTotalBeforeTE ,
" barTotalAfterTE " => $barTotalAfterTE ,
" diffsollist " => $diffSollIst
2020-11-19 23:02:57 +01:00
);
2020-11-19 23:14:48 +01:00
2020-11-19 23:14:10 +01:00
$retVal [ " sn " ] = CommonUtils :: getConfigValueAtClosingTime ( $pdo , 'sn' , " ? " , $closingid );
$retVal [ " uid " ] = CommonUtils :: getConfigValueAtClosingTime ( $pdo , 'uid' , " ? " , $closingid );
$retVal [ " version " ] = CommonUtils :: getConfigValueAtClosingTime ( $pdo , 'version' , " ? " , $closingid );
$retVal [ " systemid " ] = CommonUtils :: getConfigValueAtClosingTime ( $pdo , 'systemid' , " ? " , $closingid );
2020-11-19 23:14:48 +01:00
$retVal [ " version " ] = $masterData [ " version " ];
$retVal [ " template " ] = $masterData [ " template " ];
$retVal [ " companyinfo " ] = $masterData [ " companyinfo " ];
$retVal [ " dsfinvk_name " ] = $masterData [ " dsfinvk_name " ];
$retVal [ " dsfinvk_street " ] = $masterData [ " dsfinvk_street " ];
$retVal [ " dsfinvk_postalcode " ] = $masterData [ " dsfinvk_postalcode " ];
$retVal [ " dsfinvk_city " ] = $masterData [ " dsfinvk_city " ];
$retVal [ " dsfinvk_country " ] = $masterData [ " dsfinvk_country " ];
$retVal [ " dsfinvk_stnr " ] = $masterData [ " dsfinvk_stnr " ];
$retVal [ " dsfinvk_ustid " ] = $masterData [ " dsfinvk_ustid " ];
$retVal [ " closingid " ] = $closingid ;
$retVal [ " billcount " ] = $closingpart -> billcount ;
$retVal [ " billsum " ] = str_replace ( '.' , $decpoint , $closingpart -> billsum ) . " " . $currency ;
$retVal [ " closingdate " ] = $closingpart -> closingdate ;
$retVal [ " remark " ] = $closingpart -> remark ;
$retVal [ " counting " ] = $counting ;
$retVal [ " counted " ] = $counted ;
2020-11-19 22:47:44 +01:00
if ( $fromWeb ) {
echo json_encode ( array ( " status " => " OK " , " msg " => $retVal ));
} else {
return $retVal ;
}
}
2020-11-19 23:11:49 +01:00
private function htmlreport ( $closingid ) {
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
$ok = self :: checkForClosingConsistency ( $pdo , $closingid );
if (( $ok == 0 )) {
echo " Datenbank inkonsistent " ;
return ;
}
2020-11-19 23:14:48 +01:00
$closing = $this -> getClosingSummaryCore ( $closingid , $pdo , false , false , false , 8 );
2020-11-19 23:11:49 +01:00
$clostemplate = CommonUtils :: getConfigValue ( $pdo , " clostemplate " , " Kein Template festgelegt " );
$templatelines = Templateutils :: getTemplateAsLineArray ( $clostemplate );
$reporter = new ClosTemplater ( $templatelines , $closingid , $closing );
$txt = $reporter -> createWithTemplate ( $pdo );
$html = " <html> " ;
$html = " <head><meta charset='UTF-8'></head> " ;
$html .= " <body><style> " ;
$html .= " </style> " ;
$html .= $txt ;
$version = CommonUtils :: getConfigValue ( $pdo , " version " , " " );
$html .= " <br><br><hr><div style='text-align:right;font-style:italic;'>OrderSprinter $version </div> " ;
$html .= " </body></html> " ;
echo $html ;
}
2020-11-19 23:14:48 +01:00
public static function createdCountedValuesForClosing ( $pdo ) {
$sql = " SELECT id from %closing% ORDER BY id " ;
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql );
foreach ( $result as $r ) {
$clsid = $r [ 'id' ];
$sqlNoCounting = " SELECT COALESCE(SUM(B.brutto),'0.00') as sumbrutto FROM %bill% B,%closing% C WHERE B.paymentid='1' AND C.id<=? AND C.id=B.closingid AND B.intguestid is null " ;
$sumbruttores = CommonUtils :: fetchSqlAll ( $pdo , $sqlNoCounting , array ( $clsid ));
CommonUtils :: execSql ( $pdo , " UPDATE %closing% SET counted=? WHERE id=? " , array ( $sumbruttores [ 0 ][ " sumbrutto " ], $clsid ));
}
}
private static function signValueByTseAndUpdateClosing ( $pdo , $closingid , $valueToSign ) {
$status = TSE :: sendFreeContentToTSE ( $pdo , $valueToSign );
if ( $status [ " status " ] != " OK " ) {
return ( array ( " status " => " ERROR " , " msg " => " TSE-Signierung fehlgeschlagen. Vorgang konnte nicht ausgeführt werden. " ));
} else {
$logtime = 0 ;
$trans = 0 ;
$tseSignature = '' ;
$pubkeyRef = null ;
$sigalgRef = null ;
$sigcounter = 0 ;
if ( $status [ " usetse " ] == DbUtils :: $TSE_OK ) {
$msgAsString = $status [ " msg " ];
$tseLog = json_decode ( $msgAsString , true );
$logtime = $tseLog [ " logtime " ];
$trans = $tseLog [ " trans " ];
$sigcounter = $tseLog [ " sigcounter " ];
$tseSignature = $tseLog [ " signature " ];
$publicKey = $tseLog [ " publickey " ];
$tseCertificate = $tseLog [ " certificate " ];
$serialNo = $tseLog [ " serialno " ];
$sigalgRef = CommonUtils :: referenceValueInTseValuesTable ( $pdo , $tseLog [ " sigalg " ]);
$pubkeyRef = CommonUtils :: referenceValueInTseValuesTable ( $pdo , $tseLog [ " publickey " ]);
$serialNoRef = CommonUtils :: referenceValueInTseValuesTable ( $pdo , $tseLog [ " serialno " ]);
$certificateRef = CommonUtils :: referenceValueInTseValuesTable ( $pdo , $tseLog [ " certificate " ]);
}
$opid = Operations :: createOperation (
$pdo ,
DbUtils :: $PROCESSTYPE_SONSTIGER_VORGANG ,
DbUtils :: $OPERATION_IN_CLOSING_TABLE ,
$logtime ,
$trans ,
$valueToSign ,
$tseSignature ,
$pubkeyRef ,
$sigalgRef ,
$serialNoRef ,
$certificateRef ,
$sigcounter ,
$status [ " usetse " ]);
$sql = " UPDATE %closing% SET opid=? WHERE id=? " ;
CommonUtils :: execSql ( $pdo , $sql , array ( $opid , $closingid ));
return array ( " status " => " OK " );
}
}
2020-11-19 23:10:09 +01:00
}