<?php
 
session_start();
include("DMMYSQLG.php");
//include("DSA1000.php");
 
//var_dump($_SESSION);
//include("DMMYSQLG.php");
//inclu�mos la clase ajax
require ('xajax/xajax_core/xajax.inc.php');
//instanciamos el objeto de la clase xajax
$xajax = new xajax(); 

function agrega($form_entrada){
$dt_empres=mysqli_fetch_array(mysqli_query($con ,"SELECT * FROM dxempres WHERE cla_emp = '".$_SESSION['empresa']."'"));
//$dt_empres['fec_alta']=date('Y-m-d');
$ssql = "insert into fcomhues (no_pers,no_hab,folio,nombre,comentarios,cvmesa,cvpunto,cvarea,cvmesero,acc_fec,usuario) values ('" . $form_entrada["txtpersona"] . "','" . $form_entrada["txthabit"] . "','" . $form_entrada["txtfolio"] . "','" . $form_entrada["txtnombre"] . "','" . $form_entrada["txtcomen"] . "','" . $_GET['ms'] . "','" . $_GET['pt'] . "','" . $_GET['ar'] . "','" . $form_entrada["txtmesero"] . "','".$dt_empres['fec_alta']."','" . $_SESSION['num_usu'] . "' )";	
	if (mysqli_query($con ,$ssql)){
    	$salida = "Insertado correctamente";
	}else{
	    $salida = "No se ha insertado. Este es el error: " . mysqli_error($con);
	}
}
//registramos la funci�n creada anteriormente al objeto xajax
$xajax->registerFunction("agrega");
//El objeto xajax tiene que procesar cualquier petici�n
$xajax->processRequest();
//include('JQUERY.inc');
include("DFJSISCI.inc");

if(isset($_GET['add'])){

mysqli_query($con ,"UPDATE ctmesas set posicion=posicion+1   WHERE nparte1='".$_GET['ms']."' and status=1");
$_GET['pos']=$_GET['pos']+1;
}

?>
<html>
<script type="text/javascript" src="ajax.js"></script>
<script type="text/javascript" src="ajax2.js"></script>
<script type='text/javascript'>
</script>
<?php $xajax->printJavascript("xajax/"); ?>
<head>
<script type="text/javascript" language="javascript" src="DSSCRIPT.js"></script>
<script type="text/javascript" language="javascript" src="CMD4_PR.js?3"></script>

<link rel="stylesheet" href="css/estilo1.css" type="text/css"/>
<?php

require_once("DFJQUERY.inc");
$_SESSION['foli']='';

?>
<style type="text/css">

#Layer1 {
	position:absolute;
	width:944px;
	height:149px;
	z-index:1;
	left: 93px;
	top: 240px;
}
</style>
<style>
#regBus{
height:400px;
overflow:auto;
}

</style>
<script type='text/javascript'>
function abrir(URL){
window.open(URL,"ventana1","width=230,height=260,scrollbars=NO,minimize=0") 
}
</script>
<script type='text/javascript'>
function valida(url){ 
   
window.open(url,"ventana1","width=230,height=260,scrollbars=NO,minimize=0") 

}
function add(){

envia2('MESA5000_PR.php?add=1&pos=<?php echo $_GET['pos']; ?>&mesero=<?php echo $_GET['mesero']; ?>&pt=<?php echo $_GET['pt'] ?>&ar=<?php echo $_GET['ar']; ?>&ms=<?php echo $_GET['ms']; ?>','_self','mainForm');
}

</script> 
<script type="text/javascript" language="javascript">
function imprimePedido(){
if(ge('impresion').value==""){

		openWindow({
			action:'TIKET004_imp.php',
			name:'ReciboDePedido',
			scroll:true,
			resize:false,
			width:300
		},{
			control:'1',
			bot:ge('bot').value,
			npi:'1'
			
		});	
	
}else {
	if (confirm('LA CUENTA YA SE IMPRIMIO �DESEAS VOLVER A IMPRIMIR LA CUENTA?'))
	{
		openWindow({
			action:'TIKET004_imp.php',
			name:'ReciboDePedido',
			scroll:true,
			resize:false,
			width:300
		},{
			control:'1',		
			bot:ge('bot').value,
			npi:'1'
			
		});	

	}else{
	return false;
	}
}	
		
}


function verificaPedido(){
if(ge('impresion').value==""){

	alert('TIENE QUE IMPRIMIR LA CUENTA');
	
}else {
javascript:window.open('CAL10000.php?tpo=14&amp;pos=<?php
 echo $_GET['pos']; ?>&amp;pt=<?php
 echo $_GET['pt'] ?>&amp;ar=<?php
 echo $_GET['ar']; ?>&amp;ms=<?php
 echo $_GET['ms']; ?>&amp;cont=<?php
 echo $_GET['pos']; ?>&amp;mesero=<?php
 echo $_GET['mesero']; ?>','VENTANA','width=280,height=260,scrollbars=NO,minimize=0');
}	 
		
}

</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head>
<body>
<form name="mainForm" id="mainForm" method="post" action="" target="_self" onsubmit="return false">

  </form>

<form name="form1" id="form1" action="" method="post">
  <input name="txtmesero" type="hidden" value="<?php
  $varnp=(isset($msr['nparte1']))?$msr['nparte1']:"";
 echo $varnp; ?>">

  <div align="center">
    
    <table width="463" height="22" border='0'>
      <tr>
        <th colspan="8"> 
          <div align="center">POSICION DE LA MESA: <?php
 
		  //VERIFICACION DE LA IMPRESION
		  $sql_imp=mysqli_fetch_array(mysqli_query($con ,"SELECT num_imp FROM fimpresion WHERE fol_ven=0 and mesa='".$_GET['ms']."' and status=1"));
		  echo $sql_imp['num_imp'];
		  if ($sql_imp['num_imp']!=0)
		  $imp=1;
		  else 
		  $imp='';
		  
		  //POSICION DE LA MESA
		  $busm=mysqli_fetch_array(mysqli_query($con ,"SELECT np_desc1,nparte1 FROM ctmesas WHERE nparte1='".$_GET['ms']."'"));
		  echo "(".$busm['nparte1'].")  ".$busm['np_desc1']; ?></div>
       	</th></tr>
			<tr>
		  <td height="26">
			<div id="control" style="display:none">mesa<input name="bot" id="bot" type="text" value="<?php
  echo $_GET['ms']; ?>">
			control<input name="control" id="control" type="text" value="3">
			impresion<input name="impresion" id="impresion" type="text" value="<?php
  echo $imp; ?>">
			</div>
			<div align="right" id="botctrl">
		    <?php 
                    ////<input name="mesera" type="button" class="styTabEspB" value="MESERO" onClick="abrir('CALCAM00.php?ms=<?php
 //echo $reg['nparte1'];?7>&ar=<?php
 //echo $reg['area'];?7>&pt=<?php
 //echo $reg['descripcion'];?7>&mesero=<?php
 //echo $_GET['mesero'];?7>&cant=<?php
 //echo $_GET['cant'];?7>&tpo=89')"/> ?>
			
			<?php 
				@$dt_invent = mysqli_query($con ,"SELECT * FROM dgcatven WHERE   usuario ='".$_SESSION['num_usu']."'");
				
                //Se verifica si ya ha sido cargado este producto en venta e inicializa $canPed
                //echo mysqli_error($con);
                if (mysqli_num_rows($dt_invent) > 0) {
					
                   $display="";
                } else {
                 $display="display:none";
                }
				//var_dump($_SESSION);
			?>
			<input  type="button" class="styTabEspB" onClick="getOnLayer({action:'DYVALPASS.php',idLayer:'divValPass',title:'CLAVE NUEVO MESERO'},{arg:'M',onIntro:'checkPass'})" style="<?php echo $display;?>" value="C. MESERO" />
			  <input name="pagar2" type="button" class="styTabEspB" onClick="add()" value="AGREGAR"/>
			 <input name="pagar2" type="button" class="styTabEspB" onClick="envia2('MESASPIK_PR.php','_self','form1')" value="REGRESAR"/>

	      </div></td>
		</tr>
	</table>


 <table border="0" align="center">
	
	  <tr class="condes" style="border:double">
	   	  <?php
 
			$res=mysqli_fetch_array(mysqli_query($con ,"select status from ctmesas where nparte1='".$_GET['ms']."'"));
				if ($res['status']==1){
			$bcon=mysqli_fetch_array(mysqli_query($con ,"select count(*) as valor from fpos where mesa='".$_GET['ms']."' and status='1'"));
		if ($bcon['valor']!=0){ // si la consulta es uno okupado 
//		echo "ocupado";
//$params[]="t1=".'B'.$_REQUEST['bot']."";
		}else if ($bcon['valor']==0){ // si la consulta es 0 desocupado 
		$mod=mysqli_query($con ,"update ctmesas set status=0, fol_ven=0 where nparte1='".$_GET['ms']."'");
		mysqli_query($con ,"update ctmesas set eliminado=1 where codigo<>'0' and status=0 and fol_ven=0 and nparte1='".$_GET['ms']."'");
		$mod_hues=mysqli_query($con ,"update fcomhues set status=0 where cvmesa=".$_GET['ms']."  and status=1 ");
		echo mysqli_error($con);	
                $_SESSION['habitacion']="";
                 $_SESSION['foliohb'] = ""; 
                   $_SESSION['hsp']="";
		echo	"<script type='text/javascript'>
				alert('MESA DESOCUPADA');
				envia2('MESASPIK_PR.php','_self','form1');
				</script> ";
		} 								
	}




$nums=mysqli_fetch_array(mysqli_query($con ,"select max(nparte1) as valor from fpos where mesa='".$_GET['ms']."' and status='1'"));
$cuenta=$_GET['pos'];

if(intval($cuenta)>=intval($nums['valor'])){
$cuenta=$_GET['pos'];
}else{
$cuenta=$nums['valor'];	 
}

			
	
	 if ($cuenta==1){
	 $div=1;
	 $bandera=1;
	 }else {
	 $div=$cuenta/2;
	 }
For ($i=1;$i<=$cuenta;$i++)
{ ///Numero de posiciones
$txthabit=isset($_POST['txthabit'])?$_POST['txthabit']:"";
	//Verificar si la posicion ya tiene una cuenta abierta
	$sql=mysqli_query($con ,"select * from fpos where nparte1='".$i."' and mesa='".$_GET['ms']."' and status='1'");
	$sql_fcom=mysqli_query($con ,"select fcomd from dgpememd  order by fcomd desc limit 1");
    $regf=mysqli_fetch_array($sql_fcom);
    $cmk=$regf['fcomd']+1;
	if ($res=mysqli_num_rows($sql)!=0){
?>
		  <td class="condes" style="border:double"><div align="center">
			<input type='button' name='aceptar' style="background: #FF0000; font-family:Arial, Helvetica, sans-serif; font-size:38px; height:60px; width:80px" value='<?php
 echo $i; ?>'  id='u-<?php
 echo $i; ?>'   onClick="envia2('CAJA1.php?mesero=<?php
 echo $_GET['mesero'];?>&pt=<?php
 echo $_GET['pt'];?>&ar=<?php
 echo $_GET['ar'];?>&ms=<?php
 echo $_GET['ms'];?>&hb=<?php
 echo $txthabit;?>&pos=<?php
 echo $i; ?>&cont=<?php
 echo $cuenta; ?>&tpo=4','_self','form1')"/> 
			  </div></td> 			
	<?php
 
	}else{?>
		  <td class="condes" style="border:double"><div align="center">
			<input type='button' name='aceptar' style="font-family:Arial, Helvetica, sans-serif; font-size:38px; height:60px; width:40px"   value='<?php
 echo $i; ?>'  id='u-<?php
 echo $i; ?>'   onClick="envia2('CAJA1.php?mesero=<?php
 echo $_GET['mesero'];?>&pt=<?php
 echo $_GET['pt'];?>&ar=<?php
 echo $_GET['ar'];?>&ms=<?php
 echo $_GET['ms'];?>&hb=<?php
 echo $txthabit;?>&pos=<?php
 echo $i; ?>&cont=<?php
 echo $cuenta; ?>&comand=<?php echo $cmk; ?>','_self','form1')" />
			  </div></td> 		
<?php
 
    $ssql1 = mysqli_query($con ,"UPDATE ctmesas SET posicion='".$cuenta."' where nparte1='".$_GET['ms']."' and descripcion='".$_GET['pt']."' and area='".$_GET['ar']."'");
	} 
	if ( ($i%$div) == 0) {
		if ($i==$cuenta) { 
			if (($div==1)&& ($bandera==1) ){
		//////////////Numero de mesa cuando sea 1?>
		<tr style="border:none"> 
			<td class="condes" style="border:double" colspan="<?php
 echo $div ?>" align="center">
			<div align="center">
			<input type='button' name='aceptar' size='29' style="font-family:Arial, Helvetica, sans-serif; font-size:38px;" value='<?php
 $busm=mysqli_fetch_array(mysqli_query($con ,"SELECT np_desc1,nparte1 FROM ctmesas WHERE nparte1='".$_GET['ms']."'"));
		  echo "(".$busm['nparte1'].")  ".$busm['np_desc1']; ?>' /> 
			</div>			</td>  
			</tr>		
<?php
   	}
		} else { ////Numero de mesa cuando sea mas de uno?> 
		  <tr style="border:none"> 
			<td class="condes" style="border:double" colspan="<?php
 echo $div ?>" align="center">
			<div align="center">
			<input type='button' name='aceptar' size='29' style="font-family:Arial, Helvetica, sans-serif; font-size:38px; " value='<?php
 $busm=mysqli_fetch_array(mysqli_query($con ,"SELECT np_desc1,nparte1 FROM ctmesas WHERE nparte1='".$_GET['ms']."'"));
		  echo "(".$busm['nparte1'].")  ".$busm['np_desc1']; ?>' /> 
			</div>			</td>  
			</tr>	
<?php
   }
	}
}?> 
		</tr>
</table>
</div>
</form>

</body>
</html>
<?php mysqli_close($con); 	//mejorar pv restaurante 245-->200 quitarle comanda manual?>
<script>
  $(document).ready(function() {

         $(document).keypress(function(tecla) {


          num=String.fromCharCode(tecla.keyCode||tecla.charCode) 
            console.log(num);
            $('#u-'+num).css("background","#FF0000");
  $('#u-'+num).trigger("click");
         });
     });

    </script>
      
