<?php

require_once("../lib/dbconn.php");
$host=$pmshost;
$port=$pmsport;

//PERTAMA BACA DULU KALAU KOSONG BARU ISI
$package =chr(2)."0103E".chr(3); //READ CARD

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
    exit;
} else {
	  $result = socket_connect($socket, $host, $port);
	  //PERTAMA BACA DULU KALAU KOSONG BARU ISI
	  $package =chr(2)."0103E".chr(3); //READ CARD
  
		  if ($result === false) {
		    echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
		    exit;
		   }else {
			   socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec'=>30, 'usec'=>10)); //SET TIME OUT 30detik
			   $test = socket_write($socket, $package, strlen($package));
			   $result = socket_read($socket,2048) or die('Could not read from socket');
				//echo $result;
				if($result){
					$nostx=str_replace(chr(2),"",$result);	
					$nordx=str_replace(chr(3),"",$nostx);	
					
					if(trim($nordx)!="03010|R|D|O|N|"){
							$room_number="";
							$guest_name="";
							$date_checkin="";
							$date_checkout="";
							$booking_id="";
							$action="erase";
							
							$hasil = explode("|",trim($nordx));
							foreach($hasil as $key){
								
								if(substr($key,0,1)=="R"){
									$room_number = substr($key,1);
								}else if(substr($key,0,1)=="D"){
									$date_checkin = substr($key,1);
								}else if(substr($key,0,1)=="O"){
									$date_checkout = substr($key,1);
								}else if(substr($key,0,1)=="1"){
									$booking_id = substr($key,1);
								}else if(substr($key,0,1)=="N"){
									$guest_name = substr($key,1);
								}
							}
							$checkin_time="";
							if($date_checkin!=""){
								$year_checkin = substr($date_checkin,0,4);
								$month_checkin = substr($date_checkin,4,2);
								$day_checkin = substr($date_checkin,6,2);
								$hour_checkin = substr($date_checkin,8,2);
								$minute_checkin = substr($date_checkin,10,2);
								$checkin_time=$day_checkin."-".$month_checkin."-".$year_checkin." ".$hour_checkin.":".$minute_checkin;
							}
							$checkout_time="";
							if($date_checkout!=""){
								$year_checkout = substr($date_checkout,0,4);
								$month_checkout = substr($date_checkout,4,2);
								$day_checkout = substr($date_checkout,6,2);
								$hour_checkout = substr($date_checkout,8,2);
								$minute_checkout = substr($date_checkout,10,2);
								$checkout_time=$day_checkout."-".$month_checkout."-".$year_checkout." ".$hour_checkin.":".$minute_checkout;
							}
							//LOG HERE
							@mysql_query("insert into tbl_card_log (action, nama, room_number,checkin_date, checkout_date, result,booking_id) values ('$action','$guest_name','$room_number','$date_checkin','$date_checkout','$nordx','$booking_id')"); 
							
							$package2 =chr(2)."0103B|R".$room_number."|N".$guest_name."".chr(3); //CHECKIN 4 desember 2015 jam 1610
							
							socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec'=>30, 'usec'=>10)); //SET TIME OUT 30detik
							$test = socket_write($socket, $package2, strlen($package2));
							$result = socket_read($socket,2048) or die('Could not read from socket');
							if($result){
								echo "sukses";
							} 
					}else{
						if(trim($nordx)=="03018"){ //KALAU TIDAK ADA KARTU DI SLOT
							echo "Masukkan kartu dan ulangi proses";	
						}else{
							echo"kartu ini sudah kosong, harap ganti dengan kartu lain ";
							
						}
						socket_close($socket);
						exit;
					}
				
			}else{
				echo "no reply";
				socket_close($socket);
				exit;
			}

	}
}
?>