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

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


title("<img src='images/judul-setting.png' width='65' align='absmiddle' >  Master Point of Sales");
echo "<BR>";

if($_POST[actions] == "tambah"){
	$cek1 = getFromTable("select count(id) + 1 from tbl_layanan ");
	
	mysql_query("insert into tbl_layanan (id, nm_layanan, jns_layanan, hrg_layanan, jenis, ket_layanan, food, bafeer, tax, service) values 
	('$cek1', '".$_POST[nm_layanan]."', '".$_POST[jns]."', ".$_POST[hrg_layanan].", '".$_POST[jns_layanan]."'
	, '".$_POST[ket_layanan]."', '".$_POST[hrg_food]."', '".$_POST[hrg_bufeer]."', '".$_POST[hrg_tax]."', '".$_POST[hrg_service]."')");
	
	echo "<script language='JavaScript'>document.location='$SC?p=$PID&jns=".$_POST[jns]."'</script>";
}

if($_POST[actions] == "ubah"){
	$idx = $_GET['idx'];
	
	$cek1 = getFromTable("select count(item_id) from tbl_transaksi where jns_trans != 'ROOM' and item_id = $idx ");
	$cek2 = getFromTable("select upper(nm_layanan) from tbl_layanan where id = $idx ");
	if ($cek1 > 0 and $cek2 != strtoupper($_POST[nm_layanan])){
		echo "<script>alert('You can not change! Data are ready in transactions.');</script>";
	} else {
		mysql_query("update tbl_layanan set nm_layanan='".$_POST[nm_layanan]."', jenis='".$_POST[jns_layanan]."', hrg_layanan=".$_POST[hrg_layanan].",  ket_layanan= '".nl2br($_POST[ket_layanan])."', food= '".$_POST[hrg_food]."', bafeer='".$_POST[hrg_bufeer]."', tax='".$_POST[hrg_tax]."', service='".$_POST[hrg_service]."' where id=".$idx);
	}
	
	echo "<script language='JavaScript'>document.location='$SC?p=$PID&jns=".$_POST[jns]."'</script>";
}

if($_POST[actions] == "delete"){
	//$cek1 = getFromTable("select count(id) + 1 from tbl_layanan ");
	$idx = $_GET['idx'];
	mysql_query("update tbl_layanan set status_layanan = 0 where id=".$idx);
	echo "<script language='JavaScript'>document.location='$SC?p=$PID&jns=".$_POST[jns]."'</script>";
}


if ($_GET[act] == "tambah") {
    $keter = getFromTable("select keterangan from tbl_referensi where id = '".$_GET[jns]."' and kd = 'POS'");
    $q1 = @mysql_query("select * from tbl_referensi where  kd = 'JPS'");
   
    ?>
    <form enctype="multipart/form-data" method="POST">
    <input type="hidden" name="p" value="<?php echo $_GET[p];?>" />
    <input type="hidden" name="jns" value="<?php echo $_GET[jns];?>" />
    <input type="hidden" name="actions" value="tambah" />
        <table width="40%" border="0" class="gridtable_form">
        	<tr>
                <td>Service Type</td>
                <td>: <input type="text" value="<?php echo $keter; ?>" readonly="readonly"></td>   
            </tr>  
            <tr>
                <td>Name of Service</td>
                <td>: <input type="text" name="nm_layanan" value="" required="required"></td>   
            </tr>      
			<tr>
                <td>Type of Service</td>
                <td>: 
				<select name="jns_layanan" >
				<?php  while (@$row = mysql_fetch_array($q1)) :?>
					<?php if($row['id']!='000'):?>
					<option value="<?=$row['keterangan'];?>"   ><?=$row['keterangan'];?></option>
					<?php endif;?>
				<?php endwhile;?>
				</select>
				</td>   
            </tr>            
            <tr>
                <td>Description</td>
                <td>: <textarea name="ket_layanan" required="required"> </textarea></td>  
            </tr>
            <!--tr>
                <td>Image</td>
                <td>: <input type="file" name="gambar" value=""></td>  
            </tr-->
            <tr>
                <td>Real Price</td>
                <td>: <input type="text" name="hrg_food" value="0" class="hrg_food" onkeyup="hitung_total()" style="text-align: right" required="required"></td>   
            </tr>
            <tr>
                <td>Bufeer</td>
                <td>: <input type="text" name="hrg_bufeer" value="0" class="hrg_bufeer" onkeyup="hitung_total()" style="text-align: right" required="required"></td>   
            </tr>
            <tr>
                <td>Service</td>
                <td>: <input type="text" name="hrg_service" value="0" class="hrg_service" onkeyup="hitung_total()" style="text-align: right" required="required"></td>   
            </tr>
            <tr>
                <td>Tax</td>
                <td>: <input type="text" name="hrg_tax" value="0" class="hrg_tax" onkeyup="hitung_total()" style="text-align: right" required="required"></td>   
            </tr>
            <tr>
                <td>Price</td>
                <td>: <input type="text" name="hrg_layanan" value="0" class="hrg_layanan" style="text-align: right" required="required"></td>   
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>: <input type="submit" value="Save"></td>   
            </tr>
        </table>
    </form>
    <script language='JavaScript'>
    	function hitung_total(){
		    var food = parseInt($(".hrg_food").val());
		    var buffer = parseInt($(".hrg_bufeer").val());
		    var service = parseInt($(".hrg_service").val());
		    var tax = parseInt($(".hrg_tax").val());

		    tot1 = food + buffer + service + tax; 
		    $(".hrg_layanan").val(tot1);
		}		
    	
    </script>
    <?php
    
}else if ($_GET[act] == "edit") {

    $sql = "select a.*, b.keterangan, case when a.status_layanan = 1 then 'Tersedia' else 'Kosong' end as status from tbl_layanan a, tbl_referensi b 
    where a.jns_layanan = b.id and b.kd = 'POS' and a.jns_layanan = '".$_GET[jns]."' and a.id= '".$_GET['idx']."' order by a.nm_layanan ";
	
	@$r1 = mysql_query($sql,$con);
    @$n1 = mysql_num_rows($r1);
	@$row = mysql_fetch_object($r1);
	$keter = getFromTable("select keterangan from tbl_referensi where id = '".$_GET[jns]."' and kd = 'POS'");
	$q1 = @mysql_query("select * from tbl_referensi where  kd = 'JPS'");
    ?>
    <form enctype="multipart/form-data" method="POST">
    <input type="hidden" name="p" value="<?php echo $_GET[p];?>" />
    <input type="hidden" name="jns" value="<?php echo $_GET[jns];?>" />
    <!-- <input type="hidden" name="jns_layanan" value="<?php echo $_GET[jns];?>" /> -->
    <input type="hidden" name="actions" value="ubah" />
        <table width="40%" border="0" class="gridtable_form">
        	<tr>
                <td>Service Type</td>
                <td>: <input type="text" value="<?php echo $keter;?>" readonly="readonly"></td>   
            </tr>  
            <tr>
                <td>Name of Service</td>
                <td>: <input type="text" name="nm_layanan" value="<?=$row->nm_layanan;?>" required="required"></td>   
            </tr>   
			<tr>
                <td>Type of Service</td>
                <td>: 
				<select name="jns_layanan" > 
				<?php  while (@$rows = mysql_fetch_array($q1)) :?>
					<?php if($rows['id']!='000'):?>
					<option value="<?=$rows['keterangan'];?>" <?=($rows['keterangan']==$row->jenis)?'selected="selected"':''?>   ><?=$rows['keterangan'];?></option>
					<?php endif;?>
				<?php endwhile;?>
				</select>
				</td>   
            </tr>     
            <tr>
                <td>Description</td>
                <td>: <textarea name="ket_layanan" required="required"><?=$row->ket_layanan;?></textarea></td>  
            </tr>
            <!--tr>
                <td>Image</td>
                <td>: <input type="file" name="gambar" value=""></td>  
            </tr-->
            <tr>
                <td>Real Price</td>
                <td>: <input type="text" name="hrg_food" value="<?=$row->food;?>" class="hrg_food" onkeyup="hitung_total()" style="text-align: right" required="required"></td>   
            </tr>
            <tr>
                <td>Bufeer</td>
                <td>: <input type="text" name="hrg_bufeer" value="<?=$row->bafeer;?>" class="hrg_bufeer" onkeyup="hitung_total()" style="text-align: right" required="required"></td>   
            </tr>
            <tr>
                <td>Service</td>
                <td>: <input type="text" name="hrg_service" value="<?=$row->service;?>" class="hrg_service" onkeyup="hitung_total()" style="text-align: right" required="required"></td>   
            </tr>
            <tr>
                <td>Tax</td>
                <td>: <input type="text" name="hrg_tax" value="<?=$row->tax;?>" class="hrg_tax" onkeyup="hitung_total()" style="text-align: right" required="required"></td>   
            </tr>
            <tr>
                <td>Price</td>
                <td>: <input type="text" name="hrg_layanan" value="<?=$row->hrg_layanan;?>" class="hrg_layanan" style="text-align: right" required="required"></td>   
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>: <input type="submit" value="Save"></td>   
            </tr>
        </table>
    </form>
    <script language='JavaScript'>
    	function hitung_total(){
		    var food = parseInt($(".hrg_food").val());
		    var buffer = parseInt($(".hrg_bufeer").val());
		    var service = parseInt($(".hrg_service").val());
		    var tax = parseInt($(".hrg_tax").val());

		    tot1 = food + buffer + service + tax; 
		    $(".hrg_layanan").val(tot1);
		}		
    	
    </script>
    <?php
    
}else if ($_GET[act] == "hapus") {

    $sql = "select a.*, b.keterangan, case when a.status_layanan = 1 then 'Tersedia' else 'Kosong' end as status from tbl_layanan a, tbl_referensi b 
    where a.jns_layanan = b.id and b.kd = 'POS' and a.jns_layanan = '".$_GET[jns]."' and a.id= '".$_GET['idx']."' order by a.nm_layanan ";
   
	@$r1 = mysql_query($sql,$con);
    @$n1 = mysql_num_rows($r1);
	@$row = mysql_fetch_object($r1);
	$keter = getFromTable("select keterangan from tbl_referensi where id = '".$_GET[jns]."' and kd = 'POS'");
    ?>
    <form enctype="multipart/form-data" method="POST">
    <input type="hidden" name="p" value="<?php echo $_GET[p];?>" />
    <input type="hidden" name="jns" value="<?php echo $_GET[jns];?>" />
    <input type="hidden" name="jns_layanan" value="<?php echo $_GET[jns];?>" />
    <input type="hidden" name="actions" value="delete" />
        <table width="40%" border="0" class="gridtable_form">
        	<tr>
                <td>Service Type</td>
                <td>: <input type="text" value="<?php echo $keter;?>" readonly="readonly"></td>   
            </tr>  
            <tr>
                <td>Name of Service</td>
                <td>: <input type="text" name="nm_layanan" value="<?=$row->nm_layanan;?>" readonly="readonly" required="required"></td>   
            </tr>            
            <tr>
                <td>Description</td>
                <td>: <textarea name="ket_layanan" required="required" readonly="readonly"><?=$row->ket_layanan;?></textarea></td>  
            </tr>
            <!--tr>
                <td>Image</td>
                <td>: <input type="file" name="gambar" value="" readonly="readonly"></td>  
            </tr-->
            <tr>
                <td>Real Price</td>
                <td>: <input type="text" name="hrg_food" readonly="readonly" value="<?=$row->food;?>" class="hrg_food" onkeyup="hitung_total()" style="text-align: right" required="required"></td>   
            </tr>
            <tr>
                <td>Bufeer</td>
                <td>: <input type="text" name="hrg_bufeer" readonly="readonly" value="<?=$row->bafeer;?>" class="hrg_bufeer" onkeyup="hitung_total()" style="text-align: right" required="required"></td>   
            </tr>
            <tr>
                <td>Service</td>
                <td>: <input type="text" name="hrg_service" readonly="readonly" value="<?=$row->service;?>" class="hrg_service" onkeyup="hitung_total()" style="text-align: right" required="required"></td>   
            </tr>
            <tr>
                <td>Tax</td>
                <td>: <input type="text" name="hrg_tax" readonly="readonly" value="<?=$row->tax;?>" class="hrg_tax" onkeyup="hitung_total()" style="text-align: right" required="required"></td>   
            </tr>
            <tr>
                <td>Price</td>
                <td>: <input type="text" name="hrg_layanan" readonly="readonly" value="<?=$row->hrg_layanan;?>" class="hrg_layanan" style="text-align: right" required="required"></td>   
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>: <input type="submit" value="Hapus"></td>   
            </tr>
        </table>
    </form>
    <script language='JavaScript'>
    	function hitung_total(){
		    var food = parseInt($(".hrg_food").val());
		    var buffer = parseInt($(".hrg_bufeer").val());
		    var service = parseInt($(".hrg_service").val());
		    var tax = parseInt($(".hrg_tax").val());

		    tot1 = food + buffer + service + tax; 
		    $(".hrg_layanan").val(tot1);
		}		
    	
    </script>
    <?php
    
} else {
    ?>
    <div class="ui three column stackable grid">
  		<div class="column">
		<form>
			<input type="hidden" name="p" value="<?php echo $PID;?>"/>
			<?php echo query_master_pos("jns",$_GET[jns]," onChange='submit()'");?>
		</form> 
		</div>
	</div>
		
    <?php
    $sql = "select a.*, b.keterangan, case when a.status_layanan = 1 then 'Tersedia' else 'Kosong' end as status from tbl_layanan a, tbl_referensi b 
    where a.jns_layanan = b.id and b.kd = 'POS' and a.jns_layanan = '".$_GET[jns]."' order by a.nm_layanan ";
	
	@$r1 = mysql_query($sql,$con);
    @$n1 = mysql_num_rows($r1);
    ?>
    <table border="1" width="100%" id='mytable' class="gridtable tableWithFloatingHeader">
        <tr>
            <th class="gridtable_th">
            <?php if (strlen($_GET[jns]) > 0 ) { ?>
            <a href="<?php echo "$SC?p=$PID&act=tambah&jns=$_GET[jns]"; ?>"><img src="images/icon-checklist.png" title="Tambah Jenis Room"/></a>
            <?php } else { echo "&nbsp;"; } ?>
            </th>
            <th class="gridtable_th">SERVICE</th>
            <th class="gridtable_th">TYPE</th>
            <th class="gridtable_th">SERVICE NAME</th>
            <th class="gridtable_th">PRICE</th>
            <th class="gridtable_th">DESCRIPTION</th>
            <th class="gridtable_th">STATUS</th>
        </tr>
        <?php
            while (@$row = mysql_fetch_array($r1)) {
        ?>
        <tr>
            <td class="gridtable_td" align="center">
                <a href="<?php echo "$SC?p=$PID&act=edit&idx=$row[id]&jns=$_GET[jns]";?>"><img src="images/icon-edit.png" /></a>
                <a href="<?php echo "$SC?p=$PID&act=hapus&idx=$row[id]&jns=$_GET[jns]";?>"><img src="images/icon-delete.png" /></a>
            </td>
            <td class="gridtable_td"><?php echo ucwords(strtolower($row["keterangan"]));?></td>
            <td class="gridtable_td"><?php echo ucwords(strtolower($row["jenis"]));?></td>
            <td class="gridtable_td"><?php echo $row["nm_layanan"];?></a></td>
            <td class="gridtable_td" align="right"><?php echo number_format($row["hrg_layanan"],2,',','.'); ?></td>
            <td class="gridtable_td" align=""><?php echo $row["ket_layanan"]; ?></td>
            <td class="gridtable_td" align="center"><?php echo $row["status"]; ?></td>
        </tr>
        <?php
            }
        ?>
    </table>
    <?php
}
?>
