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 23:14:48 +01:00
require_once ( '3rdparty/phpqrcode.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
2020-11-19 23:12:32 +01:00
private static $daynamesStartSunday = array (
array ( " Sonntag " , " Sunday " , " Domingo " ),
array ( " Montag " , " Monday " , " Lunes " ),
array ( " Dienstag " , " Tuesday " , " Martes " ),
array ( " Mittwoch " , " Wednesday " , " Miércoles " ),
array ( " Donnerstag " , " Thursday " , " Jueves " ),
array ( " Freitag " , " Friday " , " Viernes " ),
array ( " Samstag " , " Saturday " , " Sábado " )
);
2020-11-19 23:14:48 +01:00
private static $CASHTYPE_Privatentnahme = array ( " value " => 1 , " name " => " Privatentnahme " );
private static $CASHTYPE_Privateinlage = array ( " value " => 2 , " name " => " Privateinlage " );
private static $CASHTYPE_Geldtransit = array ( " value " => 3 , " name " => " Geldtransit " );
private static $CASHTYPE_Lohnzahlung = array ( " value " => 4 , " name " => " Lohnzahlung " );
private static $CASHTYPE_Einzahlung = array ( " value " => 5 , " name " => " Einzahlung " );
private static $CASHTYPE_Auszahlung = array ( " value " => 6 , " name " => " Auszahlung " );
private static $CASHTYPE_TrinkgeldAN = array ( " value " => 7 , " name " => " Trinkgeld an Arb.nehmer " );
private static $CASHTYPE_TrinkgeldAG = array ( " value " => 8 , " name " => " Trinkgeld an Arb.geber " );
2020-11-19 23:15:07 +01:00
private static $TSE_FORMAT_QR_TEXT = 1 ;
private static $TSE_FORMAT_ASSOC_ARRAY = 2 ;
2020-11-19 23:14:48 +01:00
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 ) {
2020-11-19 23:12:32 +01:00
$cmdsThatNeedAdminOrManagerRights = array ( 'exportCsv' , 'exportAllCsv' , 'exportAllExcel' , 'exportXlsx' , 'exportPdfReport' , 'exportPdfReportClosPeriod' ,
2020-11-19 23:12:59 +01:00
'exportPdfSummary' , 'exportPdfSummaryClosPeriod' , 'exportPdfSummaryClosPeriod' );
2020-11-19 23:12:32 +01:00
if ( in_array ( $command , $cmdsThatNeedAdminOrManagerRights )) {
if ( ! $this -> hasCurrentUserAdminOrManagerRights ()) {
2020-11-19 22:52:25 +01:00
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_BILL_NOT_AUTHOTRIZED , " msg " => ERROR_BILL_NOT_AUTHOTRIZED_MSG ));
2020-11-19 23:12:32 +01:00
return ;
2020-11-19 22:52:25 +01:00
}
2020-11-19 23:12:32 +01:00
}
2020-11-19 23:14:48 +01:00
if ( $command == 'billqrcode' ) {
return self :: outputBillQrCode ( $_GET [ " billid " ]);
}
2020-11-19 23:12:32 +01:00
if ( $command == 'exportCsv' ) {
$this -> exportCsv ( $_GET [ 'startMonth' ], $_GET [ 'startYear' ], $_GET [ 'endMonth' ], $_GET [ 'endYear' ], DO_CSV );
2020-11-19 22:52:25 +01:00
return ;
}
2020-11-19 23:12:24 +01:00
if ( $command == 'exportAllCsv' ) {
2020-11-19 23:12:32 +01:00
$this -> exportAllCsvOrExcel ( $_GET [ 'startMonth' ], $_GET [ 'startYear' ], $_GET [ 'endMonth' ], $_GET [ 'endYear' ], DO_CSV );
2020-11-19 23:12:24 +01:00
return ;
}
if ( $command == 'exportAllExcel' ) {
2020-11-19 23:12:32 +01:00
$this -> exportAllCsvOrExcel ( $_GET [ 'startMonth' ], $_GET [ 'startYear' ], $_GET [ 'endMonth' ], $_GET [ 'endYear' ], DO_EXCEL );
2020-11-19 23:12:24 +01:00
return ;
}
2020-11-19 22:52:25 +01:00
if ( $command == 'exportXlsx' ) {
2020-11-19 23:12:32 +01:00
$this -> exportCsv ( $_GET [ 'startMonth' ], $_GET [ 'startYear' ], $_GET [ 'endMonth' ], $_GET [ 'endYear' ], DO_EXCEL );
2020-11-19 22:47:44 +01:00
return ;
}
if ( $command == 'exportPdfReport' ) {
2020-11-19 23:12:32 +01:00
$this -> exportPdfReport ( $_GET [ 'startMonth' ], $_GET [ 'startYear' ], $_GET [ 'endMonth' ], $_GET [ 'endYear' ], null , null );
return ;
}
if ( $command == 'exportPdfReportClosPeriod' ) {
$this -> exportPdfReport ( 0 , 0 , 0 , 0 , $_GET [ 'closidstart' ], $_GET [ 'closidend' ]);
2020-11-19 22:47:44 +01:00
return ;
}
if ( $command == 'exportPdfSummary' ) {
2020-11-19 23:12:32 +01:00
$this -> exportPdfSummary ( $_GET [ 'startMonth' ], $_GET [ 'startYear' ], $_GET [ 'endMonth' ], $_GET [ 'endYear' ]);
return ;
}
if ( $command == 'exportPdfSummaryClosPeriod' ) {
$this -> exportPdfSummaryClosPeriod ( $_GET [ 'closidstart' ], $_GET [ 'closidend' ]);
2020-11-19 22:47:44 +01:00
return ;
}
2020-11-19 23:12:59 +01:00
if ( $command == 'exportCsvSummaryClosPeriod' ) {
$this -> exportCsvSummaryClosPeriod ( $_GET [ 'closidstart' ], $_GET [ 'closidend' ]);
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 = " " ;
2020-11-19 23:14:21 +01:00
$cashtype = 1 ;
2020-11-19 23:00:18 +01:00
if ( isset ( $_POST [ " remark " ])) {
$remark = $_POST [ 'remark' ];
}
2020-11-19 23:14:21 +01:00
if ( isset ( $_POST [ " cashtype " ])) {
$cashtype = $_POST [ 'cashtype' ];
}
self :: doCashAction ( $_POST [ 'money' ], $remark , $cashtype );
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 );
2020-11-19 23:12:22 +01:00
return ;
2020-11-19 23:02:19 +01:00
} else {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_MANAGER_NOT_AUTHOTRIZED , " msg " => ERROR_MANAGER_NOT_AUTHOTRIZED_MSG ));
}
return ;
}
2020-11-19 23:12:22 +01:00
if ( $command == 'initCardPayment' ) {
if ( $this -> hasCurrentUserPaydeskRights ()) {
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
$retVal = $this -> initCardPayment ( $pdo , $_POST [ 'billid' ]);
echo json_encode ( $retVal );
return ;
} else {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_PAYDESK_NOT_AUTHOTRIZED , " msg " => ERROR_PAYDESK_NOT_AUTHOTRIZED_MSG ));
return false ;
}
return ;
}
if ( $command == 'cancelCardPayment' ) {
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
$retVal = $this -> cancelCardPayment ( $pdo , $_POST [ 'billid' ], $_POST [ 'randvalue' ]);
echo json_encode ( $retVal );
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' ]);
2020-11-19 23:12:32 +01:00
return ;
2020-11-19 22:47:44 +01:00
} else if ( $command == 'cancelBill' ) {
2020-11-19 22:55:20 +01:00
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
2020-11-19 23:12:48 +01:00
$guestAssignedAndPaid = self :: isBillAssignedToGuestAndPaid ( $pdo , $_POST [ 'billid' ]);
if ( ! is_null ( $guestAssignedAndPaid )) {
$msg = " Rechnung ist dem Gast ' $guestAssignedAndPaid ' zugewiesen und als bezahlt deklariert. Bezahlstatus muss vor einem Bonstorno in der Gastansicht geändert werden. " ;
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_BILL_GUEST_ASSIGNED_AND_PAID , " msg " => $msg , " customer " => $guestAssignedAndPaid ));
return ;
}
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 23:12:32 +01:00
return ;
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 23:14:48 +01:00
// This method is for bills which are not from type CASH! -> do the rest also in other method
public static function signOrdersBill ( $pdo , $billid , $isCancellation ) {
$sign = " " ;
if ( $isCancellation ) {
$sign = " 0- " ;
}
$sqlNormTax = " SELECT FORMAT( $sign COALESCE(SUM(price),'0.00'),2) as sumpriceforcertaintax FROM %queue% Q,%billproducts% BP WHERE BP.billid=? AND Q.id=BP.queueid and Q.taxkey in ('1','11','21') " ;
$resultNormTax = CommonUtils :: fetchSqlAll ( $pdo , $sqlNormTax , array ( $billid ));
$normTaxSum = $resultNormTax [ 0 ][ 'sumpriceforcertaintax' ];
$sqlErmTax = " SELECT FORMAT( $sign COALESCE(SUM(price),'0.00'),2) as sumpriceforcertaintax FROM %queue% Q,%billproducts% BP WHERE BP.billid=? AND Q.id=BP.queueid and Q.taxkey in ('2','12','22') " ;
$resultErmTax = CommonUtils :: fetchSqlAll ( $pdo , $sqlErmTax , array ( $billid ));
$ermTaxSum = $resultErmTax [ 0 ][ 'sumpriceforcertaintax' ];
$sqlNullTax = " SELECT FORMAT( $sign COALESCE(SUM(price),'0.00'),2) as sumpriceforcertaintax FROM %queue% Q,%billproducts% BP WHERE BP.billid=? AND Q.id=BP.queueid and Q.taxkey in ('5') " ;
$resultNullTax = CommonUtils :: fetchSqlAll ( $pdo , $sqlNullTax , array ( $billid ));
$nullTaxSum = $resultNullTax [ 0 ][ 'sumpriceforcertaintax' ];
$signStr = " Beleg^ " . implode ( '_' , array ( $normTaxSum , $ermTaxSum , '0.00' , '0.00' , $nullTaxSum )) . " ^ " ;
$sql = " SELECT brutto,paymentid FROM %bill% WHERE id=? " ;
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $billid ))[ 0 ];
$brutto = $result [ " brutto " ];
$paymentid = $result [ " paymentid " ];
$paymentSign = " Bar " ;
if ( $paymentid != 1 ) {
$paymentSign = " Unbar " ;
}
$currency = CommonUtils :: getConfigValue ( $pdo , 'currency' , 'EUR' );
$curSign = " " ;
if ( ! in_array ( strtoupper ( $currency ), array ( " EURO " , " EUR " , " E " , " € " ))) {
$curSign = " : " . $currency ;
}
$signStr .= $brutto . " : " . $paymentSign . $curSign ;
return self :: signValueByTseAndUpdateBill ( $pdo , $billid , $signStr );
}
private static function signValueByTseAndUpdateBill ( $pdo , $billid , $valueToSign ) {
2020-11-19 23:15:07 +01:00
$tseAnswer = TSE :: sendNormalBillToTSE ( $pdo , $valueToSign );
if ( $tseAnswer [ " status " ] != " OK " ) {
2020-11-19 23:14:48 +01:00
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 ;
2020-11-19 23:15:10 +01:00
$serialNoRef = null ;
$certificateRef = null ;
2020-11-19 23:14:48 +01:00
2020-11-19 23:15:07 +01:00
if ( $tseAnswer [ " usetse " ] == DbUtils :: $TSE_OK ) {
$logtime = $tseAnswer [ " logtime " ];
$trans = $tseAnswer [ " trans " ];
$sigcounter = $tseAnswer [ " sigcounter " ];
$tseSignature = $tseAnswer [ " signature " ];
$sigalgRef = CommonUtils :: referenceValueInTseValuesTable ( $pdo , $tseAnswer [ " sigalg " ]);
$pubkeyRef = CommonUtils :: referenceValueInTseValuesTable ( $pdo , $tseAnswer [ " publickey " ]);
$serialNoRef = CommonUtils :: referenceValueInTseValuesTable ( $pdo , $tseAnswer [ " serialno " ]);
$certificateRef = CommonUtils :: referenceValueInTseValuesTable ( $pdo , $tseAnswer [ " certificate " ]);
2020-11-19 23:14:48 +01:00
}
$opid = Operations :: createOperation (
$pdo ,
DbUtils :: $PROCESSTYPE_BELEG ,
DbUtils :: $OPERATION_IN_BILL_TABLE ,
$logtime ,
$trans ,
$valueToSign ,
$tseSignature ,
$pubkeyRef ,
$sigalgRef ,
$serialNoRef ,
$certificateRef ,
$sigcounter ,
2020-11-19 23:15:07 +01:00
$tseAnswer [ " usetse " ]);
2020-11-19 23:14:48 +01:00
$sql = " UPDATE %bill% SET opid=?,logtime=? WHERE id=? " ;
CommonUtils :: execSql ( $pdo , $sql , array ( $opid , $logtime , $billid ));
return array ( " status " => " OK " );
}
}
2020-11-19 23:12:43 +01:00
public static function isBillPaidByCard ( $pdo , $billid ) {
$sql = " SELECT paymentid FROM %bill% WHERE id=? " ;
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $billid ));
if ( count ( $result ) > 0 ) {
$paymentid = $result [ 0 ][ " paymentid " ];
if (( $paymentid == 2 ) || ( $paymentid == 3 )) {
return true ;
} else {
return false ;
}
}
return false ;
}
2020-11-19 23:12:22 +01:00
public function initCardPayment ( $pdo , $billid ) {
$randval = md5 ( rand ( 0 , getrandmax ()));
$sql = " INSERT INTO %work% (item,value,signature) VALUES(?,?,?) " ;
CommonUtils :: execSql ( $pdo , $sql , array ( " sumuphash " , $billid , $randval ));
return array ( " status " => " OK " , " msg " => $randval );
}
public function cancelCardPayment ( $pdo , $billid , $randVal ) {
$sql = " SELECT count(id) as countid FROM %work% WHERE item=? AND value=? AND signature=? " ;
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( " sumuphash " , $billid , $randVal ));
$countid = $result [ 0 ][ " countid " ];
if ( $countid != 1 ) {
return array ( " status " => " ERROR " , " msg " => " Keine Authorisierung zum Stornieren der Rechnung " );
} else {
$failuretext = CommonUtils :: getConfigValue ( $pdo , " sumupfailuretext " , " " );
$ok = $this -> cancelBill ( $pdo , $billid , '' , $failuretext , true , false , false , 0 );
if ( $ok ) {
$sql = " DELETE FROM %work% WHERE item=? AND value=? AND signature=? " ;
CommonUtils :: execSql ( $pdo , $sql , array ( " sumuphash " , $billid , $randVal ));
return array ( " status " => " OK " );
} else {
return array ( " status " => " ERROR " , " msg " => " Stornierung fehlgeschlagen " );
}
}
}
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 23:15:07 +01:00
private function createBillOverallInfo ( $billid , $billuid , $thetimedate , $tablename , $brutto , $netto , $username , $userid , $printer , $host , $masterData , $tseInfo ) {
2020-11-19 23:11:29 +01:00
$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 " ;
2020-11-19 23:14:48 +01:00
$hosttext = " " ;
if ( $host == 1 ) {
$hosttext = $masterData [ " hosttext " ];
}
2020-11-19 23:11:29 +01:00
$billoverallinfo = array (
" id " => $billid ,
2020-11-19 23:15:07 +01:00
" billuid " => $billuid ,
2020-11-19 23:11:29 +01:00
" billdate " => $thetimedate ,
" billday " => $day ,
" billmonth " => $month ,
" billyear " => $year ,
" billhour " => $hour ,
" billmin " => $min ,
" brutto " => $brutto ,
" netto " => $netto ,
" table " => $tablename ,
" username " => $username ,
2020-11-19 23:14:48 +01:00
" userid " => $userid ,
2020-11-19 23:11:29 +01:00
" printer " => $printer ,
2020-11-19 23:14:10 +01:00
" host " => $host ,
" sn " => $masterData [ " sn " ],
" uid " => $masterData [ " uid " ],
" version " => $masterData [ " version " ],
" companyinfo " => $masterData [ " companyinfo " ],
2020-11-19 23:14:48 +01:00
" systemid " => $masterData [ " systemid " ],
" dsfinvk_name " => $masterData [ " dsfinvk_name " ],
" dsfinvk_street " => $masterData [ " dsfinvk_street " ],
" dsfinvk_postalcode " => $masterData [ " dsfinvk_postalcode " ],
" dsfinvk_city " => $masterData [ " dsfinvk_city " ],
" dsfinvk_country " => $masterData [ " dsfinvk_country " ],
" dsfinvk_stnr " => $masterData [ " dsfinvk_stnr " ],
" dsfinvk_ustid " => $masterData [ " dsfinvk_ustid " ],
" template " => $masterData [ " template " ],
" hospitality " => $hosttext
2020-11-19 23:11:29 +01:00
);
2020-11-19 23:15:07 +01:00
if ( is_null ( $tseInfo )) {
$billoverallinfo [ " tsestatus " ] = 0 ;
} else {
$billoverallinfo [ " tsestatus " ] = 1 ;
$billoverallinfo [ " tseserialno " ] = $tseInfo [ " tseserialno " ];
$billoverallinfo [ " transnumber " ] = $tseInfo [ " transnumber " ];
$billoverallinfo [ " sigcounter " ] = $tseInfo [ " sigcounter " ];
$billoverallinfo [ " startlogtime " ] = $tseInfo [ " startlogtime " ];
$billoverallinfo [ " logtime " ] = $tseInfo [ " logtime " ];
$billoverallinfo [ " sigalg " ] = $tseInfo [ " sigalg " ];
$billoverallinfo [ " logtimeformat " ] = $tseInfo [ " logtimeformat " ];
$billoverallinfo [ " tsesignature " ] = $tseInfo [ " tsesignature " ];
$billoverallinfo [ " pubkey " ] = $tseInfo [ " pubkey " ];
}
2020-11-19 23:11:29 +01:00
return $billoverallinfo ;
}
2020-11-19 23:14:48 +01:00
2020-11-19 23:11:29 +01:00
private function createBillTranslations ( $language ) {
$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 ]
);
return $billtranslations ;
}
2020-11-19 23:14:48 +01:00
public function getCashBillTicket ( $pdo , $billid ) {
$readableCashType = self :: getTranslateSqlCashTypeToReadable ();
$sql = " SELECT CONCAT('V-',billuid) as billuid,username,U.id as userid,billdate,REPLACE(brutto,'.',(SELECT setting from %config% where name='decpoint')) as brutto,reason, $readableCashType FROM %user% U,%bill% B WHERE B.userid=U.id and B.id=? " ;
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $billid ));
$billuid = $result [ 0 ][ 'billuid' ];
$username = $result [ 0 ][ 'username' ];
$userid = $result [ 0 ][ 'userid' ];
$thetimedate = $result [ 0 ][ 'billdate' ];
$brutto = $result [ 0 ][ 'brutto' ];
$netto = $brutto ;
$masterData = self :: getBillMasterDataAtBillTime ( $pdo , $billid , 'cashtemplate' );
2020-11-19 23:15:07 +01:00
$tseInfo = self :: getTseInfoOfBill ( $pdo , $billid , self :: $TSE_FORMAT_ASSOC_ARRAY );
$ticket = $this -> createBillOverallInfo ( $billid , $billuid , $thetimedate , '' , $brutto , $netto , $username , $userid , '' , 0 , $masterData , $tseInfo );
2020-11-19 23:14:48 +01:00
$ticket [ 'reason' ] = $result [ 0 ][ 'reason' ];
$ticket [ 'cashtype' ] = $result [ 0 ][ 'cashtype' ];
return $ticket ;
}
public function getBillWithIdAsTicket ( $pdo , $billid ) {
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 ;
}
2020-11-19 23:14:48 +01:00
$decpoint = CommonUtils :: getConfigValue ( $pdo , 'decpoint' , " . " );
2020-11-19 22:47:44 +01:00
2020-11-19 23:11:29 +01:00
$sql = " SELECT tableid,status FROM %bill% WHERE id=? " ;
$row = CommonUtils :: getRowSqlObject ( $pdo , $sql , array ( $billid ));
if ( $row -> status == 'c' ) {
2020-11-19 23:14:48 +01:00
return $this -> getCashBillTicket ( $pdo , $billid );
2020-11-19 23:11:29 +01:00
}
2020-11-19 23:14:48 +01:00
2020-11-19 22:47:44 +01:00
$sql = " SELECT count(id) as countid FROM %queue% WHERE billid=? " ;
2020-11-19 23:11:29 +01:00
$qrow = CommonUtils :: getRowSqlObject ( $pdo , $sql , array ( $billid ));
2020-11-19 22:47:44 +01:00
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:15:07 +01:00
$sql = " SELECT DISTINCT opid,IF(status='c',CONCAT('V-',billuid),billuid) as billuid,billdate,brutto,ROUND(netto,2) as netto,'-' as tablename,username,%user%.id as userid,host,COALESCE(%bill%.status,'') as status,guestinfo,paymentid,printextras 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:15:07 +01:00
$sql = " SELECT DISTINCT opid,IF(status='c',CONCAT('V-',billuid),billuid) as billuid,billdate,brutto,ROUND(netto,2) as netto,tableno as tablename,username,%user%.id as userid,host,COALESCE(%bill%.status,'') as status,guestinfo,paymentid,printextras 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:15:07 +01:00
$sql = " SELECT DISTINCT opid,IF(status='c',CONCAT('V-',billuid),billuid) as billuid,billdate,brutto,ROUND(netto,2) as netto,'-' as tablename,username,%user%.id as userid,host,COALESCE(%bill%.status,'') as status,guestinfo,paymentid,printextras 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:15:07 +01:00
$sql = " SELECT DISTINCT opid,IF(status='c',CONCAT('V-',billuid),billuid) as billuid,billdate,brutto,ROUND(netto,2) as netto,tableno as tablename,username,%user%.id as userid,host,COALESCE(%bill%.status,'') as status,guestinfo,paymentid,printextras 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
}
}
2020-11-19 23:11:29 +01:00
$row = CommonUtils :: getRowSqlObject ( $pdo , $sql , array ( $billid ));
2020-11-19 23:15:07 +01:00
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 " ;
2020-11-19 23:11:29 +01:00
$trow = CommonUtils :: getRowSqlObject ( $pdo , $sql , array ( $tableid ));
2020-11-19 23:10:46 +01:00
if ( is_null ( $trow -> abbreviation ) || ( $trow -> abbreviation == '' )) {
2020-11-19 22:55:09 +01:00
$tablename = $row -> tablename ;
} else {
$tablename = $trow -> abbreviation . " - " . $row -> tablename ;
}
} else {
$tablename = " - " ;
}
2020-11-19 22:47:44 +01:00
if ( $row == null ) {
2020-11-19 23:14:48 +01:00
echo json_encode ( array ( " job " => array ()));
2020-11-19 22:47:44 +01:00
return ;
} else {
if ( is_null ( $row -> host )) {
$host = 0 ; // default
} else {
$host = $row -> host ;
}
2020-11-19 23:14:48 +01:00
2020-11-19 23:15:07 +01:00
$tseInfo = self :: getTseInfoOfBill ( $pdo , $billid , self :: $TSE_FORMAT_ASSOC_ARRAY );
2020-11-19 22:47:44 +01:00
$thetimedate = $row -> billdate ;
2020-11-19 23:10:46 +01:00
$printextrasOfReceipt = $row -> printextras ;
2020-11-19 23:14:48 +01:00
$billuid = $row -> billuid ;
2020-11-19 23:10:46 +01:00
$printExtras = false ;
if ( $printextrasOfReceipt == 1 ) {
$printExtras = true ;
}
2020-11-19 23:14:48 +01:00
$masterData = self :: getBillMasterDataAtBillTime ( $pdo , $billid , 'rectemplate' );
// str_replace(".",$decpoint,$netto)
$overallBrutto = str_replace ( " . " , $decpoint , $row -> brutto );
$overallNetto = str_replace ( " . " , $decpoint , $row -> netto );
2020-11-19 23:15:07 +01:00
$billoverallinfo = $this -> createBillOverallInfo ( $billid , $billuid , $thetimedate , $tablename , $overallBrutto , $overallNetto , $row -> username , $row -> userid , 0 , $host , $masterData , $tseInfo );
2020-11-19 22:47:44 +01:00
2020-11-19 23:14:48 +01:00
// rem* guestinfo
if ( is_null ( $row -> guestinfo )) {
$billoverallinfo [ " guestinfo " ] = '' ;
} else {
$billoverallinfo [ " guestinfo " ] = $row -> guestinfo ;
2020-11-19 23:00:58 +01:00
}
2020-11-19 23:02:24 +01:00
2020-11-19 23:14:48 +01:00
// rem* PAYMENT INFO
$col = " name " ;
2020-11-19 23:15:07 +01:00
2020-11-19 23:14:48 +01:00
$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
2020-11-19 23:14:48 +01:00
$result = self :: getBillProductsInCumulatedOrder ( $pdo , $billid );
$prodarray = array ();
foreach ( $result as $zeile ) {
$productname = $zeile [ 'productname' ];
if ( $zeile [ " togo " ] == 1 ) {
$productname = " To-Go: " . $productname ;
}
$pricelevel = $zeile [ 'pricelevelname' ];
if ( $pricelevel != " A " ) {
$productname .= " ( " . $pricelevel . " ) " ;
}
$prodarray [] = array ( " count " => $zeile [ 'count' ],
" productname " => $productname ,
" pricelevel " => $zeile [ 'pricelevelname' ],
" price " => $sign . str_replace ( " . " , $decpoint , $zeile [ 'price' ]),
" total " => $sign . str_replace ( " . " , $decpoint , $zeile [ 'total' ])
);
if ( $printExtras ) {
$extrasConcatStr = $zeile [ 'concatstr' ];
if ( ! is_null ( $extrasConcatStr )) {
$singleExtras = explode ( ',' , $extrasConcatStr );
$printextraprice = CommonUtils :: getConfigValue ( $pdo , 'printextraprice' , 1 );
$sql = " SELECT name FROM %extras% WHERE id=? " ;
if ( $printextraprice == 1 ) {
$decpoint = CommonUtils :: getConfigValue ( $pdo , 'decpoint' , ';' );
if (( $decpoint != '.' ) && ( $decpoint != ',' )) {
$decpoint = ',' ;
}
$sql = " SELECT CONCAT(name,' (',REPLACE(price,'.',' $decpoint '),')') as name FROM %extras% WHERE id=? " ;
}
foreach ( $singleExtras as $aSingleExtra ) {
$singleExtraParts = explode ( '-' , $aSingleExtra );
$amount = $singleExtraParts [ 0 ];
$singleExtraId = $singleExtraParts [ 1 ];
$extraNameRes = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $singleExtraId ));
if ( count ( $extraNameRes ) > 0 ) {
$extraText = $amount . " x " . $extraNameRes [ 0 ][ " name " ];
$prodarray [] = array ( " count " => ' ' ,
" productname " => ' + ' . $extraText ,
" pricelevel " => $zeile [ 'pricelevelname' ],
" price " => ' ' ,
" total " => ' '
);
}
}
}
}
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:14:48 +01:00
$taxSql = " tax " ;
$mwstSql = " concat(' $sign ',round(sum(price) - sum(price / (1.0 + tax/100.0)),2)) " ;
$nettoSql = " concat(' $sign ',round(sum(price / (1.0 + tax/100.0)),2)) " ;
$bruttoSql = " concat(' $sign ',sum(price)) " ;
if (( $decpoint == '.' ) || ( $decpoint == ',' )) {
// REPLACE("XYZ FGH XYZ", "X", "M")
$taxSql = " REPLACE( $taxSql ,'.',' $decpoint ') " ;
$mwstSql = " REPLACE( $mwstSql ,'.',' $decpoint ') " ;
$nettoSql = " REPLACE( $nettoSql ,'.',' $decpoint ') " ;
$bruttoSql = " REPLACE( $bruttoSql ,'.',' $decpoint ') " ;
}
$sql = " select $taxSql as tax, $mwstSql as mwst, $nettoSql as netto, $bruttoSql as brutto FROM %queue%,%billproducts% WHERE %billproducts%.billid=? AND %billproducts%.queueid=%queue%.id group by tax ORDER BY tax " ;
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $billid ));
2020-11-19 22:47:44 +01:00
2020-11-19 23:14:48 +01:00
$billoverallinfo [ " products " ] = $prodarray ;
$billoverallinfo [ " taxes " ] = $result ;
return $billoverallinfo ;
}
public static function setPositionsOfProductsOnBillProductsTable ( $pdo , $billid ) {
$billarticles = self :: getBillProductsInCumulatedOrder ( $pdo , $billid );
$pos = 1 ;
foreach ( $billarticles as $billentry ) {
$queueids = $billentry [ " queueids " ];
$queueidsArr = explode ( ',' , $queueids );
$sql = " UPDATE %billproducts% BP SET position=? WHERE billid=? AND queueid=? " ;
foreach ( $queueidsArr as $aQueueid ) {
CommonUtils :: execSql ( $pdo , $sql , array ( $pos , $billid , $aQueueid ));
}
$pos ++ ;
}
}
private static function getBillProductsInCumulatedOrder ( $pdo , $billid ) {
$billprintextrasSql = CommonUtils :: fetchSqlAll ( $pdo , " SELECT printextras FROM %bill% WHERE id=? " , array ( $billid ));
$printExtras = $billprintextrasSql [ 0 ][ " printextras " ];
2020-11-19 23:12:35 +01:00
$unit = CommonUtils :: caseOfSqlUnitSelection ( $pdo );
2020-11-19 23:14:48 +01:00
$sql = " select CONCAT( $unit ,productname) as productname,price,(count(Q.productname) * price) as total,%pricelevel%.name as pricelevelname,togo,count(Q.productname) as count,%prodtype%.kind as kind,GROUP_CONCAT(Q.id) as queueids " ;
2020-11-19 23:12:35 +01:00
$sql .= " FROM %queue% Q,%pricelevel%,%billproducts%,%prodtype%,%products% where %billproducts%.billid=? AND %billproducts%.queueid=Q.id " ;
$sql .= " AND Q.pricelevel = %pricelevel%.id AND Q.productid = %products%.id AND %products%.category = %prodtype%.id " ;
2020-11-19 23:14:48 +01:00
$sql .= " GROUP BY kind, CONCAT( $unit ,productname),price,pricelevelname,togo " ;
$sql .= " ORDER BY kind, CONCAT( $unit ,productname),price,pricelevelname,togo " ;
2020-11-19 23:12:35 +01:00
2020-11-19 23:14:48 +01:00
if ( $printExtras == 1 ) {
2020-11-19 23:10:46 +01:00
$sql = " SELECT
2020-11-19 23:12:35 +01:00
CONCAT ( $unit , productname ) as productname ,
2020-11-19 23:10:46 +01:00
price ,
2020-11-19 23:14:48 +01:00
( count ( Q . productname ) * price ) as total ,
2020-11-19 23:10:46 +01:00
PL . name as pricelevelname ,
togo ,
count ( Q . productname ) as count ,
PT . kind as kind ,
2020-11-19 23:14:48 +01:00
GROUP_CONCAT ( Q . id ) as queueids ,
2020-11-19 23:10:46 +01:00
(
2020-11-19 23:12:39 +01:00
SELECT GROUP_CONCAT ( CONCAT ( amount , '-' , extraid ) ORDER BY extraid )
2020-11-19 23:10:46 +01:00
FROM
% queueextras % QE
WHERE
Q . id = QE . queueid
) as concatstr
FROM
% queue % Q ,
% pricelevel % PL ,
% billproducts % BP ,
% prodtype % PT ,
% products % P
WHERE
BP . billid = ? AND
BP . queueid = Q . id AND
Q . pricelevel = PL . id AND
Q . productid = P . id AND
P . category = PT . id
GROUP BY
kind ,
2020-11-19 23:12:35 +01:00
CONCAT ( $unit , productname ),
2020-11-19 23:10:46 +01:00
price ,
pricelevelname ,
togo ,
2020-11-19 23:14:48 +01:00
concatstr
ORDER BY kind , CONCAT ( $unit , productname ), price , pricelevelname , togo
2020-11-19 23:10:46 +01:00
" ;
};
2020-11-19 23:14:48 +01:00
return CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $billid ));
}
/**
* get the content of a bill ( to be used for printserver etc . )
*
* @ param unknown $billid
*/
function getBillWithId ( $pdo , $billid , $language , $printer , $includeGuestInfo = false , $includePayment = false ) {
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,status FROM %bill% WHERE id=? " ;
$row = CommonUtils :: getRowSqlObject ( $pdo , $sql , array ( $billid ));
if ( $row -> status == 'c' ) {
return $this -> getCashBill ( $pdo , $billid , $language , $printer );
}
$sql = " SELECT count(id) as countid FROM %queue% WHERE billid=? " ;
$qrow = CommonUtils :: getRowSqlObject ( $pdo , $sql , array ( $billid ));
$tableid = $row -> tableid ;
if ( $qrow -> countid == 0 ) {
if ( $tableid == 0 ) {
$sql = " SELECT DISTINCT IF(status='c',CONCAT('V-',billuid),billuid) as billuid,billdate,brutto,ROUND(netto,2) as netto,'-' as tablename,username,%user%.id as userid,host,COALESCE(%bill%.status,'') as status,guestinfo,paymentid,printextras FROM %bill%,%user% WHERE %bill%.id=? AND userid=%user%.id AND tableid='0' " ;
} else {
$sql = " SELECT DISTINCT IF(status='c',CONCAT('V-',billuid),billuid) as billuid,billdate,brutto,ROUND(netto,2) as netto,tableno as tablename,username,%user%.id as userid,host,COALESCE(%bill%.status,'') as status,guestinfo,paymentid,printextras FROM %bill%,%user%,%resttables% WHERE %bill%.id=? AND userid=%user%.id AND tableid=%resttables%.id " ;
}
} else {
if ( $tableid == 0 ) {
$sql = " SELECT DISTINCT IF(status='c',CONCAT('V-',billuid),billuid) as billuid,billdate,brutto,ROUND(netto,2) as netto,'-' as tablename,username,%user%.id as userid,host,COALESCE(%bill%.status,'') as status,guestinfo,paymentid,printextras FROM %bill%,%user%,%queue% WHERE %bill%.id=? AND %bill%.id=%queue%.billid AND userid=%user%.id AND tableid='0' AND paidtime is not null " ;
} else {
$sql = " SELECT DISTINCT IF(status='c',CONCAT('V-',billuid),billuid) as billuid,billdate,brutto,ROUND(netto,2) as netto,tableno as tablename,username,%user%.id as userid,host,COALESCE(%bill%.status,'') as status,guestinfo,paymentid,printextras 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 " ;
}
}
$row = CommonUtils :: getRowSqlObject ( $pdo , $sql , array ( $billid ));
$status = $row -> status ;
$sign = ( $status == " s " ? " - " : " " );
if ( $tableid != 0 ) {
$sql = " SELECT abbreviation FROM %room%,%resttables% WHERE %resttables%.id=? AND %resttables%.roomid=%room%.id " ;
$trow = CommonUtils :: getRowSqlObject ( $pdo , $sql , array ( $tableid ));
if ( is_null ( $trow -> abbreviation ) || ( $trow -> abbreviation == '' )) {
$tablename = $row -> tablename ;
} else {
$tablename = $trow -> abbreviation . " - " . $row -> tablename ;
}
} else {
$tablename = " - " ;
}
if ( $row == null ) {
echo json_encode ( array ( " billoverallinfo " => array ()));
return ;
} else {
2020-11-19 23:15:07 +01:00
$tseInfo = self :: getTseInfoOfBill ( $pdo , $billid , self :: $TSE_FORMAT_ASSOC_ARRAY );
2020-11-19 23:14:48 +01:00
if ( is_null ( $row -> host )) {
$host = 0 ; // default
} else {
$host = $row -> host ;
}
$thetimedate = $row -> billdate ;
$printextrasOfReceipt = $row -> printextras ;
$billuid = $row -> billuid ;
$printExtras = false ;
if ( $printextrasOfReceipt == 1 ) {
$printExtras = true ;
}
$masterData = self :: getBillMasterDataAtBillTime ( $pdo , $billid , 'rectemplate' );
2020-11-19 23:15:07 +01:00
if ( is_null ( $masterData [ " companyinfo " ])) {
$masterData [ " companyinfo " ] = " " ;
}
$billoverallinfo = $this -> createBillOverallInfo ( $billid , $billuid , $thetimedate , $tablename , $row -> brutto , $row -> netto , $row -> username , $row -> userid , $printer , $host , $masterData , $tseInfo );
2020-11-19 23:14:48 +01:00
if ( $includeGuestInfo ) {
if ( is_null ( $row -> guestinfo )) {
$billoverallinfo [ " guestinfo " ] = '' ;
} else {
$billoverallinfo [ " guestinfo " ] = $row -> guestinfo ;
}
}
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 ;
}
$billtranslations = $this -> createBillTranslations ( $language );
}
$result = self :: getBillProductsInCumulatedOrder ( $pdo , $billid );
2020-11-19 23:10:46 +01:00
2020-11-19 22:47:44 +01:00
$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 23:10:46 +01:00
if ( $printExtras ) {
$extrasConcatStr = $zeile [ 'concatstr' ];
if ( ! is_null ( $extrasConcatStr )) {
2020-11-19 23:12:39 +01:00
$singleExtras = explode ( ',' , $extrasConcatStr );
2020-11-19 23:12:46 +01:00
$printextraprice = CommonUtils :: getConfigValue ( $pdo , 'printextraprice' , 1 );
2020-11-19 23:12:39 +01:00
$sql = " SELECT name FROM %extras% WHERE id=? " ;
2020-11-19 23:12:46 +01:00
if ( $printextraprice == 1 ) {
$decpoint = CommonUtils :: getConfigValue ( $pdo , 'decpoint' , ';' );
if (( $decpoint != '.' ) && ( $decpoint != ',' )) {
$decpoint = ',' ;
}
$sql = " SELECT CONCAT(name,' (',REPLACE(price,'.',' $decpoint '),')') as name FROM %extras% WHERE id=? " ;
}
2020-11-19 23:12:39 +01:00
foreach ( $singleExtras as $aSingleExtra ) {
$singleExtraParts = explode ( '-' , $aSingleExtra );
$amount = $singleExtraParts [ 0 ];
$singleExtraId = $singleExtraParts [ 1 ];
$extraNameRes = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $singleExtraId ));
if ( count ( $extraNameRes ) > 0 ) {
$extraText = $amount . " x " . $extraNameRes [ 0 ][ " name " ];
$prodarray [] = array ( " count " => 0 ,
" productname " => ' + ' . $extraText ,
" pricelevel " => $zeile [ 'pricelevelname' ],
" price " => 0
);
}
2020-11-19 23:10:46 +01:00
}
}
}
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:10:46 +01:00
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 23:14:48 +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:15:07 +01:00
private static function getTseInfoOfBill ( $pdo , $billid , $tseFormat ) {
2020-11-19 23:14:48 +01:00
date_default_timezone_set ( DbUtils :: getTimeZone ());
2020-11-19 23:15:07 +01:00
$sql = " SELECT opid FROM %bill% WHERE id=? " ;
$res = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $billid ));
$opid = $res [ 0 ][ " opid " ];
if ( is_null ( $opid )) {
return null ;
}
2020-11-19 23:14:48 +01:00
2020-11-19 23:15:07 +01:00
$tsestatus = " (SELECT O.tseerror FROM %operations% O WHERE O.id=B.opid ) as tsestatus " ;
$tseserialnoSql = " (SELECT T.textvalue FROM %operations% O INNER JOIN %tsevalues% T ON O.serialno=T.id WHERE O.id=B.opid ) as serialno " ;
2020-11-19 23:14:48 +01:00
$sigalgSql = " (SELECT T.textvalue FROM %operations% O INNER JOIN %tsevalues% T ON O.sigalg=T.id WHERE O.id=B.opid ) as sigalg " ;
$pubkeySql = " (SELECT T.textvalue FROM %operations% O INNER JOIN %tsevalues% T ON O.pubkey=T.id WHERE O.id=B.opid ) as pubkey " ;
$logtimeSql = " (SELECT O.logtime FROM %operations% O WHERE O.id=B.opid ) as logtime " ;
$tsesignatureSql = " (SELECT O.tsesignature FROM %operations% O WHERE O.id=B.opid ) as tsesignature " ;
$transSql = " (SELECT O.trans FROM %operations% O WHERE O.id=B.opid ) as trans " ;
$sigcounterSql = " (SELECT O.sigcounter FROM %operations% O WHERE O.id=B.opid ) as sigcounter " ;
$signtxtSql = " (SELECT O.signtxt FROM %operations% O WHERE O.id=B.opid ) as signtxt " ;
2020-11-19 23:15:07 +01:00
$sql = " SELECT status, $tsestatus , $tseserialnoSql , $tsesignatureSql , $transSql , $sigalgSql , $pubkeySql , $logtimeSql , $sigcounterSql , $signtxtSql " ;
2020-11-19 23:14:48 +01:00
$sql .= " FROM %bill% B " ;
$sql .= " WHERE B.id=? " ;
$billProperties = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $billid ));
if ( count ( $billProperties ) == 0 ) {
return " " ;
}
if ( ! isset ( $billProperties [ 0 ][ " tsesignature " ])) {
2020-11-19 23:15:07 +01:00
return null ;
}
if ( $billProperties [ 0 ][ " tsestatus " ] != 1 ) {
return null ;
2020-11-19 23:14:48 +01:00
}
$tseSignature = $billProperties [ 0 ][ " tsesignature " ];
if ( is_null ( $tseSignature ) || ( $tseSignature == " " )) {
2020-11-19 23:15:07 +01:00
return null ;
2020-11-19 23:14:48 +01:00
}
$qrCodeVersion = " V0 " ;
$serialNoOfPOS = CommonUtils :: getConfigValue ( $pdo , 'sn' , " ORD1 " );
2020-11-19 23:15:07 +01:00
$tseserialno = $billProperties [ 0 ][ " serialno " ];
2020-11-19 23:14:48 +01:00
$processType = " Kassenbeleg-V1 " ;
$processData = $billProperties [ 0 ][ " signtxt " ];
$transNumber = $billProperties [ 0 ][ " trans " ];
$sigcounter = $billProperties [ 0 ][ " sigcounter " ];
$logtime = self :: epochTimeToDSfinTime ( $billProperties [ 0 ][ " logtime " ]);
$sigAlg = $billProperties [ 0 ][ " sigalg " ];
$logTimeFormat = " unixTime " ;
$pubkey = $billProperties [ 0 ][ " pubkey " ];
$billstatus = $billProperties [ 0 ][ " status " ];
if ( $billstatus == 'c' ) {
$startlogtime = $logtime ;
} else {
$startlogtime = self :: getFirstLogTimeOfBill ( $pdo , $billid );
}
2020-11-19 23:15:07 +01:00
if ( $tseFormat == self :: $TSE_FORMAT_QR_TEXT ) {
$tseInfo = array (
$qrCodeVersion ,
$serialNoOfPOS ,
$processType ,
$processData ,
$transNumber ,
$sigcounter ,
$startlogtime ,
$logtime ,
$sigAlg ,
$logTimeFormat ,
$tseSignature ,
$pubkey
);
return implode ( ';' , $tseInfo );
} else {
$tseInfo = array (
" serialno " => $serialNoOfPOS ,
" tseserialno " => $tseserialno ,
" transnumber " => $transNumber ,
" sigcounter " => $sigcounter ,
" startlogtime " => $startlogtime ,
" logtime " => $logtime ,
" sigalg " => $sigAlg ,
" logtimeformat " => $logTimeFormat ,
" tsesignature " => $tseSignature ,
" pubkey " => $pubkey
);
return $tseInfo ;
}
2020-11-19 23:14:48 +01:00
}
public static function getFirstLogTimeOfBill ( $pdo , $billid ) {
$sql = " SELECT MIN(Q.logtime) as starttime FROM %queue% Q,%billproducts% BP WHERE BP.billid=? AND BP.queueid=Q.id " ;
$startlogRes = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $billid ));
if ( count ( $startlogRes ) == 0 ) {
return " " ;
}
$startlogtime = self :: epochTimeToDSfinTime ( $startlogRes [ 0 ][ " starttime " ]);
return $startlogtime ;
}
2020-11-19 23:15:07 +01:00
public static function outputBillQrCode ( $billid ) {
2020-11-19 23:14:48 +01:00
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
2020-11-19 23:15:07 +01:00
$tseInfo = self :: getTseInfoOfBill ( $pdo , $billid , self :: $TSE_FORMAT_QR_TEXT );
2020-11-19 23:14:48 +01:00
if ( ! is_null ( $tseInfo ) && ( $tseInfo != " " )) {
QRcode :: png ( $tseInfo );
2020-11-19 23:14:10 +01:00
} else {
2020-11-19 23:14:48 +01:00
CommonUtils :: outputWideEmptyImage ();
2020-11-19 23:14:10 +01:00
}
}
2020-11-19 23:14:48 +01:00
private static function epochTimeToDSfinTime ( $epoch ) {
$datePart = date ( " Y-m-d " , $epoch );
$timePart = date ( " H:i:s " , $epoch );
return $datePart . " T " . $timePart . " .000 " ;
}
private static function getBillMasterDataAtBillTime ( $pdo , $billid , $templatename ) {
$sql = " SELECT billdate FROM %bill% WHERE id=? " ;
$res = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $billid ));
$billdate = $res [ 0 ][ " billdate " ];
return CommonUtils :: getMasterDataAtCertainDateTime ( $pdo , $billdate , $templatename );
}
2020-11-19 23:14:10 +01:00
2020-11-19 23:11:29 +01:00
private function getCashBill ( $pdo , $billid , $language , $printer ) {
$sql = " SELECT billdate,ROUND(brutto,2) as brutto,ROUND(netto,2) as netto,ROUND(tax,2) as tax,username,userid,reason FROM %bill% B, %user% U WHERE B.id=? AND B.userid=U.id " ;
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $billid ));
$r = $result [ 0 ];
$brutto = $r [ " brutto " ];
$netto = $r [ " netto " ];
2020-11-19 23:14:48 +01:00
$masterData = self :: getBillMasterDataAtBillTime ( $pdo , $billid , 'cashtemplate' );
2020-11-19 23:15:07 +01:00
$tseInfo = self :: getTseInfoOfBill ( $pdo , $billid , self :: $TSE_FORMAT_ASSOC_ARRAY );
$billoverallinfo = $this -> createBillOverallInfo ( $billid , " V- " . $billid , $r [ " billdate " ], ' ' , $brutto , $netto , $r [ " username " ], $r [ " userid " ], 1 , 0 , $masterData , $tseInfo );
2020-11-19 23:11:29 +01:00
$billoverallinfo [ " guestinfo " ] = '' ;
$billtranslations = $this -> createBillTranslations ( $language );
$prods = array ();
$prods [] = array (
" productname " => " Barein-/auslage " ,
" price " => $r [ " brutto " ],
" pricelevel " => " A " ,
" count " => 1
);
$reason = $r [ " reason " ];
if ( ! is_null ( $reason )) {
$prods [] = array (
" productname " => " ( $reason ) " ,
" price " => 0 ,
" pricelevel " => " A " ,
" count " => 0
);
}
$taxes = array (
array ( " tax " => " 0.00 " , " mwst " => " 0.00 " , " netto " => $netto , " brutto " => $brutto )
);
$out = array ( " billoverallinfo " => $billoverallinfo , " translations " => $billtranslations , " products " => $prods , " taxes " => $taxes );
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
2020-11-19 23:14:21 +01:00
private static function doCashAction ( $money , $remark , $cashtype ) {
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:12:46 +01:00
$userId = self :: getUserIdStatic ();
2020-11-19 23:14:21 +01:00
$result = self :: doCashActionCore ( $money , $remark , $currentTime , $userId , $cashtype );
2020-11-19 23:12:46 +01:00
echo json_encode ( $result );
}
2020-11-19 23:14:21 +01:00
public static function doCashActionCore ( $money , $remark , $datetime , $userId , $cashtype ) {
2020-11-19 23:12:46 +01:00
date_default_timezone_set ( DbUtils :: getTimeZone ());
2020-11-19 23:14:21 +01:00
if ( trim ( $money ) == '' ) {
$money = '0.00' ;
}
$moneyFloat = floatval ( $money );
2020-11-19 23:14:48 +01:00
if ( $cashtype == self :: $CASHTYPE_Privatentnahme [ " value " ]) {
2020-11-19 23:14:21 +01:00
if ( $moneyFloat >= 0.00 ) {
2020-11-19 23:14:48 +01:00
$cashtype = self :: $CASHTYPE_Privateinlage [ " value " ];
2020-11-19 23:14:21 +01:00
}
2020-11-19 23:14:48 +01:00
} else if ( $cashtype == self :: $CASHTYPE_Einzahlung [ " value " ]) {
2020-11-19 23:14:21 +01:00
if ( $moneyFloat <= 0.00 ) {
2020-11-19 23:14:48 +01:00
$cashtype = self :: $CASHTYPE_Auszahlung [ " value " ];
2020-11-19 23:14:21 +01:00
}
}
2020-11-19 23:12:46 +01:00
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
2020-11-19 23:14:21 +01:00
CommonUtils :: log ( $pdo , " QUEUE " , " Cash action with money ' $money ' at billtime ' $datetime ' with cashtype ' $cashtype ' " );
2020-11-19 23:00:18 +01:00
$pdo -> beginTransaction ();
2020-11-19 23:12:46 +01:00
$sql = " SELECT sum(brutto) as bruttosum FROM %bill% WHERE closingid is null AND paymentid=? " ;
$res = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( 1 ));
if ( count ( $res ) > 0 ) {
$sum = $res [ 0 ][ " bruttosum " ];
2020-11-19 22:47:44 +01:00
if ( is_null ( $sum )) {
$sum = 0.0 ;
}
2020-11-19 23:14:48 +01:00
$nextbillid = self :: testForNewBillIdAndUpdateWorkTable ( $pdo );
if ( $nextbillid < 0 ) {
$pdo -> rollBack ();
return ( array ( " status " => " ERROR " , " code " => ERROR_INCONSISTENT_DB , " msg " => ERROR_INCONSISTENT_DB_MSG ));
}
2020-11-19 23:12:46 +01:00
2020-11-19 23:14:48 +01:00
CommonUtils :: log ( $pdo , " QUEUE " , " Calc bill signature for cash money ' $money ' at billtime ' $datetime ' " );
$signature = CommonUtils :: calcSignatureForBill ( $datetime , $money , $money , $userId );
$sql = " SELECT (COALESCE(MAX(billuid),0)) as maxbilluid FROM %bill% " ;
$maxuidRes = CommonUtils :: fetchSqlAll ( $pdo , $sql );
$maxBilluid = $maxuidRes [ 0 ][ " maxbilluid " ];
$nextBilluid = intval ( $maxBilluid ) + 1 ;
$sql = " INSERT INTO `%bill%` (`id` ,`billuid` , `billdate`,`brutto`,`netto`,`tax`,`tableid`, `status`, `paymentid`,`cashtype`,`userid`,`ref`,`reason`,`signature`) VALUES ( ?, ?,?,?,?,?, ?, 'c', ?,?,?,?,?,?) " ;
CommonUtils :: execSql ( $pdo , $sql , array ( $nextbillid , $nextBilluid , $datetime , $money , $money , '0.00' , - 1 , 1 , $cashtype , $userId , NULL , $remark , $signature ));
$lastId = $pdo -> lastInsertId ();
if ( $lastId > 1 ) {
$sql = " SELECT brutto,prevbrutto,netto,prevnetto FROM %bill% WHERE id=? " ;
2020-11-19 22:58:20 +01:00
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
2020-11-19 23:14:48 +01:00
$stmt -> execute ( array ( $lastId - 1 ));
$row = $stmt -> fetchObject ();
}
$sql = " UPDATE %bill% SET prevbrutto=?,prevnetto=? WHERE id=? " ;
2020-11-19 23:15:07 +01:00
CommonUtils :: execSql ( $pdo , $sql , array ( $row -> brutto + $row -> prevbrutto , $row -> netto + $row -> prevnetto , $lastId ));
2020-11-19 23:14:48 +01:00
$moneyTwoDigits = number_format ( $money , 2 , '.' , '' );
2020-11-19 23:14:58 +01:00
if (( $cashtype == self :: $CASHTYPE_Privateinlage [ " value " ]) || ( $cashtype == self :: $CASHTYPE_Privatentnahme [ " value " ])) {
2020-11-19 23:14:48 +01:00
$signStr = " Beleg^ " . implode ( '_' , array ( '0.00' , '0.00' , '0.00' , '0.00' , $moneyTwoDigits )) . " ^ " . $moneyTwoDigits . " :Bar " ;
2020-11-19 23:15:04 +01:00
} else if ( $cashtype == self :: $CASHTYPE_Geldtransit [ " value " ]) {
$signStr = " Beleg^ " . implode ( '_' , array ( $moneyTwoDigits , '0.00' , '0.00' , '0.00' , '0.00' )) . " ^ " . $moneyTwoDigits . " :Bar " ;
2020-11-19 23:14:58 +01:00
} else if (( $cashtype == self :: $CASHTYPE_Einzahlung [ " value " ]) || ( $cashtype == self :: $CASHTYPE_Auszahlung [ " value " ])) {
2020-11-19 23:14:48 +01:00
$signStr = " Beleg^ " . implode ( '_' , array ( $moneyTwoDigits , '0.00' , '0.00' , '0.00' , '0.00' )) . " ^ " . $moneyTwoDigits . " :Bar " ;
} else if ( $cashtype == self :: $CASHTYPE_TrinkgeldAN [ " value " ]) {
$signStr = " Beleg^ " . implode ( '_' , array ( '0.00' , '0.00' , '0.00' , '0.00' , $moneyTwoDigits )) . " ^ " . $moneyTwoDigits . " :Bar " ;
} else if ( $cashtype == self :: $CASHTYPE_TrinkgeldAG [ " value " ]) {
$signStr = " Beleg^ " . implode ( '_' , array ( $moneyTwoDigits , '0.00' , '0.00' , '0.00' , '0.00' )) . " ^ " . $moneyTwoDigits . " :Bar " ;
}
$status = self :: signValueByTseAndUpdateBill ( $pdo , $lastId , $signStr );
if ( $status [ " status " ] != " OK " ) {
$pdo -> rollBack ();
return $status ;
}
$printcash = CommonUtils :: getConfigValue ( $pdo , 'printcash' , 0 );
if ( $printcash == 1 ) {
if ( session_id () == '' ) {
session_start ();
2020-11-19 23:12:24 +01:00
}
2020-11-19 23:14:48 +01:00
$printer = $_SESSION [ 'receiptprinter' ];
PrintQueue :: internalQueueReceiptPrintjob ( $pdo , $nextbillid , $printer );
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:14:48 +01:00
$pdo -> commit ();
return ( array ( " status " => " OK " ));
2020-11-19 22:47:44 +01:00
} else {
$pdo -> rollBack ();
2020-11-19 23:12:46 +01:00
return ( array ( " status " => " ERROR " , " code " => ERROR_GENERAL_PAYDESK_SUM , " msg " => ERROR_GENERAL_PAYDESK_SUM_MSG ));
2020-11-19 22:47:44 +01:00
}
}
/*
* 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 ;
2020-11-19 23:12:37 +01:00
$sql = " SELECT sum(brutto) as sumtotal FROM %bill% WHERE closingid is null AND paymentid='1' AND userid=? AND (status is null OR status ='c') " ;
$res = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $userId ));
if ( count ( $res ) > 0 ) {
$cashVal = $res [ 0 ][ " sumtotal " ];
if ( ! is_null ( $cashVal )) {
$cashByGuestsAndInsertTakeOut = $cashVal ;
2020-11-19 23:00:18 +01:00
}
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:12:37 +01:00
$onlyCash = 0.0 ;
$sql = " SELECT sum(brutto) as sumtotal FROM %bill% WHERE closingid is null AND paymentid='1' AND userid=? AND status ='c' " ;
CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $userId ));
$res = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $userId ));
if ( count ( $res ) > 0 ) {
$cashVal = $res [ 0 ][ " sumtotal " ];
if ( ! is_null ( $cashVal )) {
$onlyCash = $cashVal ;
}
}
echo json_encode ( array ( " cashperpayments " => $cashPerPayments , " total " => $cashByGuestsAndInsertTakeOut , " onlycash " => $onlyCash ));
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 ());
$startDate = " $year - $month - $day 00:00:00 " ;
$endDate = " $year - $month - $day 23:59:59 " ;
$whenClause = " (billdate >= ? AND billdate <= ?) " ;
2020-11-19 23:10:46 +01:00
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
2020-11-19 22:47:44 +01:00
$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 23:14:48 +01:00
$sql = " SELECT id,billuid,billdate,brutto,tableid,closingid,status,host FROM %bill% WHERE tableid >= '0' AND $whenClause " ;
2020-11-19 23:11:29 +01:00
$sql .= " UNION " ;
2020-11-19 23:14:48 +01:00
$sql .= " SELECT id,IF(status='c',CONCAT('V-',billuid),billuid) as billuid,billdate,brutto,tableid,closingid,status,host FROM %bill% WHERE status='c' AND $whenClause " ;
2020-11-19 23:11:29 +01:00
$sql .= " ORDER BY id DESC,billdate DESC " ;
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $startDate , $endDate , $startDate , $endDate ));
2020-11-19 23:00:18 +01:00
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 23:11:29 +01:00
$host = 0 ;
$tablename = " - " ;
if ( $zeile [ " status " ] != 'c' ) {
$host = $zeile [ 'host' ];
$tablename = $commonUtils -> getTableNameFromId ( $pdo , $zeile [ 'tableid' ]);
}
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:14:48 +01:00
" billuid " => $zeile [ 'billuid' ],
2020-11-19 23:00:18 +01:00
" longdate " => $zeile [ 'billdate' ],
" shortdate " => $shortdate ,
" brutto " => $zeile [ 'brutto' ],
2020-11-19 23:11:29 +01:00
" tablename " => $tablename ,
2020-11-19 23:10:46 +01:00
" billcontent " => $this -> getBillWithId ( $pdo , $theId , $l , 0 , true , false ),
2020-11-19 22:55:20 +01:00
" isClosed " => $isClosed ,
2020-11-19 23:11:29 +01:00
" host " => $host
2020-11-19 22:47:44 +01:00
);
2020-11-19 23:11:29 +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' ];
}
2020-11-19 23:12:46 +01:00
private static function getUserIdStatic () {
if ( session_id () == '' ) {
session_start ();
}
return $_SESSION [ 'userid' ];
}
2020-11-19 22:47:44 +01:00
/**
* 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 )
*/
2020-11-19 23:12:46 +01:00
private static function testForNewBillIdAndUpdateWorkTable ( $pdo ) {
2020-11-19 23:00:18 +01:00
$sql = " SELECT MAX(id) as maxbillid FROM %bill% " ;
2020-11-19 23:12:46 +01:00
$res = CommonUtils :: fetchSqlAll ( $pdo , $sql );
$maxbillid = 0 ;
if ( count ( $res ) > 0 ) {
$maxbillid = $res [ 0 ][ " maxbillid " ];
if ( is_null ( $maxbillid )) {
$maxbillid = 0 ;
}
}
$nextbillid = intval ( $maxbillid ) + 1 ;
$commonUtils = new CommonUtils ();
2020-11-19 23:00:18 +01:00
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' ];
2020-11-19 23:12:07 +01:00
$signature = CommonUtils :: calcSignatureForBill ( $currentTime , '0.00' , '0.00' , $userid );
2020-11-19 23:02:19 +01:00
2020-11-19 23:14:48 +01:00
$nextBillIds = self :: getNextBillIds ( $pdo );
$nextBilluid = $nextBillIds [ " nextbilluid " ];
$sql = " INSERT INTO `%bill%` (`id` ,`billuid`, `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 , $nextBilluid , $currentTime , '0.00' , '0.00' , $newprevbrutto , $newprevnetto , $tableid , 1 , $userid , 0 , null , null , null , $signature , 'STARTBELEG' ));
2020-11-19 23:02:19 +01:00
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:14:48 +01:00
$status = $this -> recreateBill ( $pdo , $ids , $row -> brutto , $row -> netto , $row -> tableid , $row -> paymentid , $row -> tax , 1 - $isNowHost , $row -> reservationid , $row -> guestinfo , $row -> intguestid , $row -> intguestpaid );
if ( $status [ " status " ] != " OK " ) {
$pdo -> rollBack ();
echo json_encode ( $status );
}
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 ();
2020-11-19 23:14:48 +01:00
$row = $stmt -> fetchObject ();
2020-11-19 22:55:20 +01:00
$billid = intval ( $row -> id ) + 1 ;
$commonUtils = new CommonUtils ();
$commonUtils -> setLastBillIdInWorkTable ( $pdo , $billid );
if ( is_null ( $tableid )) {
$tableid = 0 ;
}
2020-11-19 23:12:07 +01:00
$signature = CommonUtils :: calcSignatureForBill ( $currentTime , $brutto , $netto , $userid );
2020-11-19 22:55:20 +01:00
2020-11-19 23:14:48 +01:00
$nextBillIds = self :: getNextBillIds ( $pdo );
$nextBilluid = $nextBillIds [ " nextbilluid " ];
$billInsertSql = " INSERT INTO `%bill%` (`id` , `billuid`,`billdate`,`brutto`,`netto`,`tableid`,`paymentid`,`userid`,`ref`,`tax`,`host`,`reservationid`,`guestinfo`,`intguestid`,`intguestpaid`,`signature`) VALUES (?,?,?,?,?,?,?,?,NULL,NULL,?,?,?,?,?,?) " ;
CommonUtils :: execSql ( $pdo , $billInsertSql , array ( $billid , $nextBilluid , $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
2020-11-19 23:14:48 +01:00
self :: setPositionsOfProductsOnBillProductsTable ( $pdo , $newBillId );
$status = self :: signOrdersBill ( $pdo , $newBillId , false );
if ( $status [ " status " ] != " OK " ) {
return $status ;
}
2020-11-19 23:00:58 +01:00
Hotelinterface :: insertIntoHsin ( $pdo , $newBillId );
2020-11-19 23:14:48 +01:00
return array ( " status " => " OK " );
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:12:46 +01:00
* Public : because it is called by demodata
2020-11-19 22:47:44 +01:00
*/
2020-11-19 23:12:46 +01:00
public function cancelBill ( $pdo , $billid , $stornocode , $reason , $doOwnTransaction , $doEcho , $checkStornoCode , $removeproducts = 0 , $dateTime = null ) {
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:12:46 +01:00
if ( ! is_null ( $dateTime )) {
$currentTime = $dateTime ;
}
2020-11-19 22:47:44 +01:00
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 );
2020-11-19 23:12:22 +01:00
if ( is_null ( $stornocodeInDb ) && $checkStornoCode ) {
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
}
2020-11-19 23:12:46 +01:00
$nextbillid = self :: testForNewBillIdAndUpdateWorkTable ( $pdo );
2020-11-19 22:47:44 +01:00
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:14:48 +01:00
$sql = " SELECT id,ordertype,voucherid FROM %queue% WHERE billid=? " ;
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 ));
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 23:14:48 +01:00
$ordertype = $row [ " ordertype " ];
if (( $removeproducts == 1 ) || ( $ordertype == DbUtils :: $ORDERTYPE_1ZweckEinl )) {
$sql = " UPDATE %queue% SET toremove='1',paidtime=null,billid=null WHERE id=? " ;
CommonUtils :: execSql ( $pdo , $sql , array ( $row [ " id " ]));
vouchers :: handleRemoveQueueItem ( $pdo , $row [ 'id' ]);
$signRemoval = QueueContent :: signRemovalOfQueueItem ( $pdo , $row [ 'id' ]);
if ( $signRemoval [ " status " ] != " OK " ) {
if ( $doOwnTransaction ) {
$pdo -> rollBack ();
}
if ( $doEcho ) {
echo json_encode ( array ( " status " => " ERROR " , " msg " => " TSE-Signierung fehlgeschlagen: " . $signRemoval [ " msg " ]));
}
return false ;
}
}
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=? " ;
2020-11-19 23:14:48 +01:00
CommonUtils :: execSql ( $pdo , $sql , array ( $billid ));
2020-11-19 23:00:55 +01:00
}
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 23:12:07 +01:00
$signature = CommonUtils :: calcSignatureForBill ( $currentTime , $stornval , $stornonettoval , $userIdOfStornoUser );
2020-11-19 23:00:18 +01:00
2020-11-19 23:14:48 +01:00
$nextBillIds = self :: getNextBillIds ( $pdo );
$nextBilluid = $nextBillIds [ " nextbilluid " ];
$sql = " INSERT INTO `%bill%` (`id` , `billuid`, `billdate`,`brutto`,`netto`,`tax`,`tableid`, `status`, `paymentid`,`userid`,`ref`,`host`,`reservationid`,`guestinfo`,`signature`) VALUES (?,?,?,?,?,?,?, 's', ?,?,?,?,?,?,?) " ;
CommonUtils :: execSql ( $pdo , $sql , array ( $nextbillid , $nextBilluid , $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=? " ;
2020-11-19 23:14:48 +01:00
CommonUtils :: execSql ( $pdo , $sql , array ( $row -> brutto + $row -> prevbrutto + $stornval , $row -> netto + $row -> prevnetto + $stornonettoval , $refIdOfStornoEntry ));
2020-11-19 22:58:20 +01:00
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:14:48 +01:00
CommonUtils :: execSql ( $pdo , $sql , 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=? " ;
2020-11-19 23:14:48 +01:00
CommonUtils :: execSql ( $pdo , $sql , array ( $reason , $billid ));
2020-11-19 22:52:25 +01:00
}
2020-11-19 23:00:18 +01:00
foreach ( $queueIdArray as $aQueueid ) {
$billProdsSql = " INSERT INTO `%billproducts%` (`queueid` , `billid`) VALUES ( ?,?) " ;
2020-11-19 23:14:48 +01:00
CommonUtils :: execSql ( $pdo , $billProdsSql , array ( $aQueueid , $refIdOfStornoEntry ));
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:14:48 +01:00
self :: setPositionsOfProductsOnBillProductsTable ( $pdo , $refIdOfStornoEntry );
$status = self :: signOrdersBill ( $pdo , $refIdOfStornoEntry , true );
if ( $status [ " status " ] != " OK " ) {
if ( $doOwnTransaction ) {
$pdo -> rollBack ();
echo json_encode ( array ( " status " => " ERROR " , " msg " => " Rechnung kann nicht storniert werden? Ist die TSE korrekt eingebunden? " ));
}
return false ;
}
2020-11-19 23:11:27 +01:00
if ( $tableid == 0 ) {
$tableid = null ;
}
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 23:12:32 +01:00
private function exportPdfReport ( $startMonth , $startYear , $endMonth , $endYear , $closidstart = null , $closidend = null ) {
2020-11-19 22:47:44 +01:00
$pdfExport = new PdfExport ();
2020-11-19 22:55:09 +01:00
$lang = 0 ;
if ( isset ( $_GET [ " lang " ])) {
$lang = $_GET [ 'lang' ];
}
2020-11-19 23:12:32 +01:00
$pdfExport -> exportPdfReport ( $lang , $startMonth , $startYear , $endMonth , $endYear , $closidstart , $closidend );
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 23:12:32 +01:00
private function exportPdfSummaryClosPeriod ( $closidstart , $closidend ) {
$pdfExport = new PdfExport ();
$lang = 0 ;
if ( isset ( $_GET [ " lang " ])) {
$lang = $_GET [ 'lang' ];
}
$pdfExport -> exportPdfSummaryClosPeriod ( $lang , $closidstart , $closidend );
}
2020-11-19 23:12:59 +01:00
private function exportCsvSummaryClosPeriod ( $closidstart , $closidend ) {
$pdfExport = new PdfExport ();
$lang = 0 ;
if ( isset ( $_GET [ " lang " ])) {
$lang = $_GET [ 'lang' ];
}
$pdfExport -> exportCsvSummaryClosPeriod ( $lang , $closidstart , $closidend );
}
2020-11-19 23:12:32 +01:00
2020-11-19 23:12:24 +01:00
private function exportAllCsvOrExcel ( $startMonth , $startYear , $endMonth , $endYear , $exportFormat ) {
set_time_limit ( 60 * 5 );
if ( session_id () == '' ) {
session_start ();
}
$l = $_SESSION [ 'language' ];
$commonUtils = new CommonUtils ();
$currency = $commonUtils -> getCurrency ();
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
$decpoint = CommonUtils :: getConfigValue ( $pdo , 'decpoint' , '.' );
if ( $startMonth < 10 ) {
$startMonth = " 0 " . $startMonth ;
}
if ( $endMonth < 10 ) {
$endMonth = " 0 " . $endMonth ;
}
$startDate = $startYear . " - " . $startMonth . " -01 00:00:00 " ;
$endDate = $endYear . " - " . $endMonth . " -01 " ;
$lastdayOfMonth = date ( " t " , strtotime ( $endDate ));
$endDate = $endYear . " - " . $endMonth . " - " . $lastdayOfMonth . " 23:59:59 " ;
$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 (
2020-11-19 23:12:27 +01:00
'Bestellzeitpunkt' ,
'Abrechnungszeitpunkt' ,
2020-11-19 23:12:32 +01:00
'Tag der Bestellung' ,
2020-11-19 23:12:24 +01:00
$this -> t [ 'ID' ][ $l ],
$this -> t [ 'Tablename' ][ $l ],
2020-11-19 23:12:27 +01:00
'Produktpreis' . " ( $currency ) " ,
2020-11-19 23:12:24 +01:00
$this -> t [ 'Brutto' ][ $l ] . " ( $currency ) " ,
$this -> t [ 'Netto' ][ $l ] . " ( $currency ) " ,
'Umsatzsteuer (%)' ,
$this -> t [ 'host' ][ $l ],
$this -> t [ 'Ref' ][ $l ],
$this -> t [ 'State' ][ $l ],
2020-11-19 23:14:04 +01:00
'Bestell-ID' ,
2020-11-19 23:12:24 +01:00
'Produkt' ,
'Produkt-ID' ,
2020-11-19 23:12:30 +01:00
'Barcode' ,
2020-11-19 23:12:24 +01:00
$this -> t [ 'PayWay' ][ $l ],
$this -> t [ 'reason' ][ $l ],
2020-11-19 23:12:27 +01:00
'Abrechnungsbenutzer' ,
'Abrechnungsbenutzer-ID' ,
2020-11-19 23:12:24 +01:00
$this -> t [ 'ClosDate' ][ $l ],
2020-11-19 23:12:32 +01:00
$this -> t [ 'ClosRemark' ][ $l ],
2020-11-19 23:14:04 +01:00
$this -> t [ 'ClosId' ][ $l ]
2020-11-19 23:12:24 +01:00
);
$lineLength = count ( $firstRow );
$allcells [] = $firstRow ;
$payment_lang = array ( " name " , " name_en " , " name_esp " );
$payment_col = $payment_lang [ $l ];
2020-11-19 23:12:35 +01:00
$unit = CommonUtils :: caseOfSqlUnitSelection ( $pdo );
2020-11-19 23:14:21 +01:00
$cashReadable = self :: getTranslateSqlCashTypeToReadable ();
2020-11-19 23:12:24 +01:00
$sql = "
2020-11-19 23:12:32 +01:00
SELECT * FROM (
2020-11-19 23:12:24 +01:00
SELECT B . billdate as billdate , B . id as billid ,
2020-11-19 23:12:32 +01:00
ordertime , DATE_FORMAT ( ordertime , '%w' ) as orderdayofweek ,
2020-11-19 23:12:24 +01:00
IF ( tableid > '0' ,( SELECT tableno FROM % resttables % WHERE id = tableid ), '' ) as tablename ,
2020-11-19 23:12:27 +01:00
price as productprice ,
2020-11-19 23:12:24 +01:00
( IF ( B . status = 's' , - 1 , 1 ) * price ) as brutto ,
( IF ( B . status = 's' , - 1 , 1 ) * Q . price / ( 1 + Q . tax * 0.01 )) as netto ,
Q . tax as tax ,
( IF ( B . host = '1' , 'x' , '-' )) as host ,
2020-11-19 23:14:48 +01:00
COALESCE ( B . ref , '' ) as reference ,
2020-11-19 23:12:27 +01:00
B . status as status ,
2020-11-19 23:14:21 +01:00
'' as cashtype ,
2020-11-19 23:14:04 +01:00
Q . id as queueid ,
2020-11-19 23:12:35 +01:00
CONCAT ( $unit , productname ) as productname ,
2020-11-19 23:12:27 +01:00
productid ,
2020-11-19 23:14:48 +01:00
COALESCE (( SELECT barcode from % products % PR WHERE PR . id = productid ), '' ) as barcode ,
2020-11-19 23:12:27 +01:00
P . $payment_col as payment ,
2020-11-19 23:14:48 +01:00
COALESCE ( B . reason , '' ) as reason ,
U . username , U . id as userid , C . closingdate as closingdate , COALESCE ( C . remark , '' ) as remark ,
2020-11-19 23:12:32 +01:00
Q . clsid
2020-11-19 23:12:24 +01:00
from % billproducts % BP , % queue % Q , % bill % B , % payment % P , % user % U , % closing % C
WHERE BP . queueid = Q . id AND BP . billid = B . id AND B . closingid is not null AND B . paymentid = P . id
2020-11-19 23:12:32 +01:00
AND U . id = B . userid AND B . closingid = C . id
2020-11-19 23:12:24 +01:00
AND B . billdate >= ? AND B . billdate <= ?
UNION ALL
2020-11-19 23:12:27 +01:00
SELECT '' as billdate , '' as billid ,
2020-11-19 23:12:32 +01:00
ordertime , DATE_FORMAT ( ordertime , '%w' ) as orderdayofweek ,
2020-11-19 23:12:27 +01:00
IF ( tablenr > '0' ,( SELECT tableno FROM % resttables % WHERE id = tablenr ), '' ) as tablename ,
price as productprice ,
'0.00' as brutto ,
'0.00' as netto ,
Q . tax as tax ,
'-' as host ,
'' as reference ,
'd' as status ,
2020-11-19 23:14:21 +01:00
'' as cashtype ,
2020-11-19 23:14:04 +01:00
Q . id as queueid ,
2020-11-19 23:12:35 +01:00
CONCAT ( $unit , productname ) as productname ,
2020-11-19 23:12:27 +01:00
productid ,
2020-11-19 23:14:48 +01:00
COALESCE (( SELECT barcode from % products % PR WHERE PR . id = productid ), '' ) as barcode ,
2020-11-19 23:12:27 +01:00
'' as payment ,
'' as reason ,
'' as username , '' as userid ,
2020-11-19 23:14:04 +01:00
'' as closingdate , '' as remark ,
2020-11-19 23:12:32 +01:00
Q . clsid
2020-11-19 23:12:27 +01:00
FROM % queue % Q
WHERE Q . toremove = '1'
AND ordertime >= ? AND ordertime <= ?
2020-11-19 23:14:04 +01:00
AND paidtime is null
2020-11-19 23:12:27 +01:00
UNION ALL
SELECT B . billdate as billdate , B . id as billid , B . billdate as ordertime ,
2020-11-19 23:12:32 +01:00
DATE_FORMAT ( B . billdate , '%w' ) as orderdayofweek ,
2020-11-19 23:12:27 +01:00
'' as tablename ,
'' as productprice ,
B . brutto as brutto , B . netto as netto ,
'' as tax ,
'' as host ,
'' as reference ,
status ,
2020-11-19 23:14:21 +01:00
$cashReadable ,
2020-11-19 23:14:04 +01:00
'' as queueid ,
2020-11-19 23:12:27 +01:00
'Einlage' as productname ,
2020-11-19 23:12:30 +01:00
'' as productid ,
'' as barcode ,
2020-11-19 23:14:48 +01:00
'Barzahlung' as payment , COALESCE ( B . reason , '' ) as reason , U . username , U . id as userid , C . closingdate as closingdate , COALESCE ( C . remark , '' ) as remark ,
2020-11-19 23:12:32 +01:00
closingid
2020-11-19 23:12:24 +01:00
FROM % bill % B , % user % U , % closing % C WHERE B . status = 'c' AND B . closingid is not null AND B . userid = U . id AND B . closingid = C . id
AND B . billdate >= ? AND B . billdate <= ?
2020-11-19 23:12:32 +01:00
) a ORDER BY ordertime , billid
2020-11-19 23:12:24 +01:00
" ;
2020-11-19 23:12:27 +01:00
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $startDate , $endDate , $startDate , $endDate , $startDate , $endDate ));
2020-11-19 23:12:24 +01:00
foreach ( $result as $z ) {
set_time_limit ( 60 * 5 );
2020-11-19 23:12:39 +01:00
$prodprice = $z [ 'productprice' ];
2020-11-19 23:12:24 +01:00
$brutto = $z [ 'brutto' ];
$netto = $z [ 'netto' ];
$tax = $z [ 'tax' ];
if ( $exportFormat == DO_CSV ) {
2020-11-19 23:12:39 +01:00
$prodprice = number_format ( $prodprice , 2 , $decpoint , '' );
2020-11-19 23:12:24 +01:00
$brutto = number_format ( $brutto , 2 , $decpoint , '' );
$netto = str_replace ( " . " , $decpoint , $netto );
if ( $tax != '' ) {
$tax = number_format ( $tax , 2 , $decpoint , '' );
}
}
$status = $z [ 'status' ];
$cat = 'Produktverkauf' ;
if ( $status == 'x' ) {
$cat = $this -> t [ " laterCancelled " ][ $l ];
} else if ( $status == 's' ) {
$cat = $this -> t [ " storno " ][ $l ];
} else if ( $status == 'c' ) {
2020-11-19 23:14:21 +01:00
$cat = $this -> t [ " cashact " ][ $l ] . $z [ 'cashtype' ];
2020-11-19 23:12:27 +01:00
} else if ( $status == 'd' ) {
$cat = 'Storno vor Abrechnung' ;
2020-11-19 23:12:24 +01:00
}
$line = array (
2020-11-19 23:12:27 +01:00
$z [ 'ordertime' ],
2020-11-19 23:12:24 +01:00
$z [ 'billdate' ],
2020-11-19 23:12:32 +01:00
self :: $daynamesStartSunday [ intval ( $z [ 'orderdayofweek' ])][ $l ],
2020-11-19 23:12:24 +01:00
$z [ 'billid' ], $z [ 'tablename' ],
2020-11-19 23:12:39 +01:00
$prodprice , $brutto , $netto ,
2020-11-19 23:12:24 +01:00
$tax ,
$z [ 'host' ],
$z [ 'reference' ],
$cat ,
2020-11-19 23:14:04 +01:00
$z [ 'queueid' ],
2020-11-19 23:12:24 +01:00
$z [ 'productname' ],
$z [ 'productid' ],
2020-11-19 23:12:30 +01:00
$z [ 'barcode' ],
2020-11-19 23:12:24 +01:00
$z [ 'payment' ],
$z [ 'reason' ],
$z [ 'username' ], $z [ 'userid' ],
2020-11-19 23:14:04 +01:00
$z [ 'closingdate' ], $z [ 'remark' ], $z [ 'clsid' ]);
2020-11-19 23:12:24 +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' );
if ( $exportFormat == DO_EXCEL ) {
$formatCodeBrutto = " 0.00 " ;
$formatCodeNetto = " 0.0000 " ;
for ( $i = 1 ; $i < count ( $allcells ); $i ++ ) {
2020-11-19 23:12:27 +01:00
$aVal = $objWorksheet -> getCell ( 'F' . ( $i + 1 )) -> getValue ();
$objWorksheet -> getCell ( 'F' . ( $i + 1 )) -> setValueExplicit ( $aVal , PHPExcel_Cell_DataType :: TYPE_NUMERIC );
$objWorksheet -> getStyle ( 'F' . ( $i + 1 )) -> getNumberFormat () -> setFormatCode ( $formatCodeBrutto );
2020-11-19 23:12:32 +01:00
2020-11-19 23:12:27 +01:00
$aVal = $objWorksheet -> getCell ( 'G' . ( $i + 1 )) -> getValue ();
$objWorksheet -> getCell ( 'G' . ( $i + 1 )) -> setValueExplicit ( $aVal , PHPExcel_Cell_DataType :: TYPE_NUMERIC );
2020-11-19 23:12:32 +01:00
$objWorksheet -> getStyle ( 'G' . ( $i + 1 )) -> getNumberFormat () -> setFormatCode ( $formatCodeBrutto );
$aVal = $objWorksheet -> getCell ( 'H' . ( $i + 1 )) -> getValue ();
$objWorksheet -> getCell ( 'H' . ( $i + 1 )) -> setValueExplicit ( $aVal , PHPExcel_Cell_DataType :: TYPE_NUMERIC );
$objWorksheet -> getStyle ( 'H' . ( $i + 1 )) -> getNumberFormat () -> setFormatCode ( $formatCodeNetto );
2020-11-19 23:12:24 +01:00
}
}
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 );
$objWriter -> setDelimiter ( ';' );
} else {
header ( " Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet " );
header ( " Content-Disposition: attachment; filename= \" ordersprinter-datenexport.xls \" " );
header ( " Cache-Control: max-age=0 " );
$objWriter = PHPExcel_IOFactory :: createWriter ( $objPHPExcel , 'Excel5' );
}
$objWriter -> save ( " php://output " );
$objPHPExcel -> disconnectWorksheets ();
unset ( $objPHPExcel );
}
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
}
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:14:21 +01:00
$cashtypeReadableSql = self :: getTranslateSqlCashTypeToReadable ();
$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, $cashtypeReadableSql 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 23:14:21 +01:00
$status = $this -> t [ " cashact " ][ $l ] . $zeile [ 'cashtype' ];
2020-11-19 22:47:44 +01:00
} 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:12:07 +01:00
if ( ! CommonUtils :: verifyBillByValues ( null , $billdate , $brutto_orig , $netto_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:12:48 +01:00
2020-11-19 23:14:21 +01:00
public static function getTranslateSqlCashTypeToReadable () {
$sql = " (IF(cashtype is null,'',CASE WHEN cashtype='1' THEN ' Pr.entn.' WHEN cashtype='2' THEN ' Pr.einl.'
WHEN cashtype = '3' THEN ' Geldtransit' WHEN cashtype = '4' THEN ' Lohnzahlung'
2020-11-19 23:14:48 +01:00
WHEN cashtype = '5' THEN ' Einzahlung' WHEN cashtype = '6' THEN ' Auszahlung' WHEN cashtype = '7' THEN ' TG an AN' WHEN cashtype = '8' THEN ' TG an AG' ELSE '' END )) as cashtype " ;
2020-11-19 23:14:21 +01:00
return $sql ;
}
2020-11-19 23:12:48 +01:00
private static function isBillAssignedToGuestAndPaid ( $pdo , $billid ) {
2020-11-19 23:14:48 +01:00
$sql = " SELECT paymentid,intguestid,COALESCE(intguestpaid,0) as intguestpaid,C.name as customername FROM %bill% B LEFT JOIN %customers% C ON B.intguestid=C.id WHERE B.id=? " ;
2020-11-19 23:12:48 +01:00
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $billid ));
if ( count ( $result ) > 0 ) {
$entry = $result [ 0 ];
if ( $entry [ " intguestpaid " ] == 1 ) {
return $entry [ " customername " ];
}
}
return null ;
}
2020-11-19 23:14:48 +01:00
public static function getNextBillIds ( $pdo ) {
$sql = " SELECT MAX(id) as maxid FROM %bill% " ;
$maxIdRes = CommonUtils :: fetchSqlAll ( $pdo , $sql );
$maxId = $maxIdRes [ 0 ][ " maxid " ];
$sql = " SELECT (COALESCE(MAX(billuid),0)) as maxbilluid FROM %bill% WHERE status is null OR status <> 'c' " ;
$maxuidRes = CommonUtils :: fetchSqlAll ( $pdo , $sql );
$maxBilluid = $maxuidRes [ 0 ][ " maxbilluid " ];
$nextBilluid = intval ( $maxBilluid ) + 1 ;
return array ( " maxid " => $maxId , " nextbilluid " => $nextBilluid );
}
2020-11-19 23:10:09 +01:00
}