2020-11-19 22:47:44 +01:00
< ? php
2020-11-19 23:00:18 +01:00
// Datenbank-Verbindungsparameter
require_once ( 'dbutils.php' );
2020-11-19 22:47:44 +01:00
require_once ( 'commonutils.php' );
require_once ( 'admin.php' );
require_once ( 'reports.php' );
2020-11-19 22:52:25 +01:00
require_once ( 'utilities/pdfexport.php' );
2020-11-19 23:00:18 +01:00
require_once ( '3rdparty/phpexcel/classes/PHPExcel.php' );
2020-11-19 22:47:44 +01:00
define ( 'FPDF_FONTPATH' , '3rdparty/fpdf/font/' );
2020-11-19 22:52:25 +01:00
define ( 'DO_EXCEL' , 1 );
define ( 'DO_CSV' , 2 );
2020-11-19 23:00:18 +01:00
class Bill {
2020-11-19 22:47:44 +01:00
var $dbutils ;
2020-11-19 23:00:18 +01:00
var $t ;
2020-11-19 22:47:44 +01:00
private $P_SUM = array ( " Summe: " , " Sum: " , " Todo: " );
private $P_TOTAL = array ( " Total " , " Total " , " Total " );
private $P_MWST = array ( " MwSt " , " Tax " , " IVA " );
private $P_NETTO = array ( " Netto " , " Net " , " Neto " );
private $P_BRUTTO = array ( " Brutto " , " Gross " , " Bruto " );
private $P_ID = array ( " Id: " , " Id: " , " Id: " );
private $P_TABLE = array ( " Tisch: " , " Table: " , " Mesa: " );
private $P_WAITER = array ( " Es bediente Sie: " , " Waiter: " , " Camarero: " );
private $P_NO = array ( " Anz. " , " No. " , " Nú. " );
private $P_DESCR = array ( " Beschreibung " , " Description " , " Descripción " );
private $P_PRICE = array ( " Preis " , " Price " , " Precio " );
2020-11-19 23:00:18 +01:00
function __construct () {
2020-11-19 22:47:44 +01:00
$this -> dbutils = new DbUtils ();
2020-11-19 23:00:18 +01:00
require_once 'translations.php' ;
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:00:18 +01:00
2020-11-19 22:47:44 +01:00
function handleCommand ( $command ) {
if ( $command == 'exportCsv' ) {
if ( $this -> hasCurrentUserAdminOrManagerRights ()) {
2020-11-19 22:52:25 +01:00
$this -> exportCsv ( $_GET [ 'startMonth' ], $_GET [ 'startYear' ], $_GET [ 'endMonth' ], $_GET [ 'endYear' ], DO_CSV );
} else {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_BILL_NOT_AUTHOTRIZED , " msg " => ERROR_BILL_NOT_AUTHOTRIZED_MSG ));
}
return ;
}
if ( $command == 'exportXlsx' ) {
if ( $this -> hasCurrentUserAdminOrManagerRights ()) {
$this -> exportCsv ( $_GET [ 'startMonth' ], $_GET [ 'startYear' ], $_GET [ 'endMonth' ], $_GET [ 'endYear' ], DO_EXCEL );
2020-11-19 22:47:44 +01:00
} else {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_BILL_NOT_AUTHOTRIZED , " msg " => ERROR_BILL_NOT_AUTHOTRIZED_MSG ));
}
return ;
}
if ( $command == 'exportPdfReport' ) {
if ( $this -> hasCurrentUserAdminOrManagerRights ()) {
2020-11-19 22:55:09 +01:00
$this -> exportPdfReport ( $_GET [ 'startMonth' ], $_GET [ 'startYear' ], $_GET [ 'endMonth' ], $_GET [ 'endYear' ]);
2020-11-19 22:47:44 +01:00
} else {
echo " Benutzer nicht berechtigt " ;
}
return ;
}
if ( $command == 'exportPdfSummary' ) {
if ( $this -> hasCurrentUserAdminOrManagerRights ()) {
2020-11-19 22:55:09 +01:00
$this -> exportPdfSummary ( $_GET [ 'startMonth' ], $_GET [ 'startYear' ], $_GET [ 'endMonth' ], $_GET [ 'endYear' ]);
2020-11-19 22:47:44 +01:00
} else {
echo " Benutzer nicht berechtigt " ;
}
return ;
}
2020-11-19 22:55:09 +01:00
if ( $command == 'autoBackupPdfSummary' ) {
$this -> autoBackupPdfSummary ( $_POST [ 'remoteaccesscode' ]);
return ;
}
2020-11-19 22:47:44 +01:00
if ( $command == 'exportCsvOfClosing' ) {
2020-11-19 23:00:18 +01:00
if ( $this -> hasCurrentUserAdminOrManagerRights ()) {
$this -> exportCsvOfClosing ( $_GET [ 'closingid' ], DO_CSV );
} else {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_MANAGER_NOT_AUTHOTRIZED , " msg " => ERROR_MANAGER_NOT_AUTHOTRIZED_MSG ));
}
2020-11-19 22:47:44 +01:00
return ;
}
2020-11-19 22:52:25 +01:00
if ( $command == 'exportXlsxOfClosing' ) {
if ( $this -> hasCurrentUserAdminOrManagerRights ()) {
$this -> exportCsvOfClosing ( $_GET [ 'closingid' ], DO_EXCEL );
} else {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_MANAGER_NOT_AUTHOTRIZED , " msg " => ERROR_MANAGER_NOT_AUTHOTRIZED_MSG ));
}
return ;
}
2020-11-19 22:47:44 +01:00
if ( $command == 'doCashAction' ) {
if ( $this -> hasCurrentUserPaydeskRights ()) {
2020-11-19 23:00:18 +01:00
$remark = " " ;
if ( isset ( $_POST [ " remark " ])) {
$remark = $_POST [ 'remark' ];
}
$this -> doCashAction ( $_POST [ 'money' ], $remark );
2020-11-19 22:47:44 +01:00
} else {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_PAYDESK_NOT_AUTHOTRIZED , " msg " => ERROR_PAYDESK_NOT_AUTHOTRIZED_MSG ));
}
return ;
} else if ( $command == 'getCashOverviewOfUser' ) {
if ( $this -> hasCurrentUserPaydeskRights ()) {
$this -> getCashOverviewOfUser ();
} else {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_PAYDESK_NOT_AUTHOTRIZED , " msg " => ERROR_PAYDESK_NOT_AUTHOTRIZED_MSG ));
}
return ;
}
2020-11-19 22:55:20 +01:00
if ( $command == 'changeBillHost' ) {
if ( $this -> hasCurrentUserPaydeskRights ()) {
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
$this -> changeBillHost ( $pdo , $_POST [ 'billid' ], $_POST [ 'isNowHost' ]);
} else {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_PAYDESK_NOT_AUTHOTRIZED , " msg " => ERROR_PAYDESK_NOT_AUTHOTRIZED_MSG ));
}
return ;
}
2020-11-19 23:02:19 +01:00
if ( $command == 'initaustriareceipt' ) {
if ( $this -> hasCurrentUserAdminOrManagerRights ()) {
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
$status = $this -> initaustriareceipt ( $pdo );
echo json_encode ( $status );
} else {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_MANAGER_NOT_AUTHOTRIZED , " msg " => ERROR_MANAGER_NOT_AUTHOTRIZED_MSG ));
}
return ;
}
2020-11-19 23:00:18 +01:00
if ( $this -> hasCurrentUserBillRights ()) {
2020-11-19 22:47:44 +01:00
if ( $command == 'getLastBillsWithContent' ) {
$this -> getLastBillsWithContent ( $_GET [ 'day' ], $_GET [ 'month' ], $_GET [ 'year' ]);
} else if ( $command == 'cancelBill' ) {
2020-11-19 22:55:20 +01:00
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
2020-11-19 23:00:55 +01:00
$this -> cancelBill ( $pdo , $_POST [ 'billid' ], $_POST [ 'stornocode' ], $_POST [ 'reason' ], true , true , true , $_POST [ 'removeproducts' ]);
2020-11-19 22:47:44 +01:00
}
} else {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_BILL_NOT_AUTHOTRIZED , " msg " => ERROR_BILL_NOT_AUTHOTRIZED_MSG ));
2020-11-19 23:00:18 +01:00
}
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:00:18 +01:00
private function hasCurrentUserBillRights () {
session_start ();
if ( ! isset ( $_SESSION [ 'angemeldet' ]) || ! $_SESSION [ 'angemeldet' ]) {
return false ;
} else {
return ( $_SESSION [ 'right_bill' ]);
}
2020-11-19 22:47:44 +01:00
}
private function hasCurrentUserPaydeskRights () {
session_start ();
if ( ! isset ( $_SESSION [ 'angemeldet' ]) || ! $_SESSION [ 'angemeldet' ]) {
return false ;
} else {
return ( $_SESSION [ 'right_paydesk' ]);
}
}
2020-11-19 23:00:18 +01:00
private function hasCurrentUserAdminOrManagerRights () {
session_start ();
if ( ! isset ( $_SESSION [ 'angemeldet' ]) || ! $_SESSION [ 'angemeldet' ]) {
return false ;
} else {
return ( $_SESSION [ 'right_manager' ] || $_SESSION [ 'is_admin' ]);
}
2020-11-19 22:47:44 +01:00
}
2020-11-19 22:55:09 +01:00
function billIsCancelled ( $pdo , $billid ) {
$sql = " SELECT status FROM %bill% WHERE id=? " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $billid ));
$row = $stmt -> fetchObject ();
$status = $row -> status ;
$ret = false ;
if (( $status == " x " ) || ( $status == " s " )) {
$ret = true ;
}
return $ret ;
}
2020-11-19 22:47:44 +01:00
/**
* get the content of a bill ( to be used for printserver etc . )
*
* @ param unknown $billid
*/
2020-11-19 23:02:24 +01:00
function getBillWithId ( $pdo , $billid , $language , $printer , $includeGuestInfo = false , $includePayment = false ) {
2020-11-19 22:47:44 +01:00
set_time_limit ( 120 );
$commonUtils = new CommonUtils ();
$correct = $commonUtils -> verifyBill ( $pdo , $billid );
if ( ! $correct ) {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_INCONSISTENT_DB , " msg " => ERROR_INCONSISTENT_DB_MSG ));
return ;
}
$sql = " SELECT tableid FROM %bill% WHERE id=? " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $billid ));
$row = $stmt -> fetchObject ();
$sql = " SELECT count(id) as countid FROM %queue% WHERE billid=? " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $billid ));
$qrow = $stmt -> fetchObject ();
2020-11-19 22:55:09 +01:00
$tableid = $row -> tableid ;
2020-11-19 22:47:44 +01:00
if ( $qrow -> countid == 0 ) {
2020-11-19 22:55:09 +01:00
if ( $tableid == 0 ) {
2020-11-19 23:10:09 +01:00
$sql = " SELECT DISTINCT billdate,brutto,ROUND(netto,2) as netto,'-' as tablename,username,host,IFNULL(%bill%.status,'') as status,guestinfo,paymentid FROM %bill%,%user% WHERE %bill%.id=? AND userid=%user%.id AND tableid='0' " ;
2020-11-19 22:47:44 +01:00
} else {
2020-11-19 23:10:09 +01:00
$sql = " SELECT DISTINCT billdate,brutto,ROUND(netto,2) as netto,tableno as tablename,username,host,IFNULL(%bill%.status,'') as status,guestinfo,paymentid FROM %bill%,%user%,%resttables% WHERE %bill%.id=? AND userid=%user%.id AND tableid=%resttables%.id " ;
2020-11-19 22:47:44 +01:00
}
} else {
2020-11-19 22:55:09 +01:00
if ( $tableid == 0 ) {
2020-11-19 23:10:09 +01:00
$sql = " SELECT DISTINCT billdate,brutto,ROUND(netto,2) as netto,'-' as tablename,username,host,IFNULL(%bill%.status,'') as status,guestinfo,paymentid FROM %bill%,%user%,%queue% WHERE %bill%.id=? AND %bill%.id=%queue%.billid AND userid=%user%.id AND tableid='0' AND paidtime is not null " ;
2020-11-19 22:47:44 +01:00
} else {
2020-11-19 23:10:09 +01:00
$sql = " SELECT DISTINCT billdate,brutto,ROUND(netto,2) as netto,tableno as tablename,username,host,IFNULL(%bill%.status,'') as status,guestinfo,paymentid FROM %bill%,%user%,%resttables%,%queue% WHERE %bill%.id=? AND %bill%.id=%queue%.billid AND userid=%user%.id AND tableid=%resttables%.id AND paidtime is not null " ;
2020-11-19 22:47:44 +01:00
}
}
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $billid ));
$row = $stmt -> fetchObject ();
2020-11-19 22:55:09 +01:00
$status = $row -> status ;
$sign = ( $status == " s " ? " - " : " " );
if ( $tableid != 0 ) {
$sql = " SELECT abbreviation FROM %room%,%resttables% WHERE %resttables%.id=? AND %resttables%.roomid=%room%.id " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $tableid ));
$trow = $stmt -> fetchObject ();
if ( is_null ( $trow -> abbreviation )) {
$tablename = $row -> tablename ;
} else {
$tablename = $trow -> abbreviation . " - " . $row -> tablename ;
}
} else {
$tablename = " - " ;
}
2020-11-19 22:47:44 +01:00
if ( $row == null ) {
echo json_encode ( array ( " billoverallinfo " => array ()));
return ;
} else {
if ( is_null ( $row -> host )) {
$host = 0 ; // default
} else {
$host = $row -> host ;
}
$thetimedate = $row -> billdate ;
$thetimedate_arr = explode ( ' ' , $thetimedate );
$thedate = $thetimedate_arr [ 0 ];
$datearr = explode ( '-' , $thedate );
$day = sprintf ( " %02s " , $datearr [ 2 ]);
$month = sprintf ( " %02s " , $datearr [ 1 ]);
$year = sprintf ( " %04s " , $datearr [ 0 ]);
$thetime = $thetimedate_arr [ 1 ];
$thetimearr = explode ( ':' , $thetime );
$hour = $thetimearr [ 0 ];
$min = $thetimearr [ 1 ];
$thetimedate = " $day . $month . $year $hour : $min " ;
$billoverallinfo = array (
" id " => $billid ,
" billdate " => $thetimedate ,
" billday " => $day ,
" billmonth " => $month ,
" billyear " => $year ,
" billhour " => $hour ,
" billmin " => $min ,
" brutto " => $row -> brutto ,
" netto " => $row -> netto ,
2020-11-19 22:55:09 +01:00
" table " => $tablename ,
2020-11-19 22:47:44 +01:00
" username " => $row -> username ,
" printer " => $printer ,
" host " => $host
);
2020-11-19 23:00:58 +01:00
if ( $includeGuestInfo ) {
if ( is_null ( $row -> guestinfo )) {
$billoverallinfo [ " guestinfo " ] = '' ;
} else {
$billoverallinfo [ " guestinfo " ] = $row -> guestinfo ;
}
}
2020-11-19 23:02:24 +01:00
if ( $includePayment ) {
$col = " name " ;
if ( $language == 1 ) {
$col = " name_en " ;
} else if ( $language == 2 ) {
$col = " name_esp " ;
}
$paymentid = $row -> paymentid ;
$sql = " SELECT $col FROM %payment% as name WHERE id=? " ;
$paymentname = CommonUtils :: getRowSqlObject ( $pdo , $sql , array ( $paymentid ));
$billoverallinfo [ " payment " ] = $paymentname -> name ;
}
2020-11-19 22:47:44 +01:00
$billtranslations = array (
" sum " => $this -> P_SUM [ $language ],
" total " => $this -> P_TOTAL [ $language ],
" mwst " => $this -> P_MWST [ $language ],
" netto " => $this -> P_NETTO [ $language ],
" brutto " => $this -> P_BRUTTO [ $language ],
" id " => $this -> P_ID [ $language ],
" table " => $this -> P_TABLE [ $language ],
" waiter " => $this -> P_WAITER [ $language ],
" no " => $this -> P_NO [ $language ],
" descr " => $this -> P_DESCR [ $language ],
" price " => $this -> P_PRICE [ $language ]
);
}
2020-11-19 23:01:07 +01:00
$sql = " select productname,price,%pricelevel%.name as pricelevelname,togo,count(%queue%.productname) as count,%prodtype%.kind as kind from %queue%,%pricelevel%,%billproducts%,%prodtype%,%products% where %billproducts%.billid=? AND %billproducts%.queueid=%queue%.id AND %queue%.pricelevel = %pricelevel%.id AND %queue%.productid = %products%.id AND %products%.category = %prodtype%.id group by kind, productname,price,pricelevelname,togo " ;
2020-11-19 22:47:44 +01:00
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
$stmt -> execute ( array ( $billid ));
$result = $stmt -> fetchAll ();
$prodarray = array ();
foreach ( $result as $zeile ) {
2020-11-19 22:55:20 +01:00
$productname = $zeile [ 'productname' ];
if ( $zeile [ " togo " ] == 1 ) {
$productname = " To-Go: " . $productname ;
}
2020-11-19 22:47:44 +01:00
$prodarray [] = array ( " count " => $zeile [ 'count' ],
2020-11-19 22:55:20 +01:00
" productname " => $productname ,
2020-11-19 22:47:44 +01:00
" pricelevel " => $zeile [ 'pricelevelname' ],
2020-11-19 22:55:09 +01:00
" price " => $sign . $zeile [ 'price' ]
2020-11-19 22:47:44 +01:00
);
}
2020-11-19 22:55:09 +01:00
$sql = " select tax,concat(' $sign ',round(sum(price) - sum(price / (1.0 + tax/100.0)),2)) as mwst, concat(' $sign ',round(sum(price / (1.0 + tax/100.0)),2)) as netto, concat(' $sign ',sum(price)) as brutto FROM %queue%,%billproducts% WHERE %billproducts%.billid=? AND %billproducts%.queueid=%queue%.id group by tax ORDER BY tax " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
2020-11-19 22:47:44 +01:00
$stmt -> execute ( array ( $billid ));
$result = $stmt -> fetchAll ( PDO :: FETCH_OBJ );
2020-11-19 22:55:09 +01:00
2020-11-19 22:47:44 +01:00
$out = array ( " billoverallinfo " => $billoverallinfo , " translations " => $billtranslations , " products " => $prodarray , " taxes " => $result );
return $out ;
}
2020-11-19 23:00:31 +01:00
public function getAustriaTaxes ( $pdo , $billid ) {
$sql = " select tax,IF(taxaustria is not null, taxaustria, 0) as taxaustria,concat(' $sign ',round(sum(price) - sum(price / (1.0 + tax/100.0)),2)) as mwst, concat(' $sign ',round(sum(price / (1.0 + tax/100.0)),2)) as netto, concat(' $sign ',sum(price)) as brutto FROM %queue%,%billproducts% WHERE %billproducts%.billid=? AND %billproducts%.queueid=%queue%.id group by tax ORDER BY taxaustria " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $billid ));
$result = $stmt -> fetchAll ( PDO :: FETCH_OBJ );
return $result ;
}
2020-11-19 22:47:44 +01:00
/*
* insert or take out cash money . The direction done by sign of $money value
*/
2020-11-19 23:00:18 +01:00
private function doCashAction ( $money , $remark ) {
date_default_timezone_set ( DbUtils :: getTimeZone ());
2020-11-19 22:47:44 +01:00
$currentTime = date ( 'Y-m-d H:i:s' );
2020-11-19 23:02:08 +01:00
2020-11-19 23:00:18 +01:00
$pdo = $this -> dbutils -> openDbAndReturnPdo ();
2020-11-19 23:02:08 +01:00
CommonUtils :: log ( $pdo , " QUEUE " , " Cash action with money ' $money ' at billtime ' $currentTime ' " );
2020-11-19 23:00:18 +01:00
$pdo -> beginTransaction ();
$sql = " SELECT sum(brutto) as bruttosum FROM %bill% WHERE closingid is null AND paymentid='1' " ;
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
$stmt -> execute ();
$row = $stmt -> fetchObject ();
if ( $row != null ) {
2020-11-19 22:47:44 +01:00
$sum = $row -> bruttosum ;
if ( is_null ( $sum )) {
$sum = 0.0 ;
}
if (( $sum + floatval ( $money )) >= 0.0 ) {
2020-11-19 23:00:18 +01:00
$nextbillid = $this -> testForNewBillIdAndUpdateWorkTable ( $pdo );
if ( $nextbillid < 0 ) {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_INCONSISTENT_DB , " msg " => ERROR_INCONSISTENT_DB_MSG ));
$pdo -> rollBack ();
2020-11-19 22:47:44 +01:00
}
$userId = $this -> getUserId ();
2020-11-19 23:00:18 +01:00
$commonUtils = new CommonUtils ();
2020-11-19 23:01:07 +01:00
if ( trim ( $money ) == '' ) {
$money = '0.00' ;
}
2020-11-19 23:02:08 +01:00
CommonUtils :: log ( $pdo , " QUEUE " , " Calc bill signature for cash money ' $money ' at billtime ' $currentTime ' " );
2020-11-19 22:47:44 +01:00
$signature = $commonUtils -> calcSignatureForBill ( $pdo , $currentTime , $money , $money , 0.0 , $userId );
2020-11-19 23:00:18 +01:00
$sql = " INSERT INTO `%bill%` (`id` , `billdate`,`brutto`,`netto`,`tax`,`tableid`, `status`, `paymentid`,`userid`,`ref`,`reason`,`signature`) VALUES ( ?, ? , ?,?,?, ?, 'c', ?,?,?,?,?) " ;
2020-11-19 22:47:44 +01:00
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
2020-11-19 23:00:18 +01:00
$stmt -> execute ( array ( $nextbillid , $currentTime , $money , $money , '0.00' , - 1 , 1 , $userId , NULL , $remark , $signature ));
2020-11-19 22:47:44 +01:00
2020-11-19 22:58:20 +01:00
$lastId = $pdo -> lastInsertId ();
$prevbrutto = 0 ;
$prevnetto = 0 ;
if ( $lastId > 1 ) {
$sql = " SELECT brutto,prevbrutto,netto,prevnetto FROM %bill% WHERE id=? " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $lastId - 1 ));
$row = $stmt -> fetchObject ();
}
$sql = " UPDATE %bill% SET prevbrutto=?,prevnetto=? WHERE id=? " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $row -> brutto + $row -> prevbrutto , $row -> netto + $row -> prevnetto , $lastId ));
2020-11-19 22:47:44 +01:00
$pdo -> commit ();
echo json_encode ( array ( " status " => " OK " ));
} else {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_BILL_LESS_MONEY_TO_TAKE_OUT , " msg " => ERROR_BILL_LESS_MONEY_TO_TAKE_OUT_MSG ));
}
} else {
$pdo -> rollBack ();
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_GENERAL_PAYDESK_SUM , " msg " => ERROR_GENERAL_PAYDESK_SUM_MSG ));
return ;
}
}
/*
* User may ask what money he should have in his pocket by serving the guests . If the inserts and
* take outs are in in his waiter paydesk then this value is of interest , too . Return both .
*/
function getCashOverviewOfUser () {
2020-11-19 23:02:57 +01:00
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
2020-11-19 22:47:44 +01:00
$userId = $this -> getUserId ();
2020-11-19 23:02:57 +01:00
if ( session_id () == '' ) {
session_start ();
}
$lang = $_SESSION [ 'language' ];
$paymentnameitem = " name " ;
if ( $lang == 1 ) {
$paymentnameitem = " name_en " ;
} else if ( $lang == 2 ) {
$paymentnameitem = " name_esp " ;
}
$cashPerPayments = array ();
for ( $paymentid = 1 ; $paymentid <= 8 ; $paymentid ++ ) {
$sql = " SELECT $paymentnameitem as payname FROM %payment% WHERE id=? " ;
$row = CommonUtils :: getRowSqlObject ( $pdo , $sql , array ( $paymentid ));
$paymentname = $row -> payname ;
$onlyCashByGuests = 0.0 ;
$pdo = $this -> dbutils -> openDbAndReturnPdo ();
$sql = " SELECT sum(brutto) as sumtotal FROM %bill% WHERE closingid is null AND status is null AND paymentid=? AND userid=? " ;
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
$stmt -> execute ( array ( $paymentid , $userId ));
$row = $stmt -> fetchObject ();
if ( $row != null ) {
if ( $row -> sumtotal != null ) {
$onlyCashByGuests = $row -> sumtotal ;
}
}
if ( $onlyCashByGuests != '0.00' ) {
$cashPerPayments [] = array ( " payment " => $paymentname , " value " => $onlyCashByGuests );
2020-11-19 22:47:44 +01:00
}
}
$cashByGuestsAndInsertTakeOut = 0.0 ;
$sql = " SELECT sum(brutto) as sumtotal FROM %bill% WHERE closingid is null AND paymentid='1' AND userid=' $userId ' AND (status is null OR status ='c') " ;
2020-11-19 23:00:18 +01:00
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
$stmt -> execute ();
2020-11-19 22:47:44 +01:00
$row = $stmt -> fetchObject ();
if ( $row != null ) {
2020-11-19 23:00:18 +01:00
if ( $row -> sumtotal != null ) {
2020-11-19 22:47:44 +01:00
$cashByGuestsAndInsertTakeOut = $row -> sumtotal ;
2020-11-19 23:00:18 +01:00
}
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:02:57 +01:00
echo json_encode ( array ( " cashperpayments " => $cashPerPayments , " total " => $cashByGuestsAndInsertTakeOut ));
2020-11-19 22:47:44 +01:00
}
2020-11-19 22:54:51 +01:00
function getLastBillsWithContent ( $day , $month , $year ) {
2020-11-19 22:47:44 +01:00
date_default_timezone_set ( DbUtils :: getTimeZone ());
$currentTime = date ( 'Y-m-d H:i:s' );
$startDate = " $year - $month - $day 00:00:00 " ;
$endDate = " $year - $month - $day 23:59:59 " ;
$whenClause = " (billdate >= ? AND billdate <= ?) " ;
$pdo = $this -> dbutils -> openDbAndReturnPdo ();
$admin = new Admin ();
$genValues = $admin -> getGeneralConfigItems ( false , $pdo );
$l = $genValues [ 'billlanguage' ];
$commonUtils = new CommonUtils ();
2020-11-19 23:00:18 +01:00
2020-11-19 22:55:20 +01:00
$sql = " SELECT id,billdate,brutto,tableid,closingid,status,host FROM %bill% WHERE tableid >= '0' AND $whenClause ORDER BY id DESC,billdate DESC " ;
2020-11-19 22:47:44 +01:00
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $startDate , $endDate ));
2020-11-19 23:00:18 +01:00
$result = $stmt -> fetchAll ();
2020-11-19 22:47:44 +01:00
$resultarray = array ();
foreach ( $result as $zeile ) {
$theId = $zeile [ 'id' ];
if ( ! $commonUtils -> verifyBill ( $pdo , $theId )) {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_INCONSISTENT_DB , " msg " => ERROR_INCONSISTENT_DB_MSG ));
return ;
}
date_default_timezone_set ( DbUtils :: getTimeZone ());
2020-11-19 23:00:18 +01:00
$date = new DateTime ( $zeile [ 'billdate' ]);
2020-11-19 22:47:44 +01:00
$shortdate = $date -> format ( 'H:i' );
$closingID = $zeile [ 'closingid' ];
$isClosed = ( is_null ( $closingID ) ? 0 : 1 );
2020-11-19 22:55:09 +01:00
if ( $this -> billIsCancelled ( $pdo , $theId )) {
$isClosed = 1 ;
}
2020-11-19 23:00:18 +01:00
2020-11-19 22:47:44 +01:00
$arr = array ( " id " => $theId ,
2020-11-19 23:00:18 +01:00
" longdate " => $zeile [ 'billdate' ],
" shortdate " => $shortdate ,
" brutto " => $zeile [ 'brutto' ],
2020-11-19 22:54:51 +01:00
" tablename " => $commonUtils -> getTableNameFromId ( $pdo , $zeile [ 'tableid' ]),
2020-11-19 23:00:58 +01:00
" billcontent " => $this -> getBillWithId ( $pdo , $theId , $l , 0 , true ),
2020-11-19 22:55:20 +01:00
" isClosed " => $isClosed ,
2020-11-19 23:00:18 +01:00
" host " => $zeile [ 'host' ]
2020-11-19 22:47:44 +01:00
);
2020-11-19 23:00:18 +01:00
$resultarray [] = $arr ;
}
2020-11-19 22:47:44 +01:00
$hosthtml = file_get_contents ( " ../customer/bon-bewirtungsvorlage.html " );
ob_start ();
echo json_encode ( array ( " status " => " OK " , " code " => OK , " msg " => $resultarray , " hosthtml " => $hosthtml ));
ob_end_flush ();
}
private function getUserId () {
2020-11-19 23:00:18 +01:00
if ( session_id () == '' ) {
session_start ();
2020-11-19 22:47:44 +01:00
}
return $_SESSION [ 'userid' ];
}
/**
* Test if it is allowed to insert new bill as storno bill or if manipulation has happened
*
* Returns ( - 1 ) in case of an error , a positive return value is the new id , ( which is already updated in work table )
*/
private function testForNewBillIdAndUpdateWorkTable ( $pdo ) {
$commonUtils = new CommonUtils ();
2020-11-19 23:00:18 +01:00
$sql = " SELECT MAX(id) as maxbillid FROM %bill% " ;
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
$stmt -> execute ();
$row = $stmt -> fetchObject ();
$nextbillid = intval ( $row -> maxbillid ) + 1 ;
if ( ! $commonUtils -> verifyLastBillId ( $pdo , $nextbillid )) {
return ( - 1 );
} else {
2020-11-19 22:47:44 +01:00
$commonUtils -> setLastBillIdInWorkTable ( $pdo , $nextbillid );
2020-11-19 23:00:18 +01:00
return $nextbillid ;
2020-11-19 22:47:44 +01:00
}
}
2020-11-19 23:02:19 +01:00
private function initaustriareceipt ( $pdo ) {
date_default_timezone_set ( DbUtils :: getTimeZone ());
$currentTime = date ( 'Y-m-d H:i:s' );
$pdo -> beginTransaction ();
try {
// calculate next bill id
$sql = " SELECT MAX(id) as maxid FROM %bill% " ;
$row = CommonUtils :: getRowSqlObject ( $pdo , $sql );
$maxbillid = $row -> maxid ;
$nextbillid = 1 ;
$newprevbrutto = 0 ;
$newprevnetto = 0 ;
if ( ! is_null ( $maxbillid )) {
$nextbillid = intval ( $maxbillid ) + 1 ;
2020-11-19 23:10:09 +01:00
$sql = " SELECT brutto,ROUND(netto,2) as netto,prevbrutto,prevnetto FROM %bill% WHERE id=? " ;
2020-11-19 23:02:19 +01:00
$row = CommonUtils :: getRowSqlObject ( $pdo , $sql , array ( intval ( $maxbillid )));
$newprevbrutto = $row -> prevbrutto ;
$newprevnetto = $row -> prevnetto ;
}
$commonUtils = new CommonUtils ();
$commonUtils -> setLastBillIdInWorkTable ( $pdo , $nextbillid );
$tableid = 0 ;
if ( session_id () == '' ) {
session_start ();
}
$userid = $_SESSION [ 'userid' ];
$signature = $commonUtils -> calcSignatureForBill ( $pdo , $currentTime , '0.00' , '0.00' , '0.00' , $userid );
$sql = " INSERT INTO `%bill%` (`id` , `billdate`,`brutto`,`netto`,`prevbrutto`,`prevnetto`,`tableid`,`paymentid`,`userid`,`ref`,`tax`,`host`,`reservationid`,`guestinfo`,`intguestid`,`signature`,`reason`) VALUES (?,?,?,?,?,?,?,?,?,NULL,NULL,?,?,?,?,?,?) " ;
CommonUtils :: execSql ( $pdo , $sql , array ( $nextbillid , $currentTime , '0.00' , '0.00' , $newprevbrutto , $newprevnetto , $tableid , 1 , $userid , 0 , null , null , null , $signature , 'STARTBELEG' ));
CommonUtils :: log ( $pdo , " QUEUE " , " Created bill STARTBELEG with id= $nextbillid from user $userid " );
Rksv :: doStartBeleg ( $pdo , $nextbillid , $currentTime );
} catch ( Exception $ex ) {
$pdo -> rollBack ();
return array ( " status " => " ERROR " , " msg " => $ex -> getMessage ());
}
$pdo -> commit ();
return array ( " status " => " OK " );
}
2020-11-19 22:55:20 +01:00
private function changeBillHost ( $pdo , $billid , $isNowHost ) {
$sql = " SELECT host,closingid FROM %bill% WHERE id=? " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $billid ));
$row = $stmt -> fetchObject ();
if ( $row -> host != $isNowHost ) {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_BILL_NOT_WO_HOST , " msg " => ERROR_BILL_NOT_WO_HOST_MSG ));
return ;
}
if ( ! is_null ( $row -> closingid )) {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_BILL_ALREADY_CLOSED , " msg " => ERROR_BILL_ALREADY_CLOSED_MSG ));
return ;
}
$pdo -> beginTransaction ();
$sql = " SELECT queueid FROM %billproducts% WHERE billid=? " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $billid ));
$idsOfBill = $stmt -> fetchAll ();
$ids = array ();
foreach ( $idsOfBill as $anId ) {
$ids [] = $anId [ " queueid " ];
}
2020-11-19 23:02:08 +01:00
$sql = " SELECT brutto,netto,tableid,paymentid,tax,reservationid,guestinfo,intguestid,intguestpaid FROM %bill% WHERE id=? " ;
2020-11-19 22:55:20 +01:00
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $billid ));
$row = $stmt -> fetchObject ();
2020-11-19 23:00:55 +01:00
$ok = $this -> cancelBill ( $pdo , $billid , " " , " OrderSprinter-Bewirtungseigenschaft " , false , false , false , 0 );
2020-11-19 22:55:20 +01:00
if ( ! $ok ) {
$pdo -> rollBack ();
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_BILL_CANCEL_IMOSSIBLE , " msg " => ERROR_BILL_CANCEL_IMOSSIBLE_MSG ));
return ;
}
2020-11-19 23:02:08 +01:00
$this -> recreateBill ( $pdo , $ids , $row -> brutto , $row -> netto , $row -> tableid , $row -> paymentid , $row -> tax , 1 - $isNowHost , $row -> reservationid , $row -> guestinfo , $row -> intguestid , $row -> intguestpaid );
2020-11-19 22:55:20 +01:00
$pdo -> commit ();
echo json_encode ( array ( " status " => " OK " , " code " => OK ));
}
2020-11-19 23:02:08 +01:00
function recreateBill ( $pdo , $ids_array , $brutto , $netto , $tableid , $paymentId , $tax , $host , $reservationid , $guestinfo , $intguestid , $intguestpaid ) {
2020-11-19 22:55:20 +01:00
$userid = $this -> getUserId ();
date_default_timezone_set ( DbUtils :: getTimeZone ());
$currentTime = date ( 'Y-m-d H:i:s' );
$billid = ( - 1 );
$sql = " SELECT id from %bill% ORDER BY id DESC " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ();
$numberOfIds = $stmt -> rowCount ();
$row = $stmt -> fetchObject ();
$billid = intval ( $row -> id ) + 1 ;
$commonUtils = new CommonUtils ();
$commonUtils -> setLastBillIdInWorkTable ( $pdo , $billid );
if ( is_null ( $tableid )) {
$tableid = 0 ;
}
$signature = $commonUtils -> calcSignatureForBill ( $pdo , $currentTime , $brutto , $netto , '0.00' , $userid );
2020-11-19 23:02:08 +01:00
$billInsertSql = " INSERT INTO `%bill%` (`id` , `billdate`,`brutto`,`netto`,`tableid`,`paymentid`,`userid`,`ref`,`tax`,`host`,`reservationid`,`guestinfo`,`intguestid`,`intguestpaid`,`signature`) VALUES (?,?,?,?,?,?,?,NULL,NULL,?,?,?,?,?,?) " ;
2020-11-19 22:55:20 +01:00
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $billInsertSql ));
2020-11-19 23:02:08 +01:00
$stmt -> execute ( array ( $billid , $currentTime , $brutto , $netto , $tableid , $paymentId , $userid , $host , $reservationid , $guestinfo , $intguestid , $intguestpaid , $signature ));
2020-11-19 23:00:58 +01:00
$newBillId = $pdo -> lastInsertId ();
2020-11-19 22:55:20 +01:00
for ( $i = 0 ; $i < count ( $ids_array ); $i ++ ) {
$queueid = $ids_array [ $i ];
$updateSql = " UPDATE %queue% SET paidtime=?, billid=? WHERE id=? " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $updateSql ));
$stmt -> execute ( array ( $currentTime , $billid , $queueid ));
$billProdsSql = " INSERT INTO `%billproducts%` (`queueid`,`billid`) VALUES ( ?,?) " ;
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $billProdsSql ));
$stmt -> execute ( array ( $queueid , $billid ));
}
2020-11-19 23:00:58 +01:00
Hotelinterface :: insertIntoHsin ( $pdo , $newBillId );
2020-11-19 22:55:20 +01:00
}
2020-11-19 22:47:44 +01:00
/*
* Cancel a bill - set all queue items to not paid and drop the bill entry
*/
2020-11-19 23:00:55 +01:00
private function cancelBill ( $pdo , $billid , $stornocode , $reason , $doOwnTransaction , $doEcho , $checkStornoCode , $removeproducts = 0 ) {
2020-11-19 23:00:18 +01:00
date_default_timezone_set ( DbUtils :: getTimeZone ());
2020-11-19 22:47:44 +01:00
$currentTime = date ( 'Y-m-d H:i:s' );
2020-11-19 23:10:26 +01:00
$recordaction = T_BILLSTORNO ;
if ( $removeproducts ) {
$recordaction = T_BILLSTORNOREMOVE ;
}
2020-11-19 23:00:35 +01:00
$stornocodeInDb = CommonUtils :: getConfigValue ( $pdo , 'stornocode' , null );
if ( is_null ( $stornocodeInDb )) {
2020-11-19 22:55:20 +01:00
if ( $doEcho ) {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_BILL_NOT_STORNO_CODE , " msg " => ERROR_BILL_NOT_STORNO_CODE_MSG ));
}
return false ;
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:00:18 +01:00
2020-11-19 22:55:20 +01:00
if ( $checkStornoCode ) {
if ( $stornocode != $stornocodeInDb ) {
if ( $doEcho ) {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_BILL_WRONG_STORNO_CODE , " msg " => ERROR_BILL_WRONG_STORNO_CODE_MSG ));
}
return false ;
}
2020-11-19 22:47:44 +01:00
}
if ( ! is_numeric ( $billid )) {
2020-11-19 22:55:20 +01:00
if ( $doEcho ) {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_BILL_WRONG_NUMERIC_VALUE , " msg " => ERROR_BILL_WRONG_NUMERIC_VALUE_MSG ));
}
return false ;
2020-11-19 23:00:18 +01:00
}
2020-11-19 22:47:44 +01:00
2020-11-19 23:00:18 +01:00
if ( $doOwnTransaction ) {
2020-11-19 22:55:20 +01:00
$pdo -> beginTransaction ();
}
2020-11-19 22:47:44 +01:00
2020-11-19 23:02:08 +01:00
$sql = " SELECT brutto,netto,tax,tableid,closingid,status,paymentid,reservationid,guestinfo,intguestid,intguestpaid FROM %bill% WHERE id=? " ;
2020-11-19 23:00:18 +01:00
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
2020-11-19 22:47:44 +01:00
$stmt -> execute ( array ( $billid ));
$row = $stmt -> fetchObject ();
$closingId = null ;
2020-11-19 23:00:18 +01:00
if ( $row != null ) {
2020-11-19 22:47:44 +01:00
$closingId = $row -> closingid ;
$brutto = $row -> brutto ;
$netto = $row -> netto ;
2020-11-19 23:00:18 +01:00
$tax = $row -> tax ;
$tableid = $row -> tableid ;
2020-11-19 22:47:44 +01:00
$status = $row -> status ;
$paymentid = $row -> paymentid ;
2020-11-19 23:00:58 +01:00
$reservationid = $row -> reservationid ;
$guestinfo = $row -> guestinfo ;
2020-11-19 23:02:08 +01:00
$intguestpaid = $row -> intguestpaid ;
2020-11-19 22:47:44 +01:00
}
if ( ! is_null ( $closingId ) || ( $status == 's' ) || ( $status == 'x' )) {
2020-11-19 22:55:20 +01:00
if ( $doOwnTransaction ) {
$pdo -> rollBack ();
2020-11-19 22:47:44 +01:00
}
2020-11-19 22:55:20 +01:00
if ( $doEcho ) {
if (( $status == 's' ) || ( $status == 'x' )) {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_BILL_ALREADY_CANCELLED , " msg " => ERROR_BILL_ALREADY_CANCELLED_MSG ));
} else {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_BILL_ALREADY_CLOSED , " msg " => ERROR_BILL_ALREADY_CLOSED_MSG ));
}
}
return false ;
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:02:08 +01:00
if ( ! is_null ( $intguestpaid )) {
if ( $doOwnTransaction ) {
$pdo -> rollBack ();
}
if ( $doEcho ) {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_BILL_CUSTOMER_PAID , " msg " => ERROR_BILL_CUSTOMER_PAID_MSG ));
}
return false ;
}
2020-11-19 22:47:44 +01:00
$commonUtils = new CommonUtils ();
$correct = $commonUtils -> verifyBill ( $pdo , $billid );
if ( ! $correct ) {
2020-11-19 22:55:20 +01:00
if ( $doOwnTransaction ) {
$pdo -> rollBack ();
}
if ( $doEcho ) {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_INCONSISTENT_DB , " msg " => ERROR_INCONSISTENT_DB_MSG ));
}
return false ;
2020-11-19 22:47:44 +01:00
}
$nextbillid = $this -> testForNewBillIdAndUpdateWorkTable ( $pdo );
if ( $nextbillid < 0 ) {
2020-11-19 22:55:20 +01:00
if ( $doEcho ) {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_INCONSISTENT_DB , " msg " => ERROR_INCONSISTENT_DB_MSG ));
2020-11-19 23:00:18 +01:00
}
2020-11-19 22:55:20 +01:00
if ( $doOwnTransaction ) {
$pdo -> rollBack ();
}
return false ;
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:00:18 +01:00
$sql = " SELECT id FROM %queue% WHERE billid=? " ;
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
2020-11-19 22:47:44 +01:00
$stmt -> execute ( array ( $billid ));
2020-11-19 23:00:18 +01:00
$result = $stmt -> fetchAll ();
$queueIdArray = array ();
2020-11-19 22:47:44 +01:00
foreach ( $result as $row ) {
2020-11-19 23:03:48 +01:00
if ( $removeproducts == 1 ) {
Workreceipts :: createCancelWorkReceipt ( $pdo , $row [ 'id' ]);
}
2020-11-19 23:00:18 +01:00
$queueIdArray [] = $row [ 'id' ];
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:00:55 +01:00
if ( $removeproducts == 0 ) {
$sql = " UPDATE %queue% SET paidtime=null,billid=null WHERE billid=? " ;
} else {
2020-11-19 23:03:48 +01:00
$sql = " UPDATE %queue% SET ordertime=null,paidtime=null,billid=null WHERE billid=? " ;
2020-11-19 23:00:55 +01:00
}
2020-11-19 23:10:26 +01:00
CommonUtils :: execSql ( $pdo , $sql , array ( $billid ));
2020-11-19 22:47:44 +01:00
2020-11-19 23:00:18 +01:00
$userIdOfStornoUser = $this -> getUserId ();
2020-11-19 22:47:44 +01:00
$stornval = 0.0 - floatval ( $brutto );
$stornonettoval = 0.0 - floatval ( $netto );
2020-11-19 23:00:18 +01:00
$commonUtils = new CommonUtils ();
2020-11-19 22:47:44 +01:00
$signature = $commonUtils -> calcSignatureForBill ( $pdo , $currentTime , $stornval , $stornonettoval , $tax , $userIdOfStornoUser );
2020-11-19 23:00:18 +01:00
2020-11-19 23:00:58 +01:00
$sql = " INSERT INTO `%bill%` (`id` , `billdate`,`brutto`,`netto`,`tax`,`tableid`, `status`, `paymentid`,`userid`,`ref`,`host`,`reservationid`,`guestinfo`,`signature`) VALUES (?,?,?,?,?,?, 's', ?,?,?,?,?,?,?) " ;
2020-11-19 23:00:18 +01:00
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
2020-11-19 23:00:58 +01:00
$stmt -> execute ( array ( $nextbillid , $currentTime , $stornval , $stornonettoval , $tax , $tableid , $paymentid , $userIdOfStornoUser , $billid , 0 , $reservationid , $guestinfo , $signature ));
2020-11-19 22:47:44 +01:00
$refIdOfStornoEntry = $pdo -> lastInsertId ();
2020-11-19 22:58:20 +01:00
$sql = " SELECT brutto,netto,prevbrutto,prevnetto FROM %bill% WHERE id=? " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $refIdOfStornoEntry - 1 ));
$row = $stmt -> fetchObject ();
$sql = " UPDATE %bill% set prevbrutto=?,prevnetto=? WHERE id=? " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $row -> brutto + $row -> prevbrutto + $stornval , $row -> netto + $row -> prevnetto + $stornonettoval , $refIdOfStornoEntry ));
2020-11-19 23:02:08 +01:00
$sql = " UPDATE %bill% SET status='x', closingid=null, ref=?, intguestid=?,intguestpaid=? WHERE id=? " ;
2020-11-19 23:00:18 +01:00
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
2020-11-19 23:02:08 +01:00
$stmt -> execute ( array ( $refIdOfStornoEntry , null , null , $billid ));
2020-11-19 22:47:44 +01:00
2020-11-19 22:52:25 +01:00
if ( ! is_null ( $reason ) && ( $reason != " " )) {
$sql = " UPDATE %bill% SET reason=? WHERE id=? " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $reason , $billid ));
}
2020-11-19 23:00:18 +01:00
foreach ( $queueIdArray as $aQueueid ) {
$billProdsSql = " INSERT INTO `%billproducts%` (`queueid` , `billid`) VALUES ( ?,?) " ;
2020-11-19 22:47:44 +01:00
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $billProdsSql ));
2020-11-19 23:00:18 +01:00
$stmt -> execute ( array ( $aQueueid , $refIdOfStornoEntry ));
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:10:26 +01:00
$sql = " INSERT INTO %records% (date,userid,tableid,action) VALUES(?,?,?,?) " ;
CommonUtils :: execSql ( $pdo , $sql , array ( $currentTime , $userIdOfStornoUser , $tableid , $recordaction ));
$recordid = $pdo -> lastInsertId ();
foreach ( $queueIdArray as $aQueueid ) {
$sql = " INSERT INTO %recordsqueue% (recordid,queueid) VALUES(?,?) " ;
CommonUtils :: execSql ( $pdo , $sql , array ( $recordid , $aQueueid ));
}
2020-11-19 23:00:58 +01:00
Hotelinterface :: insertIntoHsin ( $pdo , $refIdOfStornoEntry );
2020-11-19 22:55:20 +01:00
if ( $doOwnTransaction ) {
$pdo -> commit ();
}
2020-11-19 22:47:44 +01:00
2020-11-19 22:55:20 +01:00
if ( $doEcho ) {
echo json_encode ( array ( " status " => " OK " , " code " => OK ));
}
return true ;
2020-11-19 22:47:44 +01:00
}
2020-11-19 22:55:09 +01:00
private function autoBackupPdfSummary ( $remoteaccesscode ) {
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
2020-11-19 23:00:35 +01:00
$code = CommonUtils :: getConfigValue ( $pdo , 'remoteaccesscode' , null );
if ( is_null ( $code )) {
2020-11-19 22:55:09 +01:00
echo " No remote access code available - backup not allowed " ;
return ;
}
2020-11-19 23:00:35 +01:00
2020-11-19 22:55:09 +01:00
if ( is_null ( $code ) || ( trim ( $code ) == " " )) {
echo " No remote access code set - backup not allowed " ;
return ;
}
if ( $code != md5 ( $remoteaccesscode )) {
echo " Wrong remote access code used - backup not allowed " ;
return ;
}
$pdo = null ;
date_default_timezone_set ( DbUtils :: getTimeZone ());
$currentYear = date ( 'Y' );
$currentMonth = date ( 'n' );
$this -> exportPdfSummary ( 1 , $currentYear , $currentMonth , $currentYear );
}
2020-11-19 22:47:44 +01:00
private function exportPdfReport ( $startMonth , $startYear , $endMonth , $endYear ) {
$pdfExport = new PdfExport ();
2020-11-19 22:55:09 +01:00
$lang = 0 ;
if ( isset ( $_GET [ " lang " ])) {
$lang = $_GET [ 'lang' ];
}
$pdfExport -> exportPdfReport ( $lang , $startMonth , $startYear , $endMonth , $endYear );
2020-11-19 22:47:44 +01:00
}
private function exportPdfSummary ( $startMonth , $startYear , $endMonth , $endYear ) {
$pdfExport = new PdfExport ();
2020-11-19 22:55:09 +01:00
$lang = 0 ;
if ( isset ( $_GET [ " lang " ])) {
$lang = $_GET [ 'lang' ];
}
$pdfExport -> exportPdfSummary ( $lang , $startMonth , $startYear , $endMonth , $endYear );
2020-11-19 22:47:44 +01:00
}
2020-11-19 22:52:25 +01:00
private function exportCsv ( $startMonth , $startYear , $endMonth , $endYear , $exportType ) {
$this -> exportCsv_bin ( $startMonth , $startYear , $endMonth , $endYear , null , $exportType );
2020-11-19 22:47:44 +01:00
}
/*
* Method to export data of a special closing
*/
2020-11-19 22:52:25 +01:00
private function exportCsvOfClosing ( $closingid , $exportFormat ) {
$this -> exportCsv_bin ( null , null , null , null , $closingid , $exportFormat );
2020-11-19 22:47:44 +01:00
}
private function getDecPoint () {
2020-11-19 23:00:18 +01:00
$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 ();
2020-11-19 22:47:44 +01:00
return ( $row -> setting );
}
2020-11-19 22:52:25 +01:00
private function exportCsv_bin ( $startMonth , $startYear , $endMonth , $endYear , $onlyClosingId , $exportFormat ) {
if ( session_id () == '' ) {
session_start ();
}
2020-11-19 22:47:44 +01:00
$l = $_SESSION [ 'language' ];
$commonUtils = new CommonUtils ();
$currency = $commonUtils -> getCurrency ();
2020-11-19 22:52:25 +01:00
$decpoint = " . " ;
$formatCode = " 0.00 " ;
2020-11-19 22:47:44 +01:00
if ( $onlyClosingId == null ) {
if ( $startMonth < 10 ) {
$startMonth = " 0 " . $startMonth ;
}
if ( $endMonth < 10 ) {
$endMonth = " 0 " . $endMonth ;
}
$startDate = $startYear . " - " . $startMonth . " -01 00:00:00 " ;
2020-11-19 22:52:25 +01:00
$endDate = $endYear . " - " . $endMonth . " -01 " ;
2020-11-19 22:47:44 +01:00
$lastdayOfMonth = date ( " t " , strtotime ( $endDate ));
$endDate = $endYear . " - " . $endMonth . " - " . $lastdayOfMonth . " 23:59:59 " ;
}
2020-11-19 22:52:25 +01:00
$objPHPExcel = new PHPExcel ();
PHPExcel_Settings :: setZipClass ( PHPExcel_Settings :: PCLZIP );
$locale = 'De' ;
if ( $l == 1 ) {
$locale = 'En' ;
} else if ( $l == 2 ) {
$locale = 'Es' ;
}
$validLocale = PHPExcel_Settings :: setLocale ( $locale );
$objPHPExcel -> getProperties ()
-> setCreator ( " OrderSprinter " )
-> setLastModifiedBy ( $_SESSION [ 'currentuser' ])
-> setTitle ( " OrderSprinter Umsatzdatenexport " )
-> setSubject ( " OrderSprinter Umsatzdatenexport " )
-> setDescription ( " Umsätze " )
-> setKeywords ( " OrderSprinter Umsätze " )
-> setCategory ( " OrderSprinter Datenexport " );
$objWorksheet = $objPHPExcel -> getActiveSheet ();
$allcells = array ();
$firstRow = array (
$this -> t [ 'ID' ][ $l ],
$this -> t [ 'Date' ][ $l ],
$this -> t [ 'Brutto' ][ $l ] . " ( $currency ) " ,
$this -> t [ 'Netto' ][ $l ] . " ( $currency ) " ,
2020-11-19 23:10:09 +01:00
$this -> t [ 'Tablename' ][ $l ],
2020-11-19 22:52:25 +01:00
$this -> t [ 'State' ][ $l ],
$this -> t [ 'Ref' ][ $l ],
$this -> t [ 'host' ][ $l ],
$this -> t [ 'reason' ][ $l ],
$this -> t [ 'Userid' ][ $l ],
$this -> t [ 'User' ][ $l ]);
2020-11-19 22:47:44 +01:00
if ( $onlyClosingId == null ) {
2020-11-19 22:52:25 +01:00
$firstRow [] = $this -> t [ 'ClosId' ][ $l ];
$firstRow [] = $this -> t [ 'ClosDate' ][ $l ];
$firstRow [] = $this -> t [ 'PayWay' ][ $l ];
$firstRow [] = $this -> t [ 'ClosRemark' ][ $l ];
} else {
$firstRow [] = $this -> t [ 'PayWay' ][ $l ];
2020-11-19 22:47:44 +01:00
}
2020-11-19 22:52:25 +01:00
$lineLength = count ( $firstRow );
$allcells [] = $firstRow ;
2020-11-19 22:47:44 +01:00
$billIdsForThatClosing = array ();
$payment_lang = array ( " name " , " name_en " , " name_esp " );
$payment_col = $payment_lang [ $l ];
2020-11-19 22:52:25 +01:00
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
2020-11-19 23:10:09 +01:00
$sql = " SELECT DISTINCT %bill%.id,IF(tableid > '0',(SELECT tableno FROM %resttables% WHERE id=tableid),'') as tablename,%bill%.signature,billdate,brutto,ROUND(netto,2) as netto,IF(tax is not null, tax, '0.00') as tax,status,closingdate,remark,%bill%.host,%bill%.closingid,%payment%. $payment_col as payway,userid,ref,username,IF(%bill%.reason is not null,reason,'') as reason FROM %bill%,%closing%,%payment%,%user% " ;
2020-11-19 22:47:44 +01:00
$sql .= " WHERE closingid is not null AND %bill%.closingid=%closing%.id " ;
$sql .= " AND %bill%.paymentid=%payment%.id " ;
if ( $onlyClosingId == null ) {
2020-11-19 22:52:25 +01:00
$sql .= " AND %bill%.billdate BETWEEN ? AND ? " ;
2020-11-19 22:47:44 +01:00
} else {
2020-11-19 22:52:25 +01:00
$sql .= " AND closingid=? " ;
2020-11-19 22:47:44 +01:00
}
$sql .= " AND %bill%.userid = %user%.id " ;
$sql .= " ORDER BY billdate " ;
2020-11-19 22:55:20 +01:00
2020-11-19 22:52:25 +01:00
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
2020-11-19 22:47:44 +01:00
2020-11-19 22:52:25 +01:00
if ( $onlyClosingId == null ) {
$stmt -> execute ( array ( $startDate , $endDate ));
} else {
$stmt -> execute ( array ( $onlyClosingId ));
}
$dbresult = $stmt -> fetchAll ();
foreach ( $dbresult as $zeile ) {
2020-11-19 22:47:44 +01:00
$billid = $zeile [ 'id' ];
2020-11-19 23:10:09 +01:00
$tablename = $zeile [ 'tablename' ];
2020-11-19 22:47:44 +01:00
$billdate = $zeile [ 'billdate' ];
2020-11-19 22:52:25 +01:00
2020-11-19 22:47:44 +01:00
$brutto_orig = $zeile [ 'brutto' ];
$netto_orig = $zeile [ 'netto' ];
$tax_orig = $zeile [ 'tax' ];
2020-11-19 22:52:25 +01:00
2020-11-19 22:47:44 +01:00
$brutto = str_replace ( " . " , $decpoint , $brutto_orig );
$netto = str_replace ( " . " , $decpoint , $netto_orig );
$tax = str_replace ( " . " , $decpoint , $tax_orig );
$signature = $zeile [ 'signature' ];
2020-11-19 23:03:29 +01:00
$dbstatus = $zeile [ 'status' ];
2020-11-19 22:47:44 +01:00
$status = $zeile [ 'status' ];
2020-11-19 22:52:25 +01:00
if ( $status == 'x' ) {
$status = $this -> t [ " laterCancelled " ][ $l ];
} else if ( $status == 's' ) {
$status = $this -> t [ " storno " ][ $l ];
} else if ( $status == 'c' ) {
2020-11-19 22:47:44 +01:00
$status = $this -> t [ " cashact " ][ $l ];
} else {
$status = " " ;
}
$ref = ( $zeile [ 'ref' ] == null ? " " : $zeile [ 'ref' ]);
$userid = $zeile [ 'userid' ];
$username = $zeile [ 'username' ];
$closingid = $zeile [ 'closingid' ];
$closingdate = $zeile [ 'closingdate' ];
2020-11-19 22:52:25 +01:00
$remark = $zeile [ 'remark' ];
$paymentname = $zeile [ 'payway' ];
2020-11-19 22:47:44 +01:00
$host = ( $zeile [ 'host' ] == 1 ? " x " : " - " );
2020-11-19 22:52:25 +01:00
$reason = $zeile [ 'reason' ];
2020-11-19 23:03:29 +01:00
if ( ! $commonUtils -> verifyBillByValues ( null , $billdate , $brutto_orig , $netto_orig , $tax_orig , $userid , $signature , $dbstatus )) {
2020-11-19 22:47:44 +01:00
echo " Inconsistent Data Base Content! \n " ;
return ;
}
2020-11-19 22:52:25 +01:00
2020-11-19 22:47:44 +01:00
if ( $billid == null ) {
$billid = " - " ;
}
2020-11-19 22:52:25 +01:00
2020-11-19 22:47:44 +01:00
if ( $onlyClosingId == null ) {
2020-11-19 23:10:09 +01:00
$line = array ( $billid , $billdate , $brutto , $netto , $tablename , $status , $ref , $host , $reason , $userid , $username , $closingid , $closingdate , $paymentname , $remark );
2020-11-19 22:47:44 +01:00
} else {
2020-11-19 23:10:09 +01:00
$line = array ( $billid , $billdate , $brutto , $netto , $tablename , $status , $ref , $host , $reason , $userid , $username , $paymentname );
2020-11-19 22:52:25 +01:00
}
$allcells [] = $line ;
}
$objWorksheet -> fromArray (
$allcells , // The data to set
NULL , // Array values with this value will not be set
'A1' // Top left coordinate of the worksheet range where
);
$lastChar = chr ( ord ( 'A' ) + $lineLength - 1 );
$range = " A1: $lastChar " . " 1 " ;
$objWorksheet -> getStyle ( $range ) -> getFill ()
-> setFillType ( PHPExcel_Style_Fill :: FILL_SOLID )
-> getStartColor () -> setARGB ( 'FFadf6aa' );
$range = " A2: " . $lastChar . count ( $allcells );
$objWorksheet -> getStyle ( $range ) -> getFill ()
-> setFillType ( PHPExcel_Style_Fill :: FILL_SOLID )
-> getStartColor () -> setARGB ( 'FFd6edf8' );
for ( $i = 1 ; $i < count ( $allcells ); $i ++ ) {
$aVal = $objWorksheet -> getCell ( 'C' . ( $i + 1 )) -> getValue ();
$objWorksheet -> getCell ( 'C' . ( $i + 1 )) -> setValueExplicit ( $aVal , PHPExcel_Cell_DataType :: TYPE_NUMERIC );
$objWorksheet -> getStyle ( 'C' . ( $i + 1 )) -> getNumberFormat () -> setFormatCode ( $formatCode );
$aVal = $objWorksheet -> getCell ( 'D' . ( $i + 1 )) -> getValue ();
$objWorksheet -> getCell ( 'D' . ( $i + 1 )) -> setValueExplicit ( $aVal , PHPExcel_Cell_DataType :: TYPE_NUMERIC );
$objWorksheet -> getStyle ( 'D' . ( $i + 1 )) -> getNumberFormat () -> setFormatCode ( $formatCode );
}
if ( $exportFormat == DO_CSV ) {
header ( " Content-type: text/x-csv " );
header ( " Content-Disposition: attachment; filename= \" ordersprinter-datenexport.csv \" " );
header ( " Cache-Control: max-age=0 " );
$objWriter = new PHPExcel_Writer_CSV ( $objPHPExcel );
2020-11-19 22:58:27 +01:00
$objWriter -> setDelimiter ( ';' );
2020-11-19 22:52:25 +01:00
} else {
header ( " Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet " );
2020-11-19 22:58:12 +01:00
header ( " Content-Disposition: attachment; filename= \" ordersprinter-datenexport.xls \" " );
2020-11-19 22:52:25 +01:00
header ( " Cache-Control: max-age=0 " );
$objWriter = PHPExcel_IOFactory :: createWriter ( $objPHPExcel , 'Excel5' );
}
$objWriter -> save ( " php://output " );
$objPHPExcel -> disconnectWorksheets ();
unset ( $objPHPExcel );
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:10:09 +01:00
}