<?php 
   
$PID = "cancel_checkin";
$SC = $_SERVER["SCRIPT_NAME"];

require_once("lib/dbconn.php");
require_once("lib/form.php");
require_once("lib/functions.php");



if(isset($_GET[idx])){
	title("<img src='images/sales-report-icon.png' width='65' align='absmiddle' >  Cancel Check In");
	include_once('keterangan.php');
	
	$cek = getFromTable("select sum(jumlah) from tbl_transaksi where no_booking = '".$_GET[idx]."'");
	
	?>
	<br/>
	<div class="ui three column stackable grid" align="center">
		
		<div class="column">
			<h4 class="ui top attached block header">Select Reason Cancel Check In!</h4>
			<form action="actions/cancel_checkin_insert.php" method="POST">
			<input type="hidden" name="p" value="<?php echo $PID;?>"/>
			<input type="hidden" name="idx" value="<?php echo $_GET["idx"];?>"/>
			<input type="hidden" name="bill" value="<?php echo $cek;?>"/>
			<table width="100%" class="gridtable_form" border="1px">						
				<tr>
					<td align="center"> <?php echo query_alasan("alasan", "001");?> </td>
				</tr>
				<tr>
					<td align="center"><input type="submit" value="Cancel Checkin" ></td>
				</tr>
			</table>
			</form>
		</div>
	
	<div class="column">
		<?php
		if ($cek > 0) {
			echo "<h4 class='ui top attached error header'>Tamu ini telah mempunyai tagihan</h4>";
		}
		?>
	</div>
	<div class="column">
		<table class="gridtable" width="95%" border="1px">
				<tr>
					<th bgcolor="#2b92f9">Items</th>
					<th bgcolor="#2b92f9">Price</th>
					<th bgcolor="#2b92f9">Qty</th>
					<th bgcolor="#2b92f9">Discount</th>
					<th bgcolor="#2b92f9">Amount</th>
				</tr>
		<?php
		$sql3 = "select DISTINCT a.id, a.keterangan, a.keterangan2
						FROM tbl_referensi a
						JOIN tbl_transaksi b ON b.jns_trans = a.keterangan2
						WHERE a.kd = 'POS' and b.no_booking = '".$_GET[idx]."' and b.status_bayar = 'N'
						UNION
						select DISTINCT a.id, a.keterangan, a.keterangan2 
						FROM tbl_referensi a 
						JOIN tbl_transaksi b ON b.jns_trans = a.keterangan2 
						JOIN tbl_pos_out c on c.no_id = b.no_booking
						WHERE a.kd = 'POS' and c.no_booking = '".$_GET[idx]."' and b.status_bayar = 'N'
						order by id
						";
				
				$r3 = mysql_query($sql3);
				$n3 = mysql_num_rows($r3);
				
				$no = 0;
				while ($row3 = mysql_fetch_array($r3)) { ?>
					<tr>
						<td colspan="4">&nbsp;&nbsp;<b><?php echo $row3[keterangan]; ?></b></td>
					</tr>
				<?php 
				$sql4 = "select a.*, b.nm_layanan  from tbl_transaksi a 
						join tbl_layanan b on b.id = a.item_id 
						where a.jns_trans = '".$row3[keterangan2]."' and a.no_booking = '".$_GET[idx]."' 
						and status_trans = 1 and status_bayar = 'N'
						UNION
						select a.*, b.nm_layanan from tbl_transaksi a 
						join tbl_layanan b on b.id = a.item_id 
						join tbl_pos_out c on c.no_id = a.no_booking
						where a.jns_trans = '".$row3[keterangan2]."' and c.no_booking = '".$_GET[idx]."' 
						and a.status_trans = 1 and a.status_bayar = 'N'";
					
				$r4 = mysql_query($sql4);
				$n4 = mysql_num_rows($r4);
				
				
				
				while ($row4 = mysql_fetch_array($r4)) { 
				if (strtoupper($row3[keterangan]) == "ROOM"){
					$layanan = $nm_kamar;
					
				} else {
					$layanan = $row4[nm_layanan];
				}
				$no = $no + 1;
				?>
					<tr>
						<td><?php echo ucwords(strtolower($layanan)); ?></td>
						<td align="right"><?php echo number_format($row4[harga],2,",","."); ?></td>
						<td align="center"><?php echo $row4[qty]; ?></td>
						<td align="right"><?php echo number_format($row4[diskon],2,",","."); ?></td>
						<td align="right"><?php echo number_format($row4[jumlah],2,",","."); ?></td>
						<input type="hidden" name="harga[]" id="harga_<?php echo $no;?>" value="<?php echo $row4[jumlah];?>" />
						<input type="hidden" name="serv_kmr" id="serv_kmr" value="<?php echo $row4[jumlah];?>" />
					</tr>
					
				<?php $tott = $tott + $row4[jumlah];
				} } ?>
			</table>
	</div>
	</div>
	<?php
} else {
	title("<img src='images/sales-report-icon.png' width='65' align='absmiddle' >  Cancel Check In");
	
?>
<br/>
<form>

<table id='mytable' class="gridtable tableWithFloatingHeader" width="100%" border="1px">
	<tr>
		<td class="gridtable_th" align="center">No</td>
		<td class="gridtable_th" align="center">Guest ID</td>
		<td class="gridtable_th" align="center">Check in<br/>Date</td>
		<td class="gridtable_th" align="center">No.<br/>Room</td>
		<td class="gridtable_th" align="center">Room Type</td>
		<td class="gridtable_th" align="center">Guest</td>
		<td class="gridtable_th" align="center">Address</td>
		<td class="gridtable_th" align="center">Hp & Email</td>
	</tr>
	<?php
	$sql = "select DISTINCT a.*, c.keterangan as inisial, d.*, d.keterangan as negara, f.no_room
			from tbl_booking a 
			join tbl_transaksi b on b.no_booking = a.id_booking
			join tbl_referensi c ON c.id = a.kd_inisial and c.kd = 'INS'
			join tbl_referensi d ON d.id = a.jns_room and d.kd = 'ROM'
			join tbl_referensi e on e.id = a.kd_negara and e.kd = 'NGR'
			join tbl_room f on f.id = b.item_id
			where b.jns_trans = 'ROOM' and a.status_booking = 'cekin' and b.status_trans = 1
			 ";
	$r = mysql_query($sql);
	$n = mysql_num_rows($r);
	
	$no = 1;

while ($row = mysql_fetch_array($r)) {
	?>
	<tr>
		<td class="gridtable_td"><?php echo $no++; ?></td>
		<td class="gridtable_td" align="center"><a href="<?php echo "$SC?p=$PID&idx=$row[id_booking]";?>"><?php echo $row[id_booking]; ?></a></td>
		<td class="gridtable_td" align="center"><?php echo ucwords(strtolower($row[tgl_cekin])); ?></td>
		<td class="gridtable_td" align="center"><?php echo ucwords(strtolower($row[no_room])); ?></td>
		<td class="gridtable_td"><?php echo ucwords(strtolower($row[keterangan])); ?></td>
		<td class="gridtable_td"><?php echo ucwords(strtolower($row[inisial]." ".$row[nm_lengkap])); ?></td>
		<td class="gridtable_td"><?php echo ucwords(strtolower($row[alamat]." ".$row[kota]." ".$row[kd_pos]." ".$row[negara])); ?></td>
		<td class="gridtable_td"><?php echo ucwords(strtolower($row[no_hp]." / ".$row[email])); ?></td>
	</tr>
	<?php } ?>
</table>

</form>
<?php } ?>