<? 
$nivel = $_GET["nivel"];
$nombrevar =$_GET["nombreuser"];
$estado =$_POST["estado"];
$rut =$_POST["identidad"];
$ano=date("Y");

$desde_ft =$_POST["desde"];
$desde=date('Y-m-d',strtotime($desde_ft));

$hasta_ft =$_POST["hasta"];
$hasta=date('Y-m-d',strtotime($hasta_ft));

require_once('../../../conf/connection.php');

$query2="SELECT * FROM ventas WHERE rut_clientes='$rut' and fecha BETWEEN '$desde' AND '$hasta'";
$rs_query2=mysql_query($query2);
$nr_ide2=mysql_num_rows($rs_query2);

if($nr_ide2==0){ 

    ?><script>alert("ERROR: No existen registros, verifique los datos ingresados."); window.close(); </script>  <? 

}else{	

$saldo="SELECT SUM(total) AS saldo FROM  ventas WHERE rut_clientes='$rut' and   fecha BETWEEN '$desde' AND '$hasta'";
$rs_query=mysql_query($saldo);
$suma=mysql_result($rs_query,0,"saldo");

?>
<!DOCTYPE html>
<html dir="ltr" lang="en-US"><head><!-- Created by Artisteer v4.3.0.60745 -->
    <meta charset="utf-8">
    <title>Home</title>
    <meta name="viewport" content="initial-scale = 1.0, maximum-scale = 1.0, user-scalable = no, width = device-width">

    <!--[if lt IE 9]><script src="https://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
    <link href="../../../style.css" rel="stylesheet" type="text/css" media="screen">
    <!--[if lte IE 7]><link rel="stylesheet" href="../../style.ie7.css" media="screen" /><![endif]-->
    <link rel="stylesheet" href="../../../style.responsive.css" media="all">


    <script src="../../../jquery.js"></script>
    <script src="../../../script.js"></script>
    <script src="../../../script.responsive.js"></script>



<style>.art-content .art-postcontent-0 .layout-item-0 { margin-bottom: 10px;  }
.art-content .art-postcontent-0 .layout-item-1 { border-right-style:solid;border-right-width:1px;border-right-color:#C4C6BD; padding-right: 10px;padding-left: 10px;  }
.ie7 .art-post .art-layout-cell {border:none !important; padding:0 !important; }
.ie6 .art-post .art-layout-cell {border:none !important; padding:0 !important; }

</style>

 <script src="../../jquery.js"></script>
<script type="text/javascript" src="../js/jquery-1.6.2.js"></script>
<script type="text/javascript" src="../js/funciones/scripts.js"></script>
<link rel="stylesheet" href="../menu/styles.css" />  
 <script>
		
function imprimir() {
	window.open("rejilla_imprime.php?desde=<? echo $desde ?>&hasta=<? echo $hasta ?>&estado=<? echo $estado ?>
	&rut=<? echo $rut ?>");
	window.history.back()
	
}
function cerrar() {
	window.close();	
}
</script>
</head>
<body>
<div id="art-main">
    <div class="art-sheet clearfix">

<div class="art-layout-wrapper">
                <div class="art-content-layout">
                    <div class="art-content-layout-row">
                      <div class="art-layout-cell art-content"><article class="art-post art-article">
                                
                                                
                                <div class="art-postcontent art-postcontent-0 clearfix"><div class="art-content-layout-wrapper layout-item-0">
<div class="art-content-layout">
    <div class="art-content-layout-row">
    <div class="art-layout-cell layout-item-1" style="width: 100%" >
 
       <h3>LISTADO DE VENTAS X PRODUCTO </h3>
        <p>Rut Cliente: <? echo $rut ?> <br>
          Desde: <? echo $desde_ft ?> Hasta: <? echo $hasta_ft ?><br><br>
          
          <input type="button" id="imprime" name="Imprimir" value="Excel" class="art-button" 
                        onClick="imprimir()">
          <input type="button" id="cerrar" name="cerrar" value="Cerrar ventana" class="art-button" onClick="cerrar()">
        </p>
        <?
		//suma total de ventas efectivo = "1" y debito="2" y cheque=3 

$query="SELECT sum(total) AS efectivo FROM ventas  WHERE f_pagos='1' AND fecha BETWEEN '$desde' AND '$hasta'";
$rs_query=mysql_query($query);
$efectivo=mysql_result($rs_query,0,"efectivo");
if($efectivo==""){$efectivo="0";}else{}
$query="SELECT sum(total) AS debito FROM ventas  WHERE f_pagos='2' AND fecha BETWEEN '$desde' AND '$hasta'";
$rs_query=mysql_query($query);
$debito=mysql_result($rs_query,0,"debito");
if($debito==""){$debito="0";}else{}





$query="SELECT sum(total) AS cheques FROM ventas  WHERE f_pagos='3' AND fecha BETWEEN '$desde' AND '$hasta'";
$rs_query=mysql_query($query);
$cheques=mysql_result($rs_query,0,"cheques");
if($cheques==""){$cheques="0";}else{}


$totalventa_pago=$efectivo+$debito+$cheques;

//fin 

		
		
		 ?>
        <p>
        </p>
        

      
          <? 
        $buscar="select *, sum(detalles_pedido.valor_total) as total_producto, COUNT(detalles_pedido.idplatos) as total_cantidad 
		 from pedido  inner join  detalles_pedido on pedido.idpedido=detalles_pedido.numero
		 where pedido.fecha BETWEEN '$desde' AND '$hasta'
		 Group By detalles_pedido.idplatos ";
		   $r=mysql_query($buscar,$conn) or die("Error en el query: ".mysql_error());
		   $n=mysql_num_rows($r);
		    //si hay registros los procesamos
			if($n>0)
			{
			?>
       
        <table  width="429" border="0">
          <tr bgcolor="#69BFE0" >
            <td width="200">PRODUCTO</td>
            <td width="88">CANTIDAD</td>
            <td width="127">TOTAL</td>
          </tr>
          <?
				while($row=mysql_fetch_assoc($r))
				{ //Procesamos cada registro como una fila de tabla
					?>
          <tr>
            <td><? $idplatos=$row["idplatos"];
					        $query_mesa="SELECT * FROM platos  WHERE idplatos='$idplatos'";
							$rs_query_mesa=mysql_query($query_mesa);
							$descrip_plato=mysql_result($rs_query_mesa,0,"descrip_plato"); 
							
							echo $descrip_plato;?></td>
            <td><? echo $row["total_cantidad"];
					   ?></td>
            <td><? echo $row["total_producto"];
					   ?></td>
            
          </tr>
          <? }?>
        </table>
        <br>
        <? } 
			else
			{
				echo "No hubieron coincidencias!";
     
			}}
			 ?>
       
       <BR>
    </div>
    </div>
</div>
</div>
</div>


</article></div>
                    </div>
                </div>
            </div><footer class="art-footer">
<p><br></p>
<p>Copyright © <? echo $ano ?>. All Rights Reserved.</p>
</footer>

    </div>
    <p class="art-page-footer"><a href="http://www.virtualsurchile.cl" target="_blank">Informatica Francisco Arias E.I.R.L.</a></p>
</div>


</body></html>