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

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

title("<img src='images/sales-report-icon.png' width='65' align='absmiddle' >  Revition of Payment");

if($_GET[act] == "cek"){
	if(strlen($_GET[act]) == 0){
		echo "<script>alert('Guest ID not Empty!');</script>";
		echo "<script language='JavaScript'>document.location='index2.php?p=".$PID."'</script>";
	} else {
		$cek = getFromTable("select count(no_booking) from tbl_pembayaran where no_booking = '".$_GET[idx]."' ");
		if ($cek > 1){
			
			echo "<script language='JavaScript'>document.location='index2.php?p=".$PID."&act=list&idx=".$_GET[idx]."'</script>";
		} else {
			echo "<script>alert('Guest ID not Found!');</script>";
			echo "<script language='JavaScript'>document.location='index2.php?p=".$PID."'</script>";
		}
	}
	
	
} elseif($_GET[act] == "list"){
	include_once ("keterangan.php");
	$sql = "select a.*, b.keterangan as cab
			from tbl_pembayaran a 
			join tbl_referensi b on b.id = a.cara_bayar and b.kd = 'CAB'
			where a.no_booking = '".$_GET[idx]."' and a.jns_bayar != 'BATAL' order by a.tgl_bayar, a.wkt_bayar ";
	$r = mysql_query($sql);
	$n = mysql_num_rows($r);	
	$no = 1;
	?>
	<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">&nbsp;</td>
			<td class="gridtable_th" align="center">Date</td>
			<td class="gridtable_th" align="center">Time</td>
			<td class="gridtable_th" align="center">Check No</td>
			<td class="gridtable_th" align="center">Payment Method</td>
			<td class="gridtable_th" align="center">Payment Type</td>
			<td class="gridtable_th" align="center">Amount</td>
			<td class="gridtable_th" align="center">User Payment</td>
		</tr>
	
	<?php while ($row = mysql_fetch_array($r)) { 
	if ($row[status_bayar] == "002" && $row[jns_bayar] == "BAYAR"){
		$keterangan = "CHECKOUT PAYMENT";
	} elseif ($row[status_bayar] == "001" && $row[jns_bayar] == "DEPOSIT"){
		$keterangan = "DOWN PAYMENT";
	} elseif ($row[status_bayar] == "001" && $row[jns_bayar] == "RETUR"){
		$keterangan = "CHECKOUT (RETUR) PAYMENT";
	} else {
		$keterangan = "POINT OF SALES PAYMENT";
	}
	?>
		<tr>
			<td class="gridtable_td" align="center"><?php echo $no++;?></td>
			<td class="gridtable_td" align="center"><a href="<?php echo "$SC?p=$PID&act=hapus&idx=".$_GET[idx]."&jns=".$row[jns_bayar]."&stat=".$row[status_bayar]."&kw=".$row[no_kwitansi];?>"><img src="images/icon-ok.png"></a></td>
			<td class="gridtable_td" align="center"><?php echo $row[tgl_bayar];?></td>
			<td class="gridtable_td" align="center"><?php echo $row[wkt_bayar];?></td>
			<td class="gridtable_td" align="center"><?php echo $row[no_kwitansi];?></td>
			<td class="gridtable_td" align=""><?php echo $row[cab];?></td>
			<td class="gridtable_td" align=""><?php echo $keterangan;?></td>
			<td class="gridtable_td" align="right"><?php echo number_format($row[jml_bayar],2,",",".");?></td>
			<td class="gridtable_td" align=""><?php echo $row[user_input];?></td>
		</tr>
	<?php }	?>
	</table>
	<?php
	
} elseif($_GET[act] == "hapus"){
	if ($_GET[stat] == "002" && $_GET[jns] == "BAYAR"){
		//$keterangan = "CHECKOUT PAYMENT";
		mysql_query("update tbl_transaksi set no_kwitansi = NULL, status_bayar = 'N' where no_kwitansi = '".$_GET[kw]."'");
		mysql_query("update tbl_booking set status_booking = 'cekin' where id_booking = '".$_GET[idx]."' ");
		mysql_query("update tbl_pembayaran set jns_bayar = 'BATAL' where no_booking = '".$_GET[idx]."' and no_kwitansi = '".$_GET[kw]."' ");
		echo "<script language='JavaScript'>document.location='index2.php?p=".$PID."'</script>";
	} elseif ($_GET[stat] == "001" && $_GET[jns] == "DEPOSIT"){
		$keterangan = "DOWN PAYMENT";
		mysql_query("update tbl_pembayaran set jns_bayar = 'BATAL' where no_booking = '".$_GET[idx]."' and no_kwitansi = '".$_GET[kw]."' ");
	} elseif ($_GET[stat] == "001" && $_GET[jns] == "RETUR"){
		//$keterangan = "CHECKOUT (RETUR) PAYMENT";
		mysql_query("update tbl_transaksi set no_kwitansi = NULL, status_bayar = 'N' where no_kwitansi = '".$_GET[kw]."'");
		mysql_query("update tbl_booking set status_booking = 'cekin' where id_booking = '".$_GET[idx]."' ");
		mysql_query("update tbl_pembayaran set jns_bayar = 'BATAL' where no_booking = '".$_GET[idx]."' and no_kwitansi = '".$_GET[kw]."' ");
		echo "<script language='JavaScript'>document.location='index2.php?p=".$PID."'</script>";
	} else {
		//$keterangan = "POINT OF SALES PAYMENT";
		mysql_query("update tbl_transaksi set no_kwitansi = NULL, status_bayar = 'N' where no_kwitansi = '".$_GET[kw]."'");
		mysql_query("update tbl_pembayaran set jns_bayar = 'BATAL' where no_booking = '".$_GET[idx]."' and no_kwitansi = '".$_GET[kw]."' ");
		echo "<script language='JavaScript'>document.location='index2.php?p=".$PID."'</script>";
	}
} else {
	

?>
<form>
	<input type="hidden" name="p" value="<?php echo $PID;?>"/>
	<input type="hidden" name="act" value="cek"/>
	<div class="ui three column stackable grid" align="center">
		<div class="column"></div>
		<div class="column">
			<table width="100%" class="gridtable_form" border="1px">
				<tr>
					<td align="center"><b>Guest ID</b></td>
				</tr>
				<tr>
					<td align="center">
						<input type="text" name="idx" value="<?php echo $_GET[idx];?>" />
					</td>
				</tr>
				<tr>
					<td align="center"><input type="submit" value="Find"/></td>
				</tr>
			</table>
		</div>
		<div class="column"></div>
	</div>
</form>
<?php } ?>