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


$fechainicio_ft=$_POST["desde"];
$fechainicio=date('Y-m-d',strtotime($fechainicio_ft));
$ano=date("Y");
$fechatermino_ft=$_POST["hasta"];
$fechatermino=date('Y-m-d',strtotime($fechatermino_ft));


$codigo=$_POST["idplatos"];


if($codigo==""){ ?> <script>alert("ERROR: Codigo no corresponde o los campos de texto están vacíos.");window.history.back();</script> <? }else{
   $sel_producto="SELECT * FROM articulos  WHERE referencia='$codigo'"; 
   $rs_producto=mysql_query($sel_producto);
   $nombre=mysql_result($rs_producto,0,"descripcion");
   $art=mysql_result($rs_producto,0,"referencia");
    
$query="SELECT * FROM cartola_historica WHERE art='$art' AND fecha BETWEEN '$fechainicio' AND '$fechatermino' 
ORDER BY fecha ASC ";
$rs_query=mysql_query($query);
$nr_ide=mysql_num_rows($rs_query);


if($nr_ide==0){ 

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

}else{	
    
	//saldo entrada
	$sel_saldo_entrada="SELECT sum(entrada) AS total FROM cartola_historica where art='$art' AND fecha < '$fechainicio';"; 
    $rs_saldo_entrada=mysql_query($sel_saldo_entrada);
	$saldo_entrada=mysql_result($rs_saldo_entrada,0,"total");
	
	//saldo salida
	$sel_saldo_salida="SELECT sum(salida) AS total FROM cartola_historica where art='$art' AND fecha < '$fechainicio';"; 
    $rs_saldo_salida=mysql_query($sel_saldo_salida);
	$saldo_salida=mysql_result($rs_saldo_salida,0,"total");
	
	$total_saldo=$saldo_entrada-$saldo_salida;
 ?>
<!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>
function cerrar() {
			window.close();
		}
		
function imprimir() {
	window.open("rejilla_imprime.php?fechainicio=<? echo $fechainicio ?>&fechatermino=<? echo $fechatermino ?>&codigo=<? echo $codigo ?>");
	window.history.back()
	
}
</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%" >
 
 <table width="676" border=0 cellpadding=3 cellspacing=0 class="fuente8">					
					  <tr>
						  <td align="center" >CARTOLA HISTORICA</td>
						
					  </tr>
					  
					      <td align="center" >Productos: <? echo $nombre ?></td>
       </tr>
					  <tr>
					    <td align="center" >Codigo productos: <? echo $codigo ?></td>
       </tr>
					  <tr>
					    <td align="center" >DESDE : <? echo $fechainicio_ft?> --  HASTA: <? echo $fechatermino_ft?></td>
       </tr>
					  <tr>
					    <td align="right">
                        <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()"></td>
				      </tr>
					  
                      </table>
                    <br>
                                      <? 
								  

$sel_articulos="SELECT * FROM cartola_historica WHERE art='$art' AND fecha BETWEEN '$fechainicio' AND '$fechatermino' 
ORDER BY fecha ASC  ";
$rs_articulos=mysql_query($sel_articulos);
$contador=0;
$totalfinal=0;
$iva=19;
$numero_articulos=mysql_num_rows($rs_articulos);
       
	   
           ?>
                             
                                <table width="681" border="0">
                                  <tr class="cabeceraTabla2">
                                    <td width="98" bgcolor="#7BACC6">Fecha </td>
                                    <td width="268" bgcolor="#7BACC6">Glosa </td>
                                    <td width="99" align="right" bgcolor="#7BACC6">Entrada</td>
                                    <td width="94" align="right" bgcolor="#7BACC6">Salida</td>
                                    <td width="100" align="right" bgcolor="#7BACC6">Saldo </td>
                                  </tr>
                                  
                                  <tr class="cabeceraTabla2">
                                    <td>&nbsp;</td>
                                    <td align="left">Saldo anterior</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td align="right" class="fuente8"><?
									 if($total_saldo>0)
									{ 
									  $saldo_ant=$total_saldo; echo $saldo_ant; 
									}
									 else{$saldo_ant=0; echo $saldo_ant;
									} ?></td>
                                  </tr>
                                  <? if ($numero_articulos>0) { 
								  while ($contador < mysql_num_rows($rs_articulos)) {
								  	  
								             
								 ?>
                                  <tr class="fuente8">
                                    <td><?  $fecha_ft=mysql_result($rs_articulos,$contador,"fecha"); 
									$fecha=date('d-m-Y',strtotime($fecha_ft)); echo $fecha;
									?></td>
                                    <td align="left"><? echo mysql_result($rs_articulos,$contador,"glosa");  ?></td>
                                    <td align="right"><? $entradas=mysql_result($rs_articulos,$contador,"entrada"); echo mysql_result($rs_articulos,$contador,"entrada"); ?></td>
                                    <td align="right"><? $salidas=mysql_result($rs_articulos,$contador,"salida"); echo mysql_result($rs_articulos,$contador,"salida");?></td>
                                    <td align="right"><? 
									//suma saldo
									$saldosuma=$saldosuma+$entradas;
									//resta saldo
								    $saldoresta=$saldoresta+$salidas;
									//saldos disponibles
									
									$saldos_disp=$saldosuma-$saldoresta+$saldo_ant; echo $saldos_disp
									  ?></td>
                                  </tr>
                                  
                                  <? $contador++;}?>
                                  
                                  <tr class="itemImparTabla">
                                    <td colspan="2" align="right" bgcolor="#7BACC6" class="fuente8">&nbsp;</td>
                                    <td align="right" valign="middle" bgcolor="#7BACC6" class="fuente8"><?
       	$sel_total_ent="SELECT sum(entrada) AS total_entradas FROM cartola_historica WHERE art='$art' AND fecha BETWEEN '$fechainicio' AND '$fechatermino'";
$rs_total_ent=mysql_query($sel_total_ent);
$total_entradas=mysql_result($rs_total_ent,0,"total_entradas");  $total_entradas=$total_entradas+$saldo_ant;echo $total_entradas;                             
									
									
									?></td>
                                    <td align="right" bgcolor="#7BACC6" class="fuente8" ><?
                                    	$sel_total_sal="SELECT sum(salida) AS total_salidas FROM cartola_historica WHERE art='$art' AND fecha BETWEEN '$fechainicio' AND '$fechatermino'";
$rs_total_sal=mysql_query($sel_total_sal);
$total_salida=mysql_result($rs_total_sal,0,"total_salidas"); echo $total_salida;
									
									?></td>
                                    <td align="right" bgcolor="#7BACC6" class="fuente8" ><? $total_final=$total_entradas-$total_salida; echo $total_final; ?> </td>
                                  </tr>
                                  <? }else {?>
                                  <tr class="itemImparTabla">
                                    <td colspan="5" align="center" bgcolor="#FFFFFF" class="fuente8">&nbsp;</td>
                                  </tr>
                                  <? }?>
                                </table>	
            <? }}?>
                                  <div id="resultado"></div>
    </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>