<?
session_start();
if ($_SESSION['estado'] != "ON")
		{
		?>
		<script>
			parent.location.href = '../index.php';
		</script>
		<? 
		exit();
		}
$id_empresa = $_SESSION['ss_empresa'];
$id_user = $_SESSION['ss_user'];
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Cuentas Corrientes</title>
<?
include("../conex.php");
if (isset($_GET["id"]))$id=$_GET["id"];
else $id = $_POST["cliente"];
$emp=$id;
?>
<style type="text/css">
<!--
.Estilo1 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px;}
.Estilo2 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; color:#FF0000}
-->
</style>
</head>
<body LINK="#FFFFFF" VLINK="#FFFFFF" ALINK="#FFFFFF">
<div align="center" class="titulos2">Cuenta Corriente de Clientes </div>
  <div align="center">
    <table width="640px" border="0">
      <tr>
        <th width="50%"><div align="right">
            <input name="salir" type="button" class="Estilo1" id="salir" value="Salir" onclick="cerrar()"/>
        </div></th>
		<th width="50%"><div align="left">
			<input name="imprimir" type="button" class="Estilo1" id="imprimir" value="Imprimir" onclick="imprimir()"/>
        </div></th>
      </tr>
    </table>
    <table width="100%" border="0">
      <tr bgcolor="#CCCCCC">
	  	<th scope="col"><div align="center">Rut</div></th>
        <th scope="col"><div align="center">Nombre</div></th>
        <th scope="col"><div align="center">Cargo</div></th>
        <th scope="col"><div align="center">Abono</div></th>
        <th scope="col"><div align="center">Saldo</div></th>
        <th width="60" scope="col"><div align="center">Mov.</div></th>
      </tr>
	  <?
	  if ($emp == "0")
		{
		$fsql="SELECT DISTINCT(pago_cliente.rut) FROM pago_cliente, nombres WHERE pago_cliente.rut=nombres.rut ORDER BY nombres.rut";
		
			$fcons = mysql_query($fsql,$enlace);
		  	$cont=0;
			$total=0;
			
			$total_sedebe=0;
			$total_sepago=0;
			$total_sedife=0;
		  	while (mysql_fetch_object ($fcons))
		  		{	
				$rut=mysql_result($fcons, $cont, "rut");
			  	$f2sql="SELECT SUM(valor) AS cargo FROM pago_cliente WHERE rut='$rut' AND tipo ='C' AND empresa = $id_empresa";
			  	$f2cons = mysql_query($f2sql,$enlace);	
				$tcargo = mysql_result($f2cons, 0, "cargo");				
			  	$f3sql="SELECT razon_social AS nombre FROM nombres WHERE rut='$rut'";
			  	$f3cons = mysql_query($f3sql,$enlace);	
				$nombre=mysql_result($f3cons, 0, "nombre");
				
				$f4sql="SELECT SUM(valor) AS pagado FROM pago_cliente WHERE rut='$rut' AND tipo='A' AND empresa = $id_empresa";
			  	$f4cons = mysql_query($f4sql,$enlace);	
				$tabono = mysql_result($f4cons, 0, "pagado");
				$totalf2=number_format($tcargo,0,',','.');
				$totalf4=number_format($tabono,0,',','.');
				$diferencia_total=($tcargo-$tabono);
				$dif=number_format(($tcargo-$tabono),0,',','.');	
				$total_sedebe+=$tcargo;
				$total_sepago+=$tabono;
				$total_sedife+=$diferencia_total;
				if ($diferencia_total!=0){
		  	?>
				<tr>
		        <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong>
	            <?=$rut?>
		          </strong></span></div></td>
				<td class="Estilo1" style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="left"><strong>
			    <?=$nombre?>
			    </strong></div></td>
 
		        <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><span class="Estilo2"><strong>
	            <?=$totalf2;?>
		          </strong></span></div></td>
				
				 <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><span class="Estilo2"><strong>
			     <?=$totalf4?>
				   </strong></span></div></td>
				 
				  <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><span class="Estilo2"><strong>
			      <?=$dif;?>
				    </strong></span></div></td>

		        <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px; border-left-width:1px"><div align="center"><a href="clientes2.php?&id=<?=$rut;?>" target="_self"><img src="../Images/dir.gif" width="16" height="16" border="0" style="cursor:pointer" title="Click aqui para ver movimientos del Cliente..."></a></div></td>
		        </tr>	
			<?
			}
			$cont = $cont + 1;
		  }
		  ?>
			<tr>
		        <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px">&nbsp;</td>
				<td class="Estilo1" style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="left"><strong>
			    Totales:
			    </strong></div></td>
 
		        <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><span class="Estilo2"><strong>
	            <?=number_format($total_sedebe,0,',','.');?>
		          </strong></span></div></td>
				
				 <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><span class="Estilo2"><strong>
			     <?=number_format($total_sepago,0,',','.');?>
				   </strong></span></div></td>
				 
				  <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><span class="Estilo2"><strong>
			      <?=number_format($total_sedife,0,',','.');?>
				    </strong></span></div></td>

		        <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px; border-left-width:1px"><div align="center"><a href="clientes2.php?&id=<?=$rut;?>" target="_self"><img src="../Images/dir.gif" width="16" height="16" border="0" style="cursor:pointer" title="Click aqui para ver movimientos del Cliente..."></a></div></td>
		        </tr>	
			<?
		  
		}
		else
	  		{
			$rut=$emp;
			$f2sql="SELECT SUM(valor) AS cargo FROM pago_cliente WHERE rut='$rut' AND tipo ='C' AND empresa = $id_empresa";
			  	$f2cons = mysql_query($f2sql,$enlace);	
				$tcargo = mysql_result($f2cons, 0, "cargo");						
				$totalf2=number_format($tcargo,0,',','.');
	
			  	$f3sql="SELECT razon_social AS nombre FROM nombres WHERE rut='$rut'";
			  	$f3cons = mysql_query($f3sql,$enlace);	
				$nombre=mysql_result($f3cons, 0, "nombre");
				
				$f4sql="SELECT SUM(valor) AS pagado FROM pago_cliente WHERE rut='$rut' AND tipo='A' AND empresa=$id_empresa";
			  	$f4cons = mysql_query($f4sql,$enlace);	
				$tabono = mysql_result($f4cons, 0, "pagado");
				$totalf4=number_format($tabono,0,',','.');
				$dif=number_format(($tcargo-$tabono),0,',','.');	
		  	?>
				<tr>
		        <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong>
	            <?=$rut?>
		          </strong></span></div></td>
				<td class="Estilo1" style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="left"><strong>
			    <?=$nombre?>
			    </strong></div></td>
 
		        <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><span class="Estilo2"><strong>
	            <?=$totalf2;?>
		          </strong></span></div></td>
				
				 <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><span class="Estilo2"><strong>
			     <?=$totalf4?>
				   </strong></span></div></td>
				 
				  <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="right"><span class="Estilo2"><strong>
			      <?=$dif;?>
				    </strong></span></div></td>

		        <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px; border-left-width:1px"><div align="center"><a href="clientes2.php?&id=<?=$rut;?>" target="_self"><img src="../Images/dir.gif" width="16" height="16" border="0" style="cursor:pointer" title="Click aqui para ver movimientos del Cliente..."></a></div></td>
		        </tr>	
			<?
		}
	  ?>
    </table>
  </div>
</body>
</html>

<script language="javascript" type="text/javascript">

var nav4 = window.Event ? true : false;

<!-- Valida el no ingreso de los caracteres < = > | & / " ' [ ]  -->
function validac(evt){
var key = nav4 ? evt.which : evt.keyCode;
return (key != 60 && key != 61 && key != 62 && key != 124 && key != 38 && key != 47 && key != 34 && key != 39 && key != 91 && key != 93);
}	  

function volver(){
parent.location.href = '../';
}
</script>
<script language="javascript" type="text/javascript">
function cerrar()
{
window.top.open('','_parent','');
window.top.close();
}
function imprimir(){
document.getElementById('salir').style.display = "none";
document.getElementById('imprimir').style.display = "none";
window.print();
document.getElementById('salir').style.display = "block";
document.getElementById('imprimir').style.display = "block";
};
</script>