<? 
$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 tp_doc WHERE rut='$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(valor_total) AS saldo FROM  tp_doc WHERE rut='$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 PEDIDOS X CLIENTE</h3>
        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()"><br><br>
        
  <? 

        $buscar='select * from tp_doc where rut like \'%'.$rut.'%\' and  fecha like \'%'.$fecha_ft.'%\'';
		   $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="586" border="0">
				<tr bgcolor="#69BFE0" >
				<td width="147">Pedido</td>
				<td width="83">Fecha</td>
				
				<td width="170">Total </td>
				<td width="170">Detalles Pedido</td>
				</tr>
				<?
				while($row=mysql_fetch_assoc($r))
				{ //Procesamos cada registro como una fila de tabla
					?>
					<tr><?
					//$row["nomCampo"] estos nomCampo son los campos devueltos por el
					//query en la variable $r que procesa mysql_fetch_assoc
					?>
					 <td><? $pedido=$row["pedido"]; echo  $row["pedido"]; ?></td>
					 <td><? echo  $row["fecha"] ?></td>
					 
					 <td align="right"><? echo $row["valor_total"] ?></td>
					 <td align="right">
					 <? $buscar_r='select *, SUM(cantidad) as cant, SUM(valor_total) as valor from 
                     detalles_pedido_bd where numero like \'%'.$pedido.'%\' group by numero  ';
		             $rs=mysql_query($buscar_r,$conn) or die("Error en el query: ".mysql_error()); 
                      while($rows=mysql_fetch_assoc($rs))
				        { //Procesamos cada registro co
                          ?>
					   <div style="background-color:#CCC">
					     <?
						  $cant=$rows["cant"];
						  $plato=$rows["codarticulo"];
						  
					      $query="SELECT *  FROM articulos WHERE referencia='$plato'";
 					      $rs_query=mysql_query($query);
					      $nombre=mysql_result($rs_query,0,"descripcion");
					      $idgrupos=mysql_result($rs_query,0,"codfamilia");
                          echo $nombre;  ?>
					     (<? echo $cant ?>)</div>
					   <br>
                       <?
                         
                         
                         } ?></td>
				    </tr>
					
				<? }?>
                <tr>
					  <td colspan="6" align="right">Total: <? echo $suma ?></td>
				  </tr>
				</table><br>
			
<? } 
			else
			{
				echo "No hubieron coincidencias!";
     
			}} ?>
            
                                  
    </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>