<script type="text/javascript" src="js/autoNumeric.js"></script>
<script type="text/javascript" src="js/numeral.js"></script>
<script type="text/javascript">
	jQuery(function($) {
	    $('.auto').autoNumeric('init');
	});
	
	
</script>

<?php
$PID = "input_checkout";
$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' >  Cashier Payment (Guest In House)");
	include_once('keterangan.php');
	
	/* Update Lama Inap ke table transaksi */
	//echo "select date(CURRENT_DATE) - date('".$tgl_cekinroom."') ";
	$lama_inap = getFromtable("select date(CURRENT_DATE) - date('".$tgl_cekinroom."') ");
	if ($lama_inap == 0){
		$lama_inap = 1;
	} elseif ($lama_inap > 69){
		$lama_inap = $lama_inap - 69;
	} else {
		$lama_inap = $lama_inap;
	}
	
	if (date("Y-m-d") != $tgl_cekinroom){
		$wkt = getFromTable("select CURRENT_TIME");
		if($wkt > "13:00:00" and $wkt < "18:00:00"){
			//echo "test";
		}
	}
	
	if ($guest_type == "002"){
		//mysql_query("update tbl_transaksi set qty = $lama_inap, diskon = harga * $lama_inap where no_booking = '".$_GET[idx]."' and jns_trans = 'ROOM' and status_trans = 1");
	} else {
		//mysql_query("update tbl_transaksi set qty = $lama_inap, jumlah = harga * $lama_inap where no_booking = '".$_GET[idx]."' and jns_trans = 'ROOM' and status_trans = 1");
	}
	
	
	$r2 = mysql_query("select * from tbl_transaksi
				where no_booking = '".$_GET[idx]."' and jns_trans = 'ROOM' and status_trans = 1 and status_bayar = 'N' ");
	$n2 = mysql_num_rows($r2);
	if($n2 > 0) $d2 = mysql_fetch_object($r2);
	mysql_free_result($r2);
	
	if ($downpay > 0){
		$downpay = $downpay;
	} else {
		$downpay = 0;
	}
	?>
	<br/>
	<form action="actions/bayar_insert.php" method="POST">
	<input type="hidden" name="p" value="input_checkout"/>
	<input type="hidden" name="idx" value="<?php echo $_GET[idx];?>"/>
	<input type="hidden" name="id_kamar" value="<?php echo $id_room;?>"/>
	<input type="hidden" name="tot1" value="0" id="tot1"/>
	<input type="hidden" name="hrg_materai" value="0" id="hrg_materai"/>
	<input type="hidden" name="jenis" value="BAYAR"/>
	<div class="ui two column stackable grid" align="center">
		<div class="column">
			<table class="gridtable" width="95%" border="1px">
				<tr>
					<th bgcolor="#2b92f9">
						<input type="checkbox" name="check_all" class="check_all" id="check_all" value=""/>
					</th>
					<th bgcolor="#2b92f9">Date</th>
					<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>
				<!--
				<tr>
					<td align="center"><input type="checkbox" name="jns[]" class="jns" id="jns_1" value="<?php echo $d2->id;?>">	</td>
					<td>Room No. <?php echo $no_room; ?> - <?php echo $nm_kamar; ?></td>
					<td align="right">Rp. <?php echo number_format($d2->harga,2,",","."); ?></td>
					<td align="center"><?php echo $d2->qty; ?></td>
					<td align="right">Rp. <?php echo number_format($d2->diskon,2,",","."); ?></td>
					<td align="right">Rp. <?php echo number_format($d2->jumlah,2,",","."); ?></td>
					<input type="hidden" name="harga[]" id="harga_1" value="<?php echo $d2->jumlah;?>" />
				</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 align="center">&nbsp;</td>
						<td colspan="5"><?php echo $row3[keterangan]; ?></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 align="center">
							<input type="checkbox" name="jns[]" class="jns" id="jns_<?php echo $no;?>" value="<?php echo $row4[id];?>"/>
						</td>
						<td><?php echo ucwords(strtolower($row4[tgl_trans])); ?></td>
						<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];
				} } ?>
				<input type="hidden" name="tot_tagih" value="<?php echo $tott + $d2->jumlah;?>" />
				<input type="hidden" name="max" class="max" id="max" value="<?php echo $no;?>" />
			</table>
		</div>
		
		<div class="column">
			<table width="80%" class="gridtable_form" align="center">
				<tr>
					<td><b>Bill Amount</b></td>
					<td>: &nbsp;</td>
					<td><input type="text" name="total" class="total" id="total" value="0" style="text-align: right;" size="27" readonly="readonly"/></td>
				</tr>
				<tr>
					<td><b>Service</b></td>
					<td>: <input type="checkbox" name="serv" class="serv" id="serv"></td>
					<td><input type="text" name="js_serv" class="js_serv" id="js_serv" value="0" style="text-align: right;" size="27" readonly="readonly"/></td>
				</tr>
				<tr>
					<td><b>PPN / TAX</b></td>
					<td>: <input type="checkbox" name="ppn" class="ppn" id="ppn"></td>
					<td><input type="text" name="tax" class="tax" id="tax" value="0" style="text-align: right;" size="27" readonly="readonly"/></td>
				</tr>
				<tr>
					<td><b>Bill Total</b></td>
					<td>: &nbsp;</td>
					<td><input type="text" name="totag" class="totag" id="totag" value="0" style="text-align: right;" size="27" readonly="readonly"/></td>
				</tr>
				<tr>
					<td><b>Discount %</b></td>
					<td>: &nbsp;</td>
					<td>
						<input type="text" name="dis" class="dis" id="dis" value="0" size="3" maxlength="3" required="required"/>
						<input type="text" name="diskon" class="diskon" id="diskon" value="0" style="text-align: right;" readonly="readonly"/>
					</td>
				</tr>
				<tr>
					<td><b>Payment Status</b></td>
					<td>: &nbsp;</td>
					<td><?php echo query_statbayar("stat","","001"," OnChange=\"CekStatus(this.value);\"");?></td>
				</tr>
				<tr>
					<td><b>Downpayment</b></td>
					<td>: &nbsp;</td>
					<td><input type="text" name="dp" class="dp" id="dp" value="0" style="text-align: right;" size="27" readonly="readonly"/></td>
					<input type="hidden" name="deposit" class="deposit" id="deposit" value="<?php echo $downpay;?>" />
				</tr>
				<tr>
					<td><b>Materai</b></td>
					<td>: &nbsp;</td>
					<td><?php echo query_materai("materai","001"," OnChange=\"CekMaterai(this.value);\"");?></td>
				</tr>
				<tr>
					<td><b>Payment</b></td>
					<td>: &nbsp;</td>
					<td><input type="text" name="bayar" class="auto" id="bayar" value="0" style="text-align: right; background-color: #ffcb5e" size="27" /></td>
				</tr>
				<tr>
					<td><b>Return</b></td>
					<td>: &nbsp;</td>
					<td><input type="text" name="sisa" class="auto" id="sisa" value="0" style="text-align: right;" size="27" readonly="readonly"/></td>
				</tr>
				<tr>
					<td><b>Order Name</b></td>
					<td>: &nbsp;</td>
					<td><input type="text" name="nm_pembayar" value="<?php echo ucwords(strtolower($d->nm_lengkap)); ?>" size="27"/></td>
				</tr>
				<tr>
					<td><b>Payment Method</b></td>
					<td>: &nbsp;</td>
					<td><?php echo query_carabayar("cab","001");?></td>
				</tr>
				<tr>
					<td><b>&nbsp;</b></td>
					<td>: &nbsp;</td>
					<td><input type="submit" value="Save"/></td>
				</tr>
				<tr>
					<td><b>Guest Polio</b></td>
					<td>: &nbsp;</td>
					<td><a href="javascript: GuestFolio('<?php echo $_GET[idx];?>')"><img src="images/print-icon1.png"></a></td>
				</tr>
			</table>
		</div>
	</div>
	</form>
	
	<br/>
	<table width="90%" align="center" class="gridtable_form">
		<tr>
			<th>No</th>
			<th>&nbsp;</th>
			<th>Payment Date</th>
			<th>No. Check</th>
			<th>Total</th>
		</tr>
		<tr>
			<th colspan="5"><hr class="line_kw" /></th>
		</tr>
		<?php 
		$sql = "select * from tbl_pembayaran WHERE jns_bayar = 'DEPOSIT' 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="javascript: cetakDP('<?php echo $_GET[idx];?>','<?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 number_format($row[jml_bayar],2,',','.');?></td>
		</tr>
		<?php } ?>

		<?php 
		$sql = "select * from tbl_pembayaran WHERE jns_bayar = 'BAYAR' and no_booking = '".$_GET[idx]."' ";
		$r = mysql_query($sql);
		$n = mysql_num_rows($r);

		while ($row = mysql_fetch_array($r)) {
		?>
		<tr>
			<td align="center"><?php echo $no++;?></td>
			<td align="center"><a href="javascript: cetakPOS('<?php echo $_GET[idx];?>','<?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>
<script language='JavaScript'>
	function cetakDP(tag, tag1) {
	    sWin = window.open('includes/cetak_dp.php?idx='+tag+'&kw='+tag1, 'xWin',
	 	'top=0,left=0,width=1200,height=550,menubar=no,scrollbars=yes');
	    sWin.focus();
	}
	function cetakPOS(tag, tag1) {
	    sWin = window.open('includes/cetak_bayar.php?idx='+tag+'&kw='+tag1, 'xWin',
	 	'top=0,left=0,width=1200,height=550,menubar=no,scrollbars=yes');
	    sWin.focus();
	}
	function GuestFolio(tag) {
	    sWin = window.open('includes/guestfolio.php?idx='+tag, 'xWin',
	 'top=0,left=0,width=1200,height=550,menubar=no,scrollbars=yes');
	    sWin.focus();
	}
	function hitung_total(){    
	    var total = parseInt($("#total").val());
	    var js_serv = parseInt($("#js_serv").val());
	    var tax = parseInt($("#tax").val());
	    var diskon = parseInt($("#diskon").val());
	    var dp = parseInt($("#dp").val());
	    var materai = parseInt($("#hrg_materai").val());
	    
		tot = total + js_serv + tax; 
		tot1 = tot - (diskon + dp) + materai; 
		
		$("#totag").val(tot);
		$("#bayar").val(tot1);
		/*
		if(tot1<0){
			$("#sisa").val(tot1);	
			
		}else{
			$("#bayar").val(tot1);
			
		}
		*/
		$("#tot1").val(tot1);
	}
	
	function hitung(tag, cok){    
	    var harga = parseInt($("#harga_"+tag).val());
	    var total = parseInt($("#total").val());
	  
	    if (cok == "checked"){
			tot = total + harga; 
		} else {
			tot = total - harga; 
		}
			$("#total").val(tot);
			$("#totag").val(tot);
			$("#bayar").val(tot);
			$("#tot1").val(tot);
			
	}
	
	function CekMaterai(tag){
		
		if(tag == "002"){		
			$("#hrg_materai").val(3000);
		} else if (tag == "003"){
			$("#hrg_materai").val(6000);
		} else {
			$("#hrg_materai").val(0);
		}
		hitung_total();
	}
	
	
	function CekStatus(tag){
		var dp = parseInt($("#deposit").val());
		
		if(tag == "001"){		
			$("#dp").val(0);
		} else {
			$("#dp").val(dp);
		}
		hitung_total();
	}

	$(function(){
		
	
	    $('.check_all').click(function() {
	    	var maxi = parseInt($(".max").val());
	    	if(this.checked){
				$("#total").val(0);
				$("#totag").val(0);
				$("#bayar").val(0);
				$("#tot1").val(0);
				
			}
	    	
	    	for (i = 1; i <= maxi; i++){
				$('#jns_'+i).prop('checked', this.checked);
				var cek = $("#jns_"+i).attr("checked");
				console.log('#jns_'+i+" "+cek);
				
				hitung(i,cek);
			}
			
	        
	    });


		$('.jns').click(function(){
	    	var nm_id = $(this).attr('id').split("_");
	        var aud = nm_id[1];
	        var cek = $("#jns_"+aud).attr("checked");
	        
	         hitung(aud,cek);
	         
    	});
    	
    	$('.serv').click(function(){
	    	var total = parseInt($("#serv_kmr").val());
	        var cek = $("#serv").attr("checked");
	        
	        if (cek == "checked"){
				jasa = (total * 0.3);
			} else {
				jasa = 0;
			}
	        
	        
	        $("#js_serv").val(jasa);
	        
	        hitung_total();
    	});
    	
    	
    	$('.ppn').click(function(){
	    	var total = parseInt($("#total").val());
	        var cek = $("#ppn").attr("checked");
	        
	        if (cek == "checked"){	        	
	        	jasa = (total * 0.1);
	        } else {
				jasa = 0;
			}
	        $("#tax").val(jasa);
	        hitung_total();	  
    	});
    	
    	
    	$('.dis').keyup(function(){
	    	var totag = parseInt($("#totag").val());
	    	var dis = parseInt($("#dis").val());
	        
	        diskon = ((totag * dis) / 100);
	        
	        $("#diskon").val(diskon);
	        hitung_total();	  
    	});
    	
    	$('#bayar').keyup(function(){
	    	var totag = parseInt($("#tot1").val());
	    	var harga = $("#bayar").val();
			var harga1 = harga.replace(",","");
			var harga2 = harga1.replace(",","");
			var harga3 = parseInt(harga2.replace(".",","));
	        
	        sisa = totag - harga3;
	        
	        $("#sisa").val(sisa);
			
    	});
	});
</script>
	<?php
} else {
	title("<img src='images/sales-report-icon.png' width='65' align='absmiddle' >  Cashier Payment (Guest In House) ");
	
?>
<br/>
<form>
<input type="hidden" name="p" value="input_checkout"/>
<input type="submit" style="display: none;"/>
<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">Room</td>
		<td class="gridtable_th" align="center">Name</td>
		<td class="gridtable_th" align="center">Address</td>
		<td class="gridtable_th" align="center">Hp & Email</td>
	</tr>
	<tr>
		<td class="gridtable_th">&nbsp;</td>
		<td class="gridtable_th" align="center"><input type="text" size="10" name="kd_book" value="<?php echo $_GET[kd_book];?>"></td>
		<td class="gridtable_th">&nbsp;</td>
		<td class="gridtable_th" align="center"><input type="text" size="10" name="no_room" value="<?php echo $_GET[no_room];?>"></td>
		<td class="gridtable_th" align="center"><input type="text" name="nama" value="<?php echo $_GET[nama];?>"></td>
		<td class="gridtable_th" align="center"><input type="text" name="alamat" value="<?php echo $_GET[alamat];?>"></td>
		<td class="gridtable_th">&nbsp;</td>
	</tr>
	<?php
	$sql = "select DISTINCT a.*, b.keterangan as inisial, c.*, d.keterangan as negara, 
			(select min(z.tgl_trans) from tbl_transaksi z where z.no_booking = a.id_booking and z.jns_trans = 'ROOM') as tgl_trans, f.no_room
			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'
			join tbl_transaksi e on e.no_booking = a.id_booking
			join tbl_room f on f.id = e.item_id 
			where e.jns_trans = 'ROOM' and e.status_trans = 1 and 
			status_booking = 'cekin' and f.no_room like '%".$_GET[no_room]."%' and upper(id_booking) like '%".strtoupper($_GET[kd_book])."%' and
			upper(a.nm_lengkap) like '%".strtoupper($_GET[nama])."%' and (upper(a.alamat) like '%".strtoupper($_GET[alamat])."%'
			or upper(a.kota) like '%".strtoupper($_GET[alamat])."%' or upper(a.kd_pos) like '%".strtoupper($_GET[alamat])."%'
			or upper(d.keterangan) like '%".strtoupper($_GET[alamat])."%')  ";
	$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"><?php echo ucwords(strtolower($row[tgl_trans])); ?></td>
		<td class="gridtable_td"><?php echo ucwords(strtolower($row[no_room]." - ".$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 } ?>