2020-11-19 23:02:08 +01:00
< ? php
// Datenbank-Verbindungsparameter
require_once ( 'dbutils.php' );
require_once ( 'commonutils.php' );
require_once ( 'admin.php' );
require_once ( 'reports.php' );
require_once ( 'utilities/pdfexport.php' );
require_once ( 'utilities/Emailer.php' );
require_once ( '3rdparty/phpexcel/classes/PHPExcel.php' );
class Customers {
private static $rights = array (
" createNewCustomer " => array ( " loggedin " => 1 , " isadmin " => 0 , " rights " => array ( " customers " )),
" deleteCustomer " => array ( " loggedin " => 1 , " isadmin " => 0 , " rights " => array ( " customers " )),
" changeCustomer " => array ( " loggedin " => 1 , " isadmin " => 0 , " rights " => array ( " customers " )),
" getCustomers " => array ( " loggedin " => 1 , " isadmin " => 0 , " rights " => array ( " customers " )),
" createNewGroup " => array ( " loggedin " => 1 , " isadmin " => 0 , " rights " => array ( " customers " )),
" getGroups " => array ( " loggedin " => 1 , " isadmin " => 0 , " rights " => array ( " customers " )),
" changeGroup " => array ( " loggedin " => 1 , " isadmin " => 0 , " rights " => array ( " customers " )),
" deleteGroup " => array ( " loggedin " => 1 , " isadmin " => 0 , " rights " => array ( " customers " )),
" getVacations " => array ( " loggedin " => 1 , " isadmin " => 0 , " rights " => array ( " customers " )),
" newVacation " => array ( " loggedin " => 1 , " isadmin " => 0 , " rights " => array ( " customers " )),
" delVacation " => array ( " loggedin " => 1 , " isadmin " => 0 , " rights " => array ( " customers " )),
" getCustomersForReserv " => array ( " loggedin " => 1 , " isadmin " => 0 , " rights " => array ( " reservation " )),
" getBills " => array ( " loggedin " => 1 , " isadmin " => 0 , " rights " => array ( " customers " )),
" pay " => array ( " loggedin " => 1 , " isadmin " => 0 , " rights " => array ( " customers " )),
" unpay " => array ( " loggedin " => 1 , " isadmin " => 0 , " rights " => array ( " customers " )),
" printbill " => array ( " loggedin " => 1 , " isadmin " => 0 , " rights " => array ( " customers " )),
" payallbills " => array ( " loggedin " => 1 , " isadmin " => 0 , " rights " => array ( " customers " )),
" printallbills " => array ( " loggedin " => 1 , " isadmin " => 0 , " rights " => array ( " customers " )),
2020-11-19 23:02:12 +01:00
" emailGroup " => array ( " loggedin " => 1 , " isadmin " => 0 , " rights " => array ( " customers " )),
" exportLog " => array ( " loggedin " => 1 , " isadmin " => 0 , " rights " => array ( " customers " )),
2020-11-19 23:11:36 +01:00
" getPaymentsForGuest " => array ( " loggedin " => 1 , " isadmin " => 0 , " rights " => array ( " customers " )),
" reportbills " => array ( " loggedin " => 1 , " isadmin " => 0 , " rights " => array ( " customers " )),
2020-11-19 23:11:42 +01:00
" reportcustomerbills " => array ( " loggedin " => 1 , " isadmin " => 0 , " rights " => array ( " customers " )),
2020-11-19 23:11:36 +01:00
" payallguests " => array ( " loggedin " => 1 , " isadmin " => 0 , " rights " => array ( " customers " ))
2020-11-19 23:02:08 +01:00
);
2020-11-19 23:02:12 +01:00
public static $CANCEL = 0 ;
public static $PAY = 1 ;
2020-11-19 23:11:36 +01:00
private static $CUS_BILL_ID = array ( " Bon-ID " , " Bill ID " , " ID " );
private static $CUS_OVERVIEW = array ( " Übersicht über alle unbezahlten Gästerechnungen " , " Overview of unpaid customer bills " , " Resumen de las facturas non-pagadas de huéspedes " );
private static $CUS_CREATED = array ( " Erstellt " , " Created " , " Creado " );
private static $CUS_DATE = array ( " Datum " , " Date " , " Fecha " );
private static $CUS_BILL_SUM = array ( " Betrag " , " Sum " , " Suma " );
private static $CUS_SUM = array ( " Summe " , " Sum " , " Suma " );
private static $CUS_NO_BILLS = array ( " Keine offenen Rechnungen " , " No unpaid bills " , " No hay ninguna factura non-pagada " );
2020-11-19 23:02:08 +01:00
private static function checkRights ( $command ) {
if ( session_id () == '' ) {
session_start ();
}
if ( ! array_key_exists ( $command , self :: $rights )) {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_COMMAND_NOT_FOUND , " msg " => ERROR_COMMAND_NOT_FOUND_MSG ));
return false ;
}
$cmdRights = self :: $rights [ $command ];
if ( $cmdRights [ " loggedin " ] == 1 ) {
if ( ! isset ( $_SESSION [ 'angemeldet' ]) || ! $_SESSION [ 'angemeldet' ]) {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_NOT_AUTHOTRIZED , " msg " => ERROR_NOT_AUTHOTRIZED_MSG ));
return false ;
}
}
if ( $cmdRights [ " isadmin " ] == 1 ) {
if ( ! isset ( $_SESSION [ 'angemeldet' ]) || ! $_SESSION [ 'angemeldet' ]) {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_NOT_AUTHOTRIZED , " msg " => ERROR_NOT_AUTHOTRIZED_MSG ));
return false ;
} else {
if ( $_SESSION [ 'is_admin' ] == 0 ) {
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_COMMAND_NOT_ADMIN , " msg " => ERROR_COMMAND_NOT_ADMIN_MSG ));
return false ;
}
}
}
if ( ! is_null ( $cmdRights [ " rights " ])) {
foreach ( $cmdRights [ " rights " ] as $aRight ) {
if ( $aRight == 'customers' ) {
if ( $_SESSION [ 'right_customers' ] == 1 ) {
return true ;
}
}
if ( $aRight == 'reservation' ) {
if ( $_SESSION [ 'right_reservation' ] == 1 ) {
return true ;
}
}
}
echo json_encode ( array ( " status " => " ERROR " , " code " => ERROR_NOT_AUTHOTRIZED , " msg " => ERROR_NOT_AUTHOTRIZED_MSG ));
return false ;
}
return true ;
}
function __construct () {
//
}
function handleCommand ( $command ) {
2020-11-19 23:11:42 +01:00
if (( $command == " reportbills " ) || ( $command == " reportcustomerbills " )) {
2020-11-19 23:11:36 +01:00
if ( session_id () == '' ) {
session_start ();
}
if ( ! isset ( $_SESSION [ 'right_customers' ])) {
echo " ERROR: no sufficient rights " ;
return ;
}
if ( ! $_SESSION [ 'right_customers' ]) {
echo " ERROR: no sufficient rights " ;
return ;
}
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
2020-11-19 23:11:42 +01:00
self :: $command ( $pdo );
2020-11-19 23:11:36 +01:00
return ;
}
2020-11-19 23:02:08 +01:00
if ( ! self :: checkRights ( $command )) {
return false ;
}
$pdo = DbUtils :: openDbAndReturnPdoStatic ();
if ( $command == 'createNewCustomer' ) {
2020-11-19 23:02:19 +01:00
echo json_encode ( $this -> createNewCustomer ( $pdo , $_POST [ 'name' ], $_POST [ 'email' ], $_POST [ 'addr' ], $_POST [ 'remark' ], $_POST [ 'phone' ], $_POST [ 'mobil' ], $_POST [ 'www' ], $_POST [ 'hello' ], $_POST [ 'regards' ], $_POST [ 'checkin' ], $_POST [ 'checkout' ], $_POST [ 'room' ]));
2020-11-19 23:02:08 +01:00
} else if ( $command == 'deleteCustomer' ) {
echo json_encode ( $this -> deleteCustomer ( $pdo , $_POST [ 'id' ]));
} else if ( $command == 'changeCustomer' ) {
2020-11-19 23:02:19 +01:00
echo json_encode ( $this -> changeCustomer ( $pdo , $_POST [ " id " ], $_POST [ 'name' ], $_POST [ 'email' ], $_POST [ 'addr' ], $_POST [ 'remark' ], $_POST [ 'phone' ], $_POST [ 'mobil' ], $_POST [ 'www' ], $_POST [ 'hello' ], $_POST [ 'regards' ], $_POST [ " groups " ]));
2020-11-19 23:02:08 +01:00
} else if ( $command == 'getCustomers' ) {
2020-11-19 23:11:36 +01:00
echo json_encode ( self :: getCustomers ( $pdo , $_POST [ 'search' ], $_POST [ 'date' ], $_POST [ 'onlyopenbills' ]));
2020-11-19 23:02:08 +01:00
} else if ( $command == 'createNewGroup' ) {
echo json_encode ( $this -> createNewGroup ( $pdo , $_POST [ 'name' ], $_POST [ 'remark' ]));
} else if ( $command == 'getGroups' ) {
echo json_encode ( $this -> getGroups ( $pdo , $_POST [ 'search' ]));
} else if ( $command == 'changeGroup' ) {
echo json_encode ( $this -> changeGroup ( $pdo , $_POST [ " id " ], $_POST [ 'name' ], $_POST [ 'remark' ]));
} else if ( $command == 'deleteGroup' ) {
echo json_encode ( $this -> deleteGroup ( $pdo , $_POST [ 'id' ]));
} else if ( $command == 'getCustomersForReserv' ) {
echo json_encode ( $this -> getCustomersForReserv ( $pdo , $_POST [ 'search' ]));
} else if ( $command == 'getVacations' ) {
echo json_encode ( $this -> getVacations ( $pdo , $_GET [ 'cusid' ]));
} else if ( $command == 'newVacation' ) {
echo json_encode ( $this -> newVacation ( $pdo , $_POST [ 'id' ], $_POST [ 'checkin' ], $_POST [ 'checkout' ], $_POST [ 'room' ], $_POST [ 'remark' ]));
} else if ( $command == 'delVacation' ) {
echo json_encode ( $this -> delVacation ( $pdo , $_POST [ 'id' ]));
} else if ( $command == 'getBills' ) {
2020-11-19 23:11:36 +01:00
echo json_encode ( self :: getAllBills ( $pdo , $_GET [ 'cusid' ]));
2020-11-19 23:02:08 +01:00
} else if ( $command == 'pay' ) {
2020-11-19 23:02:12 +01:00
echo json_encode ( $this -> pay ( $pdo , $_POST [ 'id' ], $_POST [ 'paymentid' ]));
2020-11-19 23:02:08 +01:00
} else if ( $command == 'unpay' ) {
2020-11-19 23:02:12 +01:00
echo json_encode ( $this -> unpay ( $pdo , $_POST [ 'id' ], $_POST [ " code " ], $_POST [ " remark " ]));
2020-11-19 23:02:08 +01:00
} else if ( $command == 'printbill' ) {
echo json_encode ( $this -> printBill ( $pdo , $_POST [ 'id' ]));
} else if ( $command == 'payallbills' ) {
2020-11-19 23:02:12 +01:00
echo json_encode ( $this -> payallbills ( $pdo , $_POST [ 'id' ], $_POST [ 'paymentid' ]));
2020-11-19 23:02:08 +01:00
} else if ( $command == 'printallbills' ) {
echo json_encode ( $this -> printallbills ( $pdo , $_POST [ 'id' ]));
} else if ( $command == 'emailGroup' ) {
2020-11-19 23:02:19 +01:00
echo json_encode ( $this -> emailGroup ( $pdo , $_POST [ 'groupid' ], $_POST [ " subject " ], $_POST [ " bcc " ], $_POST [ " text " ]));
2020-11-19 23:02:12 +01:00
} else if ( $command == 'getPaymentsForGuest' ) {
echo json_encode ( $this -> getPaymentsForGuest ( $pdo ));
} else if ( $command == 'exportLog' ) {
self :: exportLog ( $pdo );
2020-11-19 23:11:36 +01:00
} else if ( $command == 'payallguests' ) {
echo json_encode ( self :: payallguests ( $pdo , $_POST [ 'paymentid' ]));
2020-11-19 23:02:08 +01:00
} else {
echo " Command not supported. " ;
}
}
// for internal request
private function hasCurrentUserCustomersRights () {
session_start ();
if ( ! isset ( $_SESSION [ 'angemeldet' ]) || ! $_SESSION [ 'angemeldet' ]) {
// no user logged in
return false ;
} else {
return ( $_SESSION [ 'right_customers' ]);
}
}
2020-11-19 23:02:19 +01:00
private function createNewCustomer ( $pdo , $name , $email , $addr , $remark , $phone , $mobil , $www , $hello , $regards , $checkin , $checkout , $room ) {
2020-11-19 23:02:08 +01:00
date_default_timezone_set ( DbUtils :: getTimeZone ());
$currentTime = date ( 'Y-m-d H:i:s' );
if ( $checkin == '' ) {
$checkin = null ;
}
if ( $checkout == '' ) {
$checkout = null ;
}
if ( $room == '' ) {
$room = null ;
}
2020-11-19 23:02:19 +01:00
if ( $hello == '' ) {
$hello = null ;
}
if ( $regards == '' ) {
$regards = null ;
}
2020-11-19 23:02:08 +01:00
try {
$pdo -> beginTransaction ();
$sql = " SELECT count(id) as countid from %customers% WHERE name=? " ;
$row = CommonUtils :: getRowSqlObject ( $pdo , $sql , array ( $name ));
$number = $row -> countid ;
if ( $row -> countid > 0 ) {
$code = 1 ;
} else {
$code = 0 ;
}
2020-11-19 23:02:19 +01:00
$sql = " INSERT INTO %customers% (name,email,address,remark,phone,mobil,www,hello,regards,created,lastmodified) VALUES(?,?,?,?,?,?,?,?,?,?,?) " ;
2020-11-19 23:02:08 +01:00
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
2020-11-19 23:02:19 +01:00
$stmt -> execute ( array ( $name , $email , $addr , $remark , $phone , $mobil , $www , $hello , $regards , $currentTime , $currentTime ));
2020-11-19 23:02:08 +01:00
$cusid = $pdo -> lastInsertId ();
self :: addVacationsCore ( $pdo , $cusid , $checkin , $checkout , $room , null );
2020-11-19 23:02:12 +01:00
$pdo -> commit ();
2020-11-19 23:02:08 +01:00
return array ( " status " => " OK " , " code " => $code , " value " => $number );
} catch ( Exception $e ) {
$pdo -> rollBack ();
return array ( " status " => " ERROR " , " msg " => $e -> getMessage ());
}
}
private static function addVacationsCore ( $pdo , $cusid , $checkin , $checkout , $room , $remark ) {
if ( $checkin == '' ) {
$checkin = null ;
}
if ( $checkout == '' ) {
$checkout = null ;
}
if ( ! is_null ( $checkin ) || ! is_null ( $checkout )) {
$sql = " INSERT INTO %vacations% (customerid,checkin,checkout,room,remark) VALUES(?,?,?,?,?) " ;
CommonUtils :: execSql ( $pdo , $sql , array ( $cusid , $checkin , $checkout , $room , $remark ));
}
}
private function createNewGroup ( $pdo , $name , $remark ) {
date_default_timezone_set ( DbUtils :: getTimeZone ());
$currentTime = date ( 'Y-m-d H:i:s' );
try {
2020-11-19 23:12:00 +01:00
$sql = " SELECT count(id) as countid from `%groups%` WHERE name=? " ;
2020-11-19 23:02:08 +01:00
$row = CommonUtils :: getRowSqlObject ( $pdo , $sql , array ( $name ));
if ( $row -> countid > 0 ) {
return array ( " status " => " ERROR " , " msg " => " Group name already exists " , " code " => 1 );
}
2020-11-19 23:12:00 +01:00
$sql = " INSERT INTO `%groups%` (name,remark,created) VALUES(?,?,?) " ;
2020-11-19 23:02:08 +01:00
CommonUtils :: execSql ( $pdo , $sql , array ( $name , $remark , $currentTime ));
return array ( " status " => " OK " );
} catch ( Exception $e ) {
return array ( " status " => " ERROR " , " msg " => $e -> getMessage (), " code " => 2 );
}
}
2020-11-19 23:02:19 +01:00
private function changeCustomer ( $pdo , $id , $name , $email , $addr , $remark , $phone , $mobil , $www , $hello , $regards , $groups ) {
2020-11-19 23:02:08 +01:00
date_default_timezone_set ( DbUtils :: getTimeZone ());
$currentTime = date ( 'Y-m-d H:i:s' );
try {
2020-11-19 23:02:19 +01:00
$sql = " UPDATE %customers% SET name=?,email=?,address=?, remark=?, phone=?, mobil=?, www=?,hello=?,regards=?,lastmodified=? WHERE id=? " ;
CommonUtils :: execSql ( $pdo , $sql , array ( $name , $email , $addr , $remark , $phone , $mobil , $www , $hello , $regards , $currentTime , $id ));
2020-11-19 23:02:08 +01:00
$sql = " DELETE FROM %groupcustomer% WHERE customerid=? " ;
CommonUtils :: execSql ( $pdo , $sql , array ( $id ));
if ( $groups != '' ) {
foreach ( $groups as $aGroupId ) {
$sql = " INSERT INTO %groupcustomer% (customerid,groupid) VALUES(?,?) " ;
CommonUtils :: execSql ( $pdo , $sql , array ( $id , $aGroupId ));
}
}
return array ( " status " => " OK " );
} catch ( Exception $e ) {
return array ( " status " => " ERROR " , " msg " => $e -> getMessage ());
}
}
private function newVacation ( $pdo , $cusid , $checkin , $checkout , $room , $remark ) {
try {
self :: addVacationsCore ( $pdo , $cusid , $checkin , $checkout , $room , $remark );
return array ( " status " => " OK " , " cusid " => $cusid );
} catch ( Exception $e ) {
return array ( " status " => " ERROR " , " msg " => $e -> getMessage ());
}
}
private function delVacation ( $pdo , $id ) {
try {
$pdo -> beginTransaction ();
$sql = " SELECT count(id) as countid FROM %vacations% WHERE id=? " ;
$row = CommonUtils :: getRowSqlObject ( $pdo , $sql , array ( $id ));
if ( $row -> countid != 1 ) {
$pdo -> rollBack ();
return array ( " status " => " ERROR " , " msg " => " Vacations entry not found " );
} else {
$sql = " SELECT customerid FROM %vacations% WHERE id=? " ;
$row = CommonUtils :: getRowSqlObject ( $pdo , $sql , array ( $id ));
$cusid = $row -> customerid ;
$sql = " DELETE FROM %vacations% WHERE id=? " ;
CommonUtils :: execSql ( $pdo , $sql , array ( $id ));
$pdo -> commit ();
return array ( " status " => " OK " , " cusid " => $cusid );
}
} catch ( Exception $ex ) {
$pdo -> rollBack ();
return array ( " status " => " ERROR " , " msg " => $ex -> getMessage ());
}
}
2020-11-19 23:02:12 +01:00
private static function payOrUnpay ( $pdo , $id , $value , $doTransaction , $remark = null ) {
2020-11-19 23:02:08 +01:00
try {
2020-11-19 23:02:12 +01:00
if ( $doTransaction ) {
$pdo -> beginTransaction ();
}
2020-11-19 23:02:08 +01:00
$sql = " SELECT count(id) as countid FROM %bill% WHERE id=? " ;
$row = CommonUtils :: getRowSqlObject ( $pdo , $sql , array ( $id ));
if ( $row -> countid != 1 ) {
2020-11-19 23:02:12 +01:00
if ( $doTransaction ) {
$pdo -> rollBack ();
}
return array ( " status " => " ERROR " , " msg " => " Bill with id= $id not found " );
2020-11-19 23:02:08 +01:00
} else {
$sql = " SELECT intguestid FROM %bill% WHERE id=? " ;
$row = CommonUtils :: getRowSqlObject ( $pdo , $sql , array ( $id ));
$cusid = $row -> intguestid ;
$sql = " UPDATE %bill% SET intguestpaid=? WHERE id=? " ;
CommonUtils :: execSql ( $pdo , $sql , array ( $value , $id ));
2020-11-19 23:02:12 +01:00
date_default_timezone_set ( DbUtils :: getTimeZone ());
$currentTime = date ( 'Y-m-d H:i:s' );
if ( session_id () == '' ) {
session_start ();
}
$userid = $_SESSION [ 'userid' ];
$sql = " INSERT INTO %customerlog% (date,action,customerid,userid,billid,remark) VALUES(?,?,?,?,?,?) " ;
$action = ( is_null ( $value ) ? self :: $CANCEL : self :: $PAY );
CommonUtils :: execSql ( $pdo , $sql , array ( $currentTime , $action , $cusid , $userid , $id , $remark ));
if ( $doTransaction ) {
$pdo -> commit ();
}
2020-11-19 23:02:08 +01:00
return array ( " status " => " OK " , " cusid " => $cusid );
}
} catch ( Exception $ex ) {
2020-11-19 23:02:12 +01:00
if ( $doTransaction ) {
$pdo -> rollBack ();
}
2020-11-19 23:02:08 +01:00
return array ( " status " => " ERROR " , " msg " => $ex -> getMessage ());
}
}
2020-11-19 23:11:36 +01:00
private static function payallguests ( $pdo , $paymentid ) {
$paymentname = self :: getPaymentName ( $pdo , $paymentid );
$pdo -> beginTransaction ();
$allGuestWithUnpaidBills = self :: getCustomers ( $pdo , '' , '' , true );
foreach ( $allGuestWithUnpaidBills [ " msg " ] as $g ) {
$customerid = $g [ " id " ];
$billsOfGuest = self :: getUnpaidBills ( $pdo , $customerid )[ " msg " ][ " bills " ];
foreach ( $billsOfGuest as $b ) {
$billid = $b [ " id " ];
$ret = self :: payOrUnpay ( $pdo , $billid , 1 , false , $paymentname );
if ( $ret [ " status " ] != " OK " ) {
$pdo -> rollBack ();
return $ret ;
}
}
}
$pdo -> commit ();
return array ( " status " => " OK " );
}
2020-11-19 23:02:12 +01:00
private function pay ( $pdo , $id , $paymentid ) {
$paymentname = self :: getPaymentName ( $pdo , $paymentid );
return ( self :: payOrUnpay ( $pdo , $id , 1 , true , $paymentname ));
2020-11-19 23:02:08 +01:00
}
2020-11-19 23:02:12 +01:00
private function unpay ( $pdo , $id , $code , $remark ) {
$stornocode = CommonUtils :: getConfigValue ( $pdo , 'cancelguestcode' , null );
if ( is_null ( $stornocode )) {
return array ( " status " => " ERROR " , " msg " => " Es wurde noch kein Stornocode in der Verwaltung festgelegt " );
}
if ( $stornocode != $code ) {
return array ( " status " => " ERROR " , " msg " => " Falscher Stornocode " );
}
return ( self :: payOrUnpay ( $pdo , $id , null , true , $remark ));
2020-11-19 23:02:08 +01:00
}
private function printBill ( $pdo , $billid ) {
if ( session_id () == '' ) {
session_start ();
}
$printer = $_SESSION [ 'receiptprinter' ];
// now get receipt info from bill table
CommonUtils :: log ( $pdo , " PRINTQUEUE " , " Insert bill with id= $billid for printer= $printer into queue for customer. " );
$printInsertSql = " INSERT INTO `%printjobs%` (`id` , `content`,`type`,`printer`) VALUES ( NULL,?,?,?) " ;
CommonUtils :: execSql ( $pdo , $printInsertSql , array (( string )( $billid ), '3' , $printer ));
return array ( " status " => " OK " );
}
private function printallbills ( $pdo , $cusid ) {
$sql = " SELECT id FROM %bill% WHERE intguestid=? " ;
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $cusid ));
foreach ( $result as $aBill ) {
$this -> printBill ( $pdo , $aBill [ " id " ]);
}
return array ( " status " => " OK " );
}
2020-11-19 23:02:12 +01:00
private function payallbills ( $pdo , $cusid , $paymentid ) {
2020-11-19 23:02:08 +01:00
try {
$pdo -> beginTransaction ();
2020-11-19 23:02:12 +01:00
$paymentname = self :: getPaymentName ( $pdo , $paymentid );
$sql = " SELECT id FROM %bill% WHERE intguestpaid is null AND intguestid=? " ;
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $cusid ));
foreach ( $result as $aBill ) {
$ok = self :: payOrUnpay ( $pdo , $aBill [ " id " ], 1 , false , $paymentname );
if ( $ok [ " status " ] != " OK " ) {
$pdo -> rollBack ();
return $ok ;
}
}
2020-11-19 23:02:08 +01:00
$pdo -> commit ();
return array ( " status " => " OK " , " cusid " => $cusid );
} catch ( Exception $ex ) {
$pdo -> rollBack ();
return array ( " status " => " ERROR " , " msg " => $ex -> getMessage ());
}
}
private function deleteCustomer ( $pdo , $id ) {
try {
$sql = " SELECT count(id) as countid FROM %bill% WHERE intguestid=? " ;
$row = CommonUtils :: getRowSqlObject ( $pdo , $sql , array ( $id ));
if ( $row -> countid > 0 ) {
return array ( " status " => " ERROR " , " msg " => " Der Gast hat zugewiesene Rechnungen " );
}
$pdo -> beginTransaction ();
$sql = " DELETE FROM %groupcustomer% WHERE customerid=? " ;
CommonUtils :: execSql ( $pdo , $sql , array ( $id ));
$sql = " DELETE FROM %vacations% WHERE customerid=? " ;
CommonUtils :: execSql ( $pdo , $sql , array ( $id ));
$sql = " DELETE FROM %customers% WHERE id=? " ;
CommonUtils :: execSql ( $pdo , $sql , array ( $id ));
$pdo -> commit ();
return array ( " status " => " OK " );
} catch ( Exception $e ) {
$pdo -> rollBack ();
return array ( " status " => " ERROR " , " msg " => $e -> getMessage ());
}
}
private function getCustomersForReserv ( $pdo , $search ) {
if ( $search == '' ) {
return array ( " status " => " OK " , " msg " => array ());
}
$s = '%' . $search . '%' ;
try {
$allcustomers = array ();
$sql = " SELECT id,name,email,address,remark,phone,mobil FROM %customers% WHERE (name like ?) OR (mobil like ?) OR (phone like ?) ORDER BY name " ;
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $s , $s , $s ));
foreach ( $result as $aCustomer ) {
$id = $aCustomer [ " id " ];
$name = $aCustomer [ " name " ];
$email = $aCustomer [ " email " ];
$phone = $aCustomer [ " phone " ];
$mobil = $aCustomer [ " mobil " ];
$finalPhone = " " ;
$summary = $aCustomer [ " name " ];
if ( $phone != " " ) {
$summary .= " - " . $phone ;
$finalPhone = $phone ;
}
if ( $mobil != " " ) {
$summary .= " - " . $mobil ;
$finalPhone = $mobil ;
}
if ( $email != " " ) {
$summary .= " - " . $email ;
}
$allcustomers [] = array ( " summary " => $summary , " id " => $id , " name " => $name , " email " => $email , " phone " => $finalPhone );
}
return array ( " status " => " OK " , " msg " => $allcustomers );
} catch ( Exception $ex ) {
return array ( " status " => " ERROR " , " msg " => $ex -> getMessage ());
}
}
private static function isCustomerPresent ( $pdo , $cusid , $date ) {
if ( $date == '' ) {
return true ;
}
$sql = " SELECT count(id) as countid FROM %vacations% WHERE customerid=? AND (checkin is not null OR checkout is not null) AND (IFNULL(checkin,' $date ') <= ? AND IFNULL(checkout,' $date ') >= ?) " ;
$row = CommonUtils :: getRowSqlObject ( $pdo , $sql , array ( $cusid , $date , $date ));
if ( $row -> countid == 0 ) {
return false ;
} else {
return true ;
}
}
2020-11-19 23:11:36 +01:00
private static function getCustomers ( $pdo , $search , $date , $onlyOpenBills ) {
2020-11-19 23:02:08 +01:00
$s = '%' . $search . '%' ;
try {
$allcustomers = array ();
2020-11-19 23:02:21 +01:00
$sql = " SELECT %customers%.id as id,name,email,address,remark,phone,mobil,www,IFNULL(hello,'') as hello,IFNULL(regards,'') as regards,DATE_FORMAT(created,'%e %b %Y - %k:%i') as created,DATE_FORMAT(lastmodified,'%e %b %Y - %k:%i') as lastmodified FROM %customers% WHERE (name like ?) OR (email like ?) ORDER BY name " ;
2020-11-19 23:02:08 +01:00
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $s , $s ));
foreach ( $result as $aCustomer ) {
if ( ! self :: isCustomerPresent ( $pdo , $aCustomer [ " id " ], $date )) {
continue ;
}
2020-11-19 23:12:00 +01:00
$sql = " SELECT groupid,name FROM %groupcustomer%,`%groups%` WHERE groupid=`%groups%`.id AND customerid=? ORDER BY name " ;
2020-11-19 23:02:08 +01:00
$assgroups = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $aCustomer [ " id " ]));
if ( $onlyOpenBills == 1 ) {
$sql = " SELECT count(id) as countid FROM %bill% WHERE intguestid=? AND intguestpaid is null " ;
$row = CommonUtils :: getRowSqlObject ( $pdo , $sql , array ( $aCustomer [ " id " ]));
if ( $row -> countid == 0 ) {
continue ;
}
}
$sql = " SELECT SUM(brutto) as openbillsum FROM %bill% WHERE intguestid=? AND intguestpaid is null " ;
$row = CommonUtils :: getRowSqlObject ( $pdo , $sql , array ( $aCustomer [ " id " ]));
$aCust = array ( " id " => $aCustomer [ " id " ],
" name " => $aCustomer [ " name " ],
" email " => $aCustomer [ " email " ],
" address " => $aCustomer [ " address " ],
" remark " => $aCustomer [ " remark " ],
" phone " => $aCustomer [ " phone " ],
" mobil " => $aCustomer [ " mobil " ],
" www " => $aCustomer [ " www " ],
2020-11-19 23:02:19 +01:00
" hello " => $aCustomer [ " hello " ],
" regards " => $aCustomer [ " regards " ],
2020-11-19 23:02:08 +01:00
" created " => $aCustomer [ " created " ],
" lastmodified " => $aCustomer [ " lastmodified " ],
" groups " => $assgroups ,
" openbillsum " => $row -> openbillsum
);
$allcustomers [] = $aCust ;
}
return array ( " status " => " OK " , " msg " => $allcustomers );
} catch ( Exception $ex ) {
return array ( " status " => " ERROR " , " msg " => $ex -> getMessage ());
}
}
private function getVacations ( $pdo , $cusid ) {
$sql = " SELECT id,IFNULL(checkin,'') as checkin,IFNULL(checkout,'') as checkout,IFNULL(room,'') as room,IFNULL(remark,'') as remark FROM %vacations% WHERE customerid=? " ;
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $cusid ));
return array ( " status " => " OK " , " msg " => array ( " vacations " => $result , " cusid " => $cusid ));
}
2020-11-19 23:11:36 +01:00
private static function getAllBills ( $pdo , $cusid ) {
return self :: getBills ( $pdo , $cusid , true );
}
private static function getUnpaidBills ( $pdo , $cusid ) {
return self :: getBills ( $pdo , $cusid , false );
}
private static function getBills ( $pdo , $cusid , $includePaidBills ) {
$where = " " ;
if ( ! $includePaidBills ) {
$where = " AND (intguestpaid IS NULL OR intguestpaid='0') " ;
}
$sql = " SELECT id,billdate,brutto,IFNULL(guestinfo,'') as guestinfo,IFNULL(intguestpaid,'0') as paid FROM %bill% WHERE intguestid=? $where " ;
2020-11-19 23:02:08 +01:00
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $cusid ));
return array ( " status " => " OK " , " msg " => array ( " bills " => $result , " cusid " => $cusid ));
}
private function getGroups ( $pdo , $search ) {
$s = '%' . $search . '%' ;
try {
2020-11-19 23:12:00 +01:00
$sql = " SELECT id,name,remark,DATE_FORMAT(created,'%e %b %Y - %k:%i') as created FROM `%groups%` WHERE name like ? ORDER BY name " ;
2020-11-19 23:02:08 +01:00
$resultFiltered = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $s ));
$resultFoundGroups = array ();
foreach ( $resultFiltered as $aFilteredGroup ) {
$groupid = $aFilteredGroup [ " id " ];
$sql = " SELECT %customers%.name as name from %customers%,%groupcustomer% WHERE %groupcustomer%.groupid=? AND %groupcustomer%.customerid=%customers%.id ORDER by name " ;
$customersInGroup = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $groupid ));
$resultFoundGroups [] = array (
" id " => $groupid ,
" name " => $aFilteredGroup [ " name " ],
" remark " => $aFilteredGroup [ " remark " ],
" created " => $aFilteredGroup [ " created " ],
" customers " => $customersInGroup );
}
2020-11-19 23:12:00 +01:00
$sql = " SELECT id,name FROM `%groups%` ORDER BY name " ;
2020-11-19 23:02:08 +01:00
$resultAll = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $s ));
$result = array ( " filtered " => $resultFoundGroups , " all " => $resultAll );
return array ( " status " => " OK " , " msg " => $result );
} catch ( Exception $ex ) {
return array ( " status " => " ERROR " , " msg " => $ex -> getMessage ());
}
}
private function changeGroup ( $pdo , $id , $name , $remark ) {
try {
2020-11-19 23:12:00 +01:00
$sql = " UPDATE `%groups%` SET name=?,remark=? WHERE id=? " ;
2020-11-19 23:02:08 +01:00
CommonUtils :: execSql ( $pdo , $sql , array ( $name , $remark , $id ));
return array ( " status " => " OK " );
} catch ( Exception $e ) {
return array ( " status " => " ERROR " , " msg " => $e -> getMessage ());
}
}
private function deleteGroup ( $pdo , $id ) {
try {
$sql = " DELETE FROM %groupcustomer% WHERE groupid=? " ;
CommonUtils :: execSql ( $pdo , $sql , array ( $id ));
2020-11-19 23:12:00 +01:00
$sql = " DELETE FROM `%groups%` WHERE id=? " ;
2020-11-19 23:02:08 +01:00
CommonUtils :: execSql ( $pdo , $sql , array ( $id ));
return array ( " status " => " OK " );
} catch ( Exception $e ) {
return array ( " status " => " ERROR " , " msg " => $e -> getMessage ());
}
}
2020-11-19 23:02:19 +01:00
private function emailGroup ( $pdo , $grpid , $subject , $bcc , $text ) {
$sql = " SELECT %customers%.name as name,IFNULL(%customers%.hello,'') as hello,IFNULL(%customers%.regards,'') as regards ,%customers%.email as email from %customers%,%groupcustomer% WHERE %groupcustomer%.groupid=? AND %groupcustomer%.customerid=%customers%.id ORDER by name " ;
2020-11-19 23:02:08 +01:00
$customersInGroup = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $grpid ));
$emails = array ();
foreach ( $customersInGroup as $aCustomer ) {
$email = $aCustomer [ " email " ];
if ( ! is_null ( $email )) {
$email = trim ( $email );
if ( $email != '' ) {
2020-11-19 23:02:19 +01:00
$emails [] = array ( " email " => $email , " hello " => $aCustomer [ " hello " ], " regards " => $aCustomer [ " regards " ], " name " => $aCustomer [ " name " ]);
2020-11-19 23:02:08 +01:00
}
}
}
$ok = true ;
2020-11-19 23:02:19 +01:00
foreach ( $emails as $anEmailEntry ) {
$anEmail = $anEmailEntry [ " email " ];
$name = $anEmailEntry [ " name " ];
$hello = $anEmailEntry [ " hello " ];
$regards = $anEmailEntry [ " regards " ];
$textToSend = str_replace ( " { NAME} " , $name , $text );
$textToSend = str_replace ( " { ANREDE} " , $hello , $textToSend );
$textToSend = str_replace ( " { GRUSS} " , $regards , $textToSend );
if ( ! Emailer :: sendEmail ( $pdo , $textToSend , $anEmail , $subject , $bcc )) {
2020-11-19 23:02:08 +01:00
$ok = false ;
}
}
if ( $ok ) {
return array ( " status " => " OK " );
} else {
return array ( " status " => " ERROR " , " msg " => 'Not all emails could be sent' );
}
}
2020-11-19 23:02:12 +01:00
public static function exportLog ( $pdo , $startDate = null , $endDate = null ) {
header ( " Content-type: text/x-csv " );
header ( " Content-Disposition: attachment; filename=Gastbezahlungen.csv " );
header ( " Cache-Control: must-revalidate, post-check=0, pre-check=0 " );
header ( " Pragma: no-cache " );
header ( " Expires: 0 " );
echo ( " Datum; Gast-ID; Gastname; Bon-ID; Aktion; Benutzer-ID; Benutzername; Stornobemerkung oder Zahlungsweg \n " );
$sql = " SELECT %customerlog%.date as date,CASE WHEN action=0 THEN 'Bezahlung storniert' WHEN action=1 THEN 'bezahlt' ELSE 'undefiniert' END as action, " ;
$sql .= " %customerlog%.billid as billid,%customerlog%.remark as remark, userid, username, customerid, %user%.username as username, %customers%.name as customername " ;
$sql .= " FROM %customerlog%,%user%,%customers% " ;
$sql .= " WHERE (userid=%user%.id AND customerid=%customers%.id) " ;
2020-11-19 23:11:44 +01:00
$sql .= " ORDER BY %customerlog%.date " ;
2020-11-19 23:02:12 +01:00
$where = '' ;
if ( is_null ( $startDate ) && ( ! is_null ( $endDate ))) {
$where = " AND (date <= ' $endDate ') " ;
} else if ( ! is_null ( $startDate ) && ( ! is_null ( $endDate ))) {
$where = " AND (date <= ' $endDate ') AND (date >= ' $startDate ') " ;
}
$sql .= $where ;
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , null );
foreach ( $result as $aLog ) {
echo $aLog [ " date " ] . " ; " ;
echo $aLog [ " customerid " ] . " ; " ;
echo self :: quoteForCsv ( $aLog [ " customername " ]) . " ; " ;
echo $aLog [ " billid " ] . " ; " ;
echo $aLog [ " action " ] . " ; " ;
echo $aLog [ " userid " ] . " ; " ;
echo self :: quoteForCsv ( $aLog [ " username " ]) . " ; " ;
echo self :: quoteForCsv ( $aLog [ " remark " ]) . " ; " ;
echo " \r \n " ;
}
}
public static function quoteForCsv ( $txt ) {
$txt = str_replace ( " \" " , " \" \" " , $txt );
return '"' . $txt . '"' ;
}
private function getPaymentsForGuest ( $pdo ) {
if ( session_id () == '' ) {
session_start ();
}
$where = " WHERE (id <> 7) AND (id <> 8) " ;
$lang = $_SESSION [ 'language' ];
$sql = " SELECT id,name FROM %payment% $where " ;
if ( $lang == 1 ) {
$sql = " SELECT id,name_en as name FROM %payment% $where " ;
} else if ( $lang == 2 ) {
$sql = " SELECT id,name_esp as name FROM %payment% $where " ;
}
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , null );
return array ( " status " => " OK " , " msg " => $result );
}
private static function getPaymentName ( $pdo , $paymentid ) {
if ( session_id () == '' ) {
session_start ();
}
$lang = $_SESSION [ 'language' ];
$sql = " SELECT id,name FROM %payment% WHERE id=? " ;
if ( $lang == 1 ) {
$sql = " SELECT id,name_en as name FROM %payment% WHERE id=? " ;
} else if ( $lang == 2 ) {
$sql = " SELECT id,name_esp as name FROM %payment% WHERE id=? " ;
}
$row = CommonUtils :: getRowSqlObject ( $pdo , $sql , array ( $paymentid ));
return $row -> name ;
2020-11-19 23:11:36 +01:00
}
private static function getUnpaidSumOfCustomer ( $pdo , $cusid , $decpoint ) {
$sql = " SELECT sum(brutto) AS sumbrutto FROM %bill% WHERE intguestid=? AND (intguestpaid IS NULL OR intguestpaid='0') " ;
$result = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $cusid ));
if ( count ( $result ) == 0 ) {
return " 0.00 " ;
} else {
return number_format ( $result [ 0 ][ " sumbrutto " ], 2 , $decpoint , '' );
}
}
2020-11-19 23:11:42 +01:00
private static function getReportCoreOfOneCustomer ( $pdo , $customerid , $lang ) {
2020-11-19 23:11:36 +01:00
$decpoint = CommonUtils :: getConfigValue ( $pdo , " decpoint " , " , " );
$currency = CommonUtils :: getConfigValue ( $pdo , " currency " , " Euro " );
2020-11-19 23:11:42 +01:00
$sql = " SELECT name FROM %customers% WHERE id=? " ;
$res = CommonUtils :: fetchSqlAll ( $pdo , $sql , array ( $customerid ));
if ( count ( $res ) != 1 ) {
return " " ;
}
$customername = $res [ 0 ][ " name " ];
$billsOfGuest = self :: getUnpaidBills ( $pdo , $customerid )[ " msg " ][ " bills " ];
$billcount = count ( $billsOfGuest ) + 2 ;
$txt = " <tr> " ;
$txt .= " <td rowspan=' $billcount ' class='name'> $customername " ;
$txt .= " <td class='header'> " . self :: $CUS_BILL_ID [ $lang ] . " <td class='header'> " . self :: $CUS_DATE [ $lang ] . " <td class='header'> " . self :: $CUS_BILL_SUM [ $lang ] . " ( $currency )</tr> " ;
foreach ( $billsOfGuest as $aBill ) {
$txt .= " <tr><td> " . $aBill [ " id " ] . " <td> " . $aBill [ " billdate " ];
$txt .= " <td> " . number_format ( $aBill [ " brutto " ], 2 , $decpoint , '' ) . " </tr> " ;
}
$guestsum = self :: getUnpaidSumOfCustomer ( $pdo , $customerid , $decpoint );
$txt .= " <tr><td colspan=2 class='sum sumheader'> " . self :: $CUS_SUM [ $lang ] . " <td class='sum sumvalue'> $guestsum </tr> " ;
return $txt ;
}
private static function reportcustomerbills ( $pdo ) {
$customerid = $_GET [ " cusid " ];
if ( session_id () == '' ) {
session_start ();
}
$lang = $_SESSION [ 'language' ];
$txt = " <html> " . self :: headerOfHtmlPage ( $lang ) . " <body> " ;
$txt .= self :: getGuestReportTitlePart ( $lang );
$txt .= " <p><table class='guestreport'> " ;
$txt .= self :: getReportCoreOfOneCustomer ( $pdo , $customerid , $lang );
$txt .= " </table> " ;
$txt .= self :: getFooter ( $pdo , $lang );
$txt .= " </body></html> " ;
echo $txt ;
}
private static function reportbills ( $pdo ) {
2020-11-19 23:11:36 +01:00
if ( session_id () == '' ) {
session_start ();
}
$lang = $_SESSION [ 'language' ];
$allGuestWithUnpaidBills = self :: getCustomers ( $pdo , '' , '' , true );
if ( $allGuestWithUnpaidBills [ " status " ] != " OK " ) {
echo " Error: " . $allGuestWithUnpaidBills [ " msg " ];
return ;
}
$txt = " <html> " . self :: headerOfHtmlPage ( $lang ) . " <body> " ;
$txt .= self :: getGuestReportTitlePart ( $lang );
$txt .= " <p><table class='guestreport'> " ;
if ( count ( $allGuestWithUnpaidBills [ " msg " ]) === 0 ) {
$txt .= " <tr><td class='center'> " . self :: $CUS_NO_BILLS [ $lang ] . " </tr> " ;
} else {
foreach ( $allGuestWithUnpaidBills [ " msg " ] as $aGuest ) {
$customerid = $aGuest [ " id " ];
2020-11-19 23:11:42 +01:00
$txt .= self :: getReportCoreOfOneCustomer ( $pdo , $customerid , $lang );
2020-11-19 23:11:36 +01:00
}
}
$txt .= " </table> " ;
2020-11-19 23:11:42 +01:00
$txt .= self :: getFooter ( $pdo , $lang );
2020-11-19 23:11:36 +01:00
$txt .= " </body></html> " ;
echo $txt ;
}
private static function headerOfHtmlPage ( $lang ) {
$txt = " <head> " ;
$txt .= " <title> " . self :: $CUS_OVERVIEW [ $lang ] . " </title> " ;
$txt .= '<meta http-equiv="content-type" content="text/html; charset=utf-8">' ;
2020-11-19 23:12:13 +01:00
$txt .= '<link rel="stylesheet" type="text/css" href="../css/guestreport.css?v=1.5.16">' ;
2020-11-19 23:11:36 +01:00
$txt .= " </head> " ;
return $txt ;
}
private static function getGuestReportTitlePart ( $lang ) {
date_default_timezone_set ( DbUtils :: getTimeZone ());
$currentTime = date ( 'Y-m-d H:i' );
$txt = " <div class='title'> " ;
$txt .= " <h1> " . self :: $CUS_OVERVIEW [ $lang ] . " </h1> " ;
$txt .= " <p> " . self :: $CUS_CREATED [ $lang ] . " : " . $currentTime ;
$txt .= " </div> " ;
return $txt ;
}
2020-11-19 23:11:42 +01:00
private static function getFooter ( $pdo , $lang ) {
$version = CommonUtils :: getConfigValue ( $pdo , " version " , " " );
2020-11-19 23:11:36 +01:00
$txt = " <div class='footer'> " ;
$txt .= " <p>OrderSprinter $version " ;
$txt .= " </div> " ;
return $txt ;
}
2020-11-19 23:02:12 +01:00
}