<?php

if ($_GET[acts] == "hapus"){
	mysql_query("update tbl_transaksi set status_trans = 0 where id = ".$_GET[f]);
	echo "<script language='JavaScript'>document.location='index2.php?p=$PID&idx=$_GET[idx]'</script>";
}

$listLayanan = mysql_query( "SELECT id, nm_layanan, hrg_layanan FROM tbl_layanan where jns_layanan = '$laay' and status_layanan = 1 order by nm_layanan ");

if (isset($_GET[act])){
	include("input_posout.php");
} else {
	
if (isset($_GET[idx])){
title("<img src='images/sales-report-icon.png' width='65' align='absmiddle' >  $judul");

include('keterangan.php');	
?>
<style>
.atable{
	border-collapse:collapse;
	border:1px solid #276F86;
        font-family: verdana,arial,sans-serif;
        font-size: 12px;
}
.atable th{
	border:1px solid #fff;
	background:#276F86;
	font-weight:bold;
        color: #fff;
}
.atable td{
	padding:4px;
	border:1px solid #276F86;
}
.atable tfoot{
	border:1px solid #fff;
	background:#fbd850;
	font-weight:bold;
        color: #000;
}
.evenRow{
	background:#AFFBA2;
}

</style>
<div align="center"><a href="javascript:CekMenu('<?php echo $laay; ?>')"><img src="images/product-sales-report-icon.png" width="20"/>&nbsp;List Menu<img src="images/product-sales-report-icon.png" width="20"/>&nbsp;</a></div>
<form action="actions/input_layanan_insert.php" method="POST">
	<input type="hidden" name="p" value="<?php echo $_GET[p]; ?>"/>
	<input type="hidden" name="idx" value="<?php echo $_GET[idx]; ?>"/>
	<input type="hidden" name="jns_trans" value="<?php echo $jnsTrans; ?>"/>
    <table border="1" class="atable" width='100%'>
        <thead>
        <tr>
            <th>ITEM</th>
            <th>PRICE</th>
            <th>QTY</th>
            <th>%</th>
            <th>DISCOUNT</th>
            <th>AMOUNT</th>
            <th><input type="button" value="Add" id='addNewRaw'/></th>
        </tr>
        </thead>
        <tbody>
        <tr id="1">
            <input type="hidden" name="total[]" class="total" id="total_1">
            <input type="hidden" name="totharga[]" class="totharga" id="totharga_1">
            <input type="hidden" name="layananid[]" class="layananid" id="layananid_1">
            <td align="center"><input type="text" name="layanan[]" class="layanan" id="layanan_1" value="" required="required"></td>
            <td align="center"><input type="text" name="harga[]" class="harga" id="harga_1" value="0" style="text-align: right;" readonly="readonly"></td>
            <td align="center"><input type="text" name="qty[]" class="qty" id="qty_1" value="1" size="5" style="text-align: center;"></td>
            <td align="center"><input type="text" name="persen[]" class="persen" id="persen_1" size="5" value="0" style="text-align: center;" ></td>
            <td align="center"><input type="text" name="diskon[]" class="diskon" id="diskon_1" value="0" style="text-align: right;" ></td>
            <td align="center"><input type="text" name="jumlah[]" class="jumlah" id="jumlah_1" value="0" style="text-align: right;" readonly="readonly"></td>
            <td align="center"><img src="images/close3.png" class="delRow" id="1" width="20" style="cursor: pointer;" title="Hapus Baris"></td>
        </tr>
        </tbody>
        <tfoot>
        <tr>
            <td align="right" colspan="5">TOTAL</td>
            <td align="center"><input type='text' name="total_all" id="tot_all" value="0" style="text-align: right;" readonly="readonly"></td>
            <td align="center"><input type="submit" value="Save"></td>
        </tr>    
        </tfoot>
    </table>
    <input type="hidden" name="max_layanan" id="max_layanan" value="1"/>
    
</form>

<script type="text/javascript">
$("document").ready(function(){
    
        $("#addNewRaw").on('click',function(){
        var jml_row = parseInt($("#max_layanan").val());
        var nextrow = jml_row + 1;
        var qty = 100;
        $("#max_layanan").val(nextrow);    
        $('.atable tbody').append('<tr id="'+nextrow+'">'+
        	'<input type="hidden" name="total[]" class="total" id="total_'+nextrow+'">'+
            '<input type="hidden" name="totharga[]" class="totharga" id="totharga_'+nextrow+'">'+
            '<input type="hidden" name="layananid[]" class="layananid" id="layananid_'+nextrow+'">'+
            '<td align="center"><input type="text" name="layanan[]" class="layanan" id="layanan_'+nextrow+'" value="" required="required"></td>'+
            '<td align="center"><input type="text" name="harga[]" class="harga" id="harga_'+nextrow+'" value="0" style="text-align: right;" readonly="readonly"></td>'+
            '<td align="center"><input type="text" name="qty[]" class="qty auto" id="qty_'+nextrow+'" value="1" size="5" style="text-align: center;"></td>'+
            '<td align="center"><input type="text" name="persen[]" class="persen auto" id="persen_'+nextrow+'" value="0" size="5" style="text-align: center;"></td>'+
            '<td align="center"><input type="text" name="diskon[]" class="diskon" id="diskon_'+nextrow+'" value="0" style="text-align: right;" ></input></td>'+
            '<td align="center"><input type="text" name="jumlah[]" class="jumlah" id="jumlah_'+nextrow+'" value="0" style="text-align: right;" readonly="readonly"></td>'+
            '<td align="center"><img src="images/close3.png" class="delRow" id="'+nextrow+'" width="20" style="cursor: pointer;"  title="Hapus Baris"></td>'+
        '</tr>');
        
        $('.layanan').keyup(function(){
        	var nm_id = $(this).attr('id').split("_");
            var aud = nm_id[1];
	         cari_data(aud);
	    });
	    
        $('.diskon').keyup(function(){
            var nm_id = $(this).attr('id').split("_");
            var aud = nm_id[1];

            hitung_diskon(aud);
       });
       
       $('.persen').keyup(function(){
            var nm_id = $(this).attr('id').split("_");
            var aud = nm_id[1];

            hitung_persen(aud);
       });

       $('.qty').keyup(function(){
            var nm_id = $(this).attr('id').split("_");
            var aud = nm_id[1];

            hitung_qty(aud);
       });

       $('.delRow').on('click', function() { 
            id  = this.id;
            hasil = $("#max_layanan").val();
            console.log(hasil);
            
            $('.atable tbody tr#'+id).remove();
            $("#max_layanan").val(hasil - 1);
        
            hitung_total();
        });        
    });
    
});

</script>

<script type="text/javascript">

function cari_data(tag){
        
    var data = [
    <?php
    while ($rew = mysql_fetch_array($listLayanan)) {
        $layanan1 = str_replace("'", " ", $rew["nm_layanan"]);
        echo "{";
        echo "id: " . $rew["id"] . ", ";
        echo "harga: " . $rew["hrg_layanan"] . ", ";
        echo "value: '" . $layanan1 . "', ";
        echo "},";
    }
    ?>
    ];
   $("#layanan_"+tag).autocomplete({
    source: data,
    messages: {
        noResults: "",
        results: function(amount) {

        }
    },
    minLength: 2,
    select: function(event, ui) {
        $("#layananid_"+tag).val('');
        $("#layanan_"+tag).val('');
        $("#harga_"+tag).val('');
        $("#jumlah_"+tag).val('');
        var kecId = ui.item.id;
        var kecNama = ui.item.value;
        var kecHarga = ui.item.harga;
		
        $("#layananid_"+tag).val(kecId);
        $("#layanan_"+tag).val(kecNama);
        $("#harga_"+tag).val(kecHarga);
        $("#jumlah_"+tag).val(kecHarga);
        
    }
    
    });
    hitung_total();
}

function hitung_qty(tag){    
    var harga = parseInt($("#harga_"+tag).val());
    var qty = parseInt($("#qty_"+tag).val());
    var tot = 0;
    
    if(qty < 0){
		alert('Qty harus lebih dari 0 !');
		$("#qty_"+tag).val(0);
	} else {
		tot = harga * qty; 
		$("#jumlah_"+tag).val(tot);
	}    
     hitung_total();
}

function hitung_diskon(tag){    
    var harga = parseInt($("#harga_"+tag).val());
    var qty = parseInt($("#qty_"+tag).val());
    var diskon = parseInt($("#diskon_"+tag).val());
    var tot = 0;
    tot = (harga * qty) - diskon; 
    
    $("#jumlah_"+tag).val(tot);
    hitung_total();
}

function hitung_persen(tag){    
    var harga = parseInt($("#harga_"+tag).val());
    var qty = parseInt($("#persen_"+tag).val());
    var diskon = parseInt($("#diskon_"+tag).val());
    var tot = 0;
    tot = (harga * qty) / 100; 
    
    $("#diskon_"+tag).val(tot);
    hitung_diskon(tag);
}

function hitung_total(){
    var total_row = parseInt($("#max_layanan").val());

    tot1 = 0; 
    for (i = 1; i <= total_row; i++) {
            tot1 = tot1 + parseInt($("#jumlah_"+i).val());
        }
    $("#tot_all").val(tot1);
}

$(function(){
            
    $('.layanan').keyup(function(){
    	var nm_id = $(this).attr('id').split("_");
        var aud = nm_id[1];
         cari_data(aud);
    });
    
    $('.diskon').keyup(function(){
        var nm_id = $(this).attr('id').split("_");
        var aud = nm_id[1];

        hitung_diskon(aud);
   });
   
   $('.persen').keyup(function(){
        var nm_id = $(this).attr('id').split("_");
        var aud = nm_id[1];

        hitung_persen(aud);
   });

   $('.qty').keyup(function(){
        var nm_id = $(this).attr('id').split("_");
        var aud = nm_id[1];
		
        hitung_qty(aud);
   });
   

});
</script>

<script language='JavaScript'>
function CekMenu(tag) {
    sWin = window.open('includes/cek_menu.php?jns='+tag, 'xWin',
 'top=0,left=0,width=1200,height=550,menubar=no,scrollbars=yes');
    sWin.focus();
}
</script>

<?php

$idx = $_GET[idx];

?>
<div class="column">
	<table class="gridtable" width="100%" border="1px">
		<tr>
			<th>&nbsp;</th>
			<th>Trans Date</th>
			<th>No.Kwitansi</th>
			<th>Items</th>
			<th>Price</th>
			<th>Qty</th>
			<th>Discount</th>
			<th>Amount</th>
		</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 = '$jnsTrans' and no_booking = '$idx' and a.no_kwitansi is NULL
				and status_trans = 1 order by a.tgl_trans, a.wkt_entry";
		$r4 = mysql_query($sql4);
		$n4 = mysql_num_rows($r4);
		
		$noo = $no++;
		$tot = 0;
		while ($row4 = mysql_fetch_array($r4)) { ?>
			<tr>
				<?php if(strlen($row4[no_kwitansi]) > 0) { ?>
				<td>&nbsp;</td>
				<?php } else { ?>
				<td align="center">
					<a href="<?php echo "$SC?p=$PID&idx=".$_GET[idx]."&acts=hapus&f=".$row4[id];?>" onClick="return confirm('Apakah yakin akan di Hapus?')"><img src="images/icon-del-left.png" title="Hapus Layanan"/></a>
				</td>
				<?php } ?>
				<td align="center"><?php echo $row4[tgl_trans]; ?></td>
				<td align="center"><?php echo $row4[no_kwitansi]; ?></td>
				<td><?php echo ucwords(strtolower($row4[nm_layanan])); ?></td>
				<td align="right">Rp. <?php echo number_format($row4[harga],2,",","."); ?></td>
				<td align="center"><?php echo $row4[qty]; ?></td>
				<td align="right">Rp. <?php echo number_format($row4[diskon],2,",","."); ?></td>
				<td align="right">Rp. <?php echo number_format($row4[jumlah],2,",","."); ?></td>				
			</tr>
			
		<?php $tot = $tot + $row4[jumlah];} ?>
		<tr>
			<td align="right" colspan="6"><a href="javascript:cetakTagihan('<?php echo $_GET[idx];?>','<?php echo $jnsTrans;?>');"><img src="images/print-icon1.png" title="Print Tagihan"/></a></td>
			<td align="right" ><b>TOTAL</b></td>
			<td align="right"><b>Rp. <?php echo number_format($tot,2,",",".");?></b></td>
		</tr>
	</table>
</div>
<script language='JavaScript'>
function cetakTagihan(tag1,tag2) {
    sWin = window.open('includes/cetak_tagihan.php?idx='+tag1+'&jns='+tag2, 'xWin',
 'top=0,left=0,width=750,height=550,menubar=no,scrollbars=yes');
    sWin.focus();
}
</script>

<?php
} else {
	title("<img src='images/sales-report-icon.png' width='65' align='absmiddle' >  Guest in House ( $judul )");
	if($_GET[p] == "input_restauran" or $_GET[p] == "input_lounge") {
?>
<div align="center"><a href="<?php echo "$SC?p=$PID&jns=$jnsTrans&kd=$laay&act=pos_out";?>"><img src="images/product-sales-report-icon.png" width="20"/>&nbsp;Non Resident<img src="images/product-sales-report-icon.png" width="20"/>&nbsp;</a></div>
<?php } ?>
<br/>
<form>
<input type="hidden" name="p" value="<?php echo $PID; ?>"/>
<input type="submit" style="display: none;"/>
<?php if($_GET[p] == "input_banquet") { ?>
<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">Banquet ID</td>
		<td class="gridtable_th" align="center">Checkin Date</td>
		<td class="gridtable_th" align="center">Banquet Type</td>
		<td class="gridtable_th" align="center">Guest 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">&nbsp;</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 a.* 
			from tbl_banquet a
			where upper(no_banquet) like '%".strtoupper($_GET[kd_book])."%' and status = 'checkin' and 
			upper(a.nm_pemesan) like '%".strtoupper($_GET[nama])."%' and (upper(a.alamat) like '%".strtoupper($_GET[alamat])."%'
			or upper(a.kota) 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[no_banquet]";?>"><?php echo $row[no_banquet]; ?></a></td>
		<td class="gridtable_td"><?php echo ucwords(strtolower($row[tgl_pakai]." ".$row[wkt_pakai])); ?></td>
		<td class="gridtable_td"><?php echo ucwords(strtolower($row[keterangan])); ?></td>
		<td class="gridtable_td"><?php echo ucwords(strtolower($row[nm_pemesan])); ?></td>
		<td class="gridtable_td"><?php echo ucwords(strtolower($row[alamat]." ".$row[kota])); ?></td>
		<td class="gridtable_td"><?php echo ucwords(strtolower($row[no_tlpn]." / ".$row[email])); ?></td>
	</tr>
	<?php } ?>
</table>
<?php } else { ?>
<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">Checkin Date</td>
		<td class="gridtable_th" align="center">Room</td>
		<td class="gridtable_th" align="center">Guest 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>
<?php } ?>
</form>
<?php 
} 
}
?>