<link rel="stylesheet" type="text/css" href="../css/cetak.css">
<?php
@session_start();
require_once("../lib/dbconn.php");
require_once("../lib/form.php");
require_once("../lib/functions.php");

$r2 = mysql_query("select *
				from tbl_pnpm");
$n2 = mysql_num_rows($r2);
if($n2 > 0) $d2 = mysql_fetch_object($r2);
mysql_free_result($r2);

$r = mysql_query("select a.*, b.keterangan as inisial, c.*, d.keterangan as negara
				from tbl_booking a 
				JOIN tbl_referensi b ON b.id = a.kd_inisial and b.kd = 'INS'
				JOIN tbl_referensi c ON c.id = a.jns_room and c.kd = 'ROM'
				join tbl_referensi d on d.id = a.kd_negara and d.kd = 'NGR'
				where a.id_booking = '".$_GET[idx]."' ");
$n = mysql_num_rows($r);
if($n > 0) $d = mysql_fetch_object($r);
mysql_free_result($r);

$id_room = getFromTable("select item_id from tbl_transaksi where jns_trans = 'ROOM' and status_trans = 1 and no_booking = '".$_GET[idx]."' ");
$no_room = getFromTable("select no_room from tbl_room where id = $id_room ");


?>

<table width="100%">
	<tr>
		<td class="judul_kw"><?php echo $d2->nm_hotel; ?></td>
	</tr>
	<tr>
		<td class="judul1_kw"><?php echo $d2->alamat1; ?></td>
	</tr>
	<tr>
		<td class="judul1_kw"><?php echo $d2->alamat2; ?></td>
	</tr>
</table>
<hr class="line_kw"/>

<?php
If(isset($_GET[kw])){ 
	$r3 = mysql_query("select * from tbl_pembayaran
					where no_kwitansi = '".$_GET[kw]."' and jns_bayar != 'BATAL'");
	$n3 = mysql_num_rows($r3);
	if($n3 > 0) $d3 = mysql_fetch_object($r3);
	mysql_free_result($r3);
	
	$cab = getFromTable("select keterangan from tbl_referensi where kd = 'CAB' and id = '".$d3->cara_bayar."'");
	
	$downpay = getFromTable("select sum(jml_bayar) from tbl_pembayaran where no_booking = '".$_GET[idx]."' and jns_bayar = 'DEPOSIT'");

	?>
	<table width="100%" class="tbl_kw" border="0px">
		<tr>
			<th width="20%">No.Bill</th>
			<td width="40%">: <?php echo $_GET[kw]; ?></td>
			<td width="5%">&nbsp;</td>
			<td width="5%">&nbsp;</td>
			<th width="10%"><?php echo ucwords(strtolower($d2->kota)); ?>,</th>
			<td width="20%"><?php echo date("d-m-Y"); ?></td>
		</tr>
		<tr>
			<th >Name</th>
			<td >: <?php echo $d->inisial." ".$d3->nm_pembayar; ?></td>
			<td >&nbsp;</td>
			<td >&nbsp;</td>
			<th >&nbsp;</th>
			<td >&nbsp;</td>
		</tr>
		<?php
		$ket = explode("-", $_GET[idx]);
		if ($ket[0] == "TRS"){ 
		$nomej = getFromTable("select no_meja from tbl_pos_out where no_id = '".$_GET[idx]."' ");
		?>
		<tr>
			<th >Table No</th>
			<td >: <?php echo $nomej; ?></td>
			<td >&nbsp;</td>
			<td >&nbsp;</td>
			<th >&nbsp;</th>
			<td >&nbsp;</td>
		</tr>	
		<?php } else { ?>
		<tr>
			<th >Room</th>
			<td >: <?php echo $no_room." - ".$d->keterangan; ?></td>
			<td >&nbsp;</td>
			<td >&nbsp;</td>
			<th >&nbsp;</th>
			<td >&nbsp;</td>
		</tr>	
		<?php } ?>
		
		<tr>
			<th >Payment Date</th>
			<td >: <?php echo $d3->tgl_bayar; ?></td>
			<td >&nbsp;</td>
			<td >&nbsp;</td>
			<th >&nbsp;</th>
			<td >&nbsp;</td>
		</tr>
		<tr>
			<th >Payment Method</th>
			<td >: <?php echo $cab; ?></td>
			<td >&nbsp;</td>
			<td >&nbsp;</td>
			<th >&nbsp;</th>	
			<td >&nbsp;</td>
		</tr>
	</table>
	<br>

	<table width="100%" class="rincian" >
		<tr>
			<th class="rincian_th" width="5%">No</th>
			<th class="rincian_th" width="10%" >Time</th>
			<th class="rincian_th" colspan="2">Item</th>
			<th class="rincian_th">Qty</th>
			<th class="rincian_th">Amount</th>
		</tr>
		<?php 
		$sql = "select DISTINCT a.id, a.keterangan, a.keterangan2,b.tgl_entry,b.wkt_entry
				FROM tbl_referensi a
				JOIN tbl_transaksi b ON b.jns_trans = a.keterangan2
				WHERE b.no_kwitansi = '".$_GET[kw]."' group by a.id";
			
		$r = mysql_query($sql);
		$n = mysql_num_rows($r);
		
		$jml2 = 0;	
		while ($row = mysql_fetch_array($r)) { ?>
		<?php 
		$tgl = date('d-M-Y',strtotime($row['tgl_entry']));
		$jam = " ".$row['wkt_entry'];
		?>
		
		<?php 
		$sql2 = "select a.*, b.nm_layanan
				from tbl_transaksi a 
				join tbl_layanan b on b.id = a.item_id
				WHERE a.no_kwitansi = '".$_GET[kw]."' and a.jns_trans = '".$row[keterangan2]."' and a.no_booking='".$_GET['idx']."'";
		
		$r2 = mysql_query($sql2);
		$n2 = mysql_num_rows($r2);
		
		$no = 1;
		$jml = 0;	
		?>
			<tr>
				<td class="rincian_td">&nbsp;</td>
				<td class="rincian_td" colspan="2"><b><?php echo $row[keterangan]; ?></b></td>
				<td class="rincian_td">&nbsp;</td>
				<td class="rincian_td">&nbsp;</td>
				<td class="rincian_td">&nbsp;</td>
			</tr>
		<?php
		while ($row2 = mysql_fetch_array($r2)) { 
			
		if ($row[keterangan2] == "ROOM"){
			$lay = "Room No. ".$no_room." - ".$d->keterangan;
		} else {
			$lay = $row2[nm_layanan];
		}
		?>
			<tr>
				<td class="rincian_td" align="center"><?php echo $no++; ?></td>
				<td class="rincian_td"><?=$tgl;?> <?=$jam;?></td>
				<td class="rincian_td" colspan="2"><?php echo $lay; ?> @<?php echo number_format($row2[harga],0,",","."); ?></td>
				<td class="rincian_td" align="center"><?php echo $row2[qty]; ?> </td>
				<td class="rincian_td" align="right"><?php echo number_format($row2[jumlah],0,",","."); ?></td>
			</tr>
		<?php $jml = $jml + $row2[jumlah]; } $jml2 = $jml2 + $jml; } ?>
		<tr>
			<td align="right" class="rincian_total" colspan="5"><b>Total</b></td>
			<td align="right" class="rincian_total"><b><?php echo number_format($jml2,0,",","."); ?></b></td>
		</tr>
		<tr>
			<td class="rincian_total" colspan="2"><b>Your Satisfaction is our happiness</b></td>
			<td align="right" class="rincian_total" colspan="3"><b>Service</b></td>
			<td align="right" class="rincian_total"><b><?php echo number_format($d3->servis,0,",","."); ?></b></td>
		</tr>
		<tr>
			<td class="rincian_total" colspan="2"><b><!--Thank you.--></b></td>
			<td align="right" class="rincian_total" colspan="3"><b>Tax</b></td>
			<td align="right" class="rincian_total"><b><?php echo number_format($d3->tax_ppn,0,",","."); ?></b></td>
		</tr>
		<tr>
			<td class="rincian_total" colspan="2"><b><!--with our service--></b></td>
			<td align="right" class="rincian_total" colspan="3"><b>Discount</b></td>
			<td align="right" class="rincian_total"><b>(<?php echo number_format($d3->diskon,0,",","."); ?>)</b></td>
		</tr>
		<tr>
			<td class="rincian_total" colspan="2"><b>Thank you.</b></td>
			<td align="right" class="rincian_total" colspan="3"><b>Materai</b></td>
			<td align="right" class="rincian_total"><b><?php echo number_format($d3->materai,0,",","."); ?></b></td>
		</tr>
		<?php
		$jml3 = $jml2 + $d3->servis + $d3->tax_ppn + $d3->materai - $d3->diskon;
		?>
		<tr>
			<td align="right" class="rincian_total" colspan="5"><b>Bill Amount</b></td>
			<td align="right" class="rincian_total"><b><?php echo number_format($jml3,0,",","."); ?></b></td>
		</tr>
		<?php if ($d3->status_bayar == "002") { ?>
		<?php //if ($row[keterangan2] == "ROOM"){?>
		<tr>
			<td align="right" class="rincian_total" colspan="5"><b>Downpayment</b></td>
			<td align="right" class="rincian_total"><b><?php echo number_format($downpay,0,",","."); ?></b></td>
		</tr>
		<?php
		$jml4=$jml3-$downpay;
		
		?>
		<tr>
			<td align="right" class="rincian_total" colspan="5"><b>Bill Total</b></td>
			<?php if($jml4<0):?>
			<td align="right" class="rincian_total"><b><?php echo number_format(0,0,",","."); ?></b></td>
			<?php else:?>
			<td align="right" class="rincian_total"><b><?php echo number_format($jml4,0,",","."); ?></b></td>
			<?php endif;?>
		</tr>
		<?php// }?>
		
		<?php if ($d3->jml_bayar > 0 ) { ?>
		
		<tr>
			<td align="right" class="rincian_total" colspan="5"><b> Payment</b></td>
			<td align="right" class="rincian_total"><b><?php echo number_format($d3->jml_bayar,0,",","."); ?></b></td>
		</tr>
		<tr>
			<td align="right" class="rincian_total" colspan="5"><b>Return</b></td>
			<td align="right" class="rincian_total"><b><?php echo number_format(abs($d3->sisa),0,",","."); ?></b></td>
		</tr>
		<?php } else {?>
		<tr>
			<td align="right" class="rincian_total" colspan="5"><b> Payment</b></td>
			<td align="right" class="rincian_total"><b><?php echo number_format(0,0,",","."); ?></b></td>
		</tr>
		<tr>
			<td align="right" class="rincian_total" colspan="5"><b>Return </b></td>
			<td align="right" class="rincian_total"><b><?php echo number_format(abs($d3->jml_bayar * -1),0,",","."); ?></b></td>
		</tr>
		
		<?php } }
	else if ($d3->status_bayar == "003") { ?>
		<?php //if ($row[keterangan2] == "ROOM"){?>
		<tr>
			<td align="right" class="rincian_total" colspan="5"><b>Downpayment</b></td>
			<td align="right" class="rincian_total"><b><?php echo number_format($downpay,0,",","."); ?></b></td>
		</tr>
		<?php
		$jml4=$jml3-$downpay;
		
		?>
		<tr>
			<td align="right" class="rincian_total" colspan="5"><b>Bill Total</b></td>
			<?php if(false):?>
			<td align="right" class="rincian_total"><b><?php echo number_format(0,0,",","."); ?></b></td>
			<?php else:?>
			<td align="right" class="rincian_total"><b><?php echo number_format($jml4,0,",","."); ?></b></td>
			<?php endif;?>
		</tr>
		<?php// }?>
		<?php if ($d3->jml_bayar > 0 ) { ?>
		
		<tr>
			<td align="right" class="rincian_total" colspan="5"><b> Payment</b></td>
			<td align="right" class="rincian_total"><b><?php echo number_format($d3->jml_bayar,0,",","."); ?></b></td>
		</tr>
		<tr>
			<td align="right" class="rincian_total" colspan="5"><b>Return</b></td>
			<td align="right" class="rincian_total"><b><?php echo number_format(abs($d3->sisa),0,",","."); ?></b></td>
		</tr>
		<?php } else {?>
		<tr>
			<td align="right" class="rincian_total" colspan="5"><b> Payment</b></td>
			<td align="right" class="rincian_total"><b><?php echo number_format(0,0,",","."); ?></b></td>
		</tr>
		<tr>
			<td align="right" class="rincian_total" colspan="5"><b>Return </b></td>
			<td align="right" class="rincian_total"><b><?php echo number_format(abs($d3->jml_bayar * -1),0,",","."); ?></b></td>
		</tr>
		<?php } }

		else { ?>
		<tr>
			<td align="right" class="rincian_total" colspan="5"><b> Payment</b></td>
			<td align="right" class="rincian_total"><b><?php echo number_format(abs($d3->jml_bayar),0,",","."); ?></b></td>
		</tr>
		<tr>
			<td align="right" class="rincian_total" colspan="5"><b>Return</b></td>
			<td align="right" class="rincian_total"><b><?php echo number_format(abs($d3->sisa * -1),0,",","."); ?></b></td>
		</tr>
		<?php } ?>
	</table>
	<br/>
	<br/>
	<table width="100%">
		<tr>
			<td align="center" class="judul1_kw"><!--Guest--></td>
			<td align="center" class="judul1_kw">Cashier</td>
		</tr>
		<tr>
			<td align="center" class="judul1_kw">&nbsp;<br><br><br></td>
			<td align="center" class="judul1_kw">&nbsp;</td>
		</tr>
		<tr>
			<td align="center" class="judul1_kw"><b><!--<?php echo ucwords(strtolower($d3->nm_pembayar)); ?>--></b></td>
			<td align="center" class="judul1_kw"><b><?php echo ucwords(strtolower($_SESSION[nama_usr])); ?></b></td>
		</tr>
	</table>
	
	<SCRIPT LANGUAGE="JavaScript">
	
	function printWindow() {
	bV = parseInt(navigator.appVersion);
	if (bV >= 4) window.print();
	}
		
	printWindow();
	</script>
<?php } else { ?>
	<table width="80%" align="center" class="tbl_list">
		<tr>
			<th colspan="5">PAYMENT ADVANCED</th>
		</tr>
		<tr>
			<th>No</th>
			<th>Print</th>
			<th>Date</th>
			<th>No. Check</th>
			<th>Amount</th>
		</tr>
		<tr>
			<th colspan="5"><hr class="line_kw" /></th>
		</tr>
		<?php 
		$sql = "select * from tbl_pembayaran WHERE jns_bayar = 'BAYAR' and no_booking = '".$_GET[idx]."' ";
		$r = mysql_query($sql);
		$n = mysql_num_rows($r);
		
		$no = 1;

	while ($row = mysql_fetch_array($r)) {
		?>
		<tr>
			<td align="center"><?php echo $no++;?></td>
			<td align="center"><a href="cetak_bayar.php?idx=<?php echo $_GET[idx];?>&kw=<?php echo $row[no_kwitansi];?>"><img src="../images/print-icon1.png"></a></td>
			<td align="center"><?php echo $row[tgl_bayar];?></td>
			<td align="center"><?php echo $row[no_kwitansi];?></td>
			<td align="right"><?php echo $row[jml_bayar];?></td>
		</tr>
		<?php } ?>
	</table>
<?php } ?>