<?
ini_set('error_reporting', 1);
session_start();
if ($_SESSION['estado'] != "ON")
		{
		?>
		<script>
			parent.location.href = '../index.php';
		</script>
		<? 
		exit();
		}
?>
<!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>Mantenimiento de Empresas...</title>
<?
include("../abre_conexion.php");
$id=$_GET["id"];
if ($id != 0 && $_POST["txtempresa"] != NULL)
{
	$emp=$_POST["txtempresa"];
}
else
{
	$emp=0;
}
//echo $emp;
?>
<style type="text/css">
<!--
.Estilo7 {
	font-size: small;
	color: #000099;
}
.Estilo11 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: small; color: #000099; }
.Estilo1 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px;}
.Estilo2 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; color:#FF0000}
.Estilo13 {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 9px;
	color: #000099;
	text-decoration: underline;
}
.Estilo14 {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 9px;
	color: #000099;
}
-->
</style>
</head>
<body LINK="#FFFFFF" VLINK="#FFFFFF" ALINK="#FFFFFF">
<form name="form1" method="post" action="muestra_cliente.php?id=1" target="_self">
<div align="center" class="titulos2">Cuenta Corriente de Clientes </div>
  <table width="261"  border="1" align="center" cellspacing="0">
    <tr bgcolor="#CCCCCC">
      <td width="45">Buscar:</td>
      <td width="148"><input name="txtempresa" type="text" id="txtempresa"  onKeyPress="return validac(event)"></td>
      <td width="54"><input name="cmdbuscar" type="submit" id="cmdbuscar" value="Buscar"></td>
    </tr>
  </table>
</form>
  <div align="center">
    <table width="90%" border="0">
      <tr bgcolor="#CCCCCC">
        <th width="216" scope="col">Nombre Cliente </th>
        <th width="60" scope="col">Saldo</th>
        <th width="60" scope="col">Mov.</th>
		<th width="60" scope="col">Caja</th>
      </tr>
	  <?
	  if ($emp == "0")
	  {
	  	  $esql="SELECT * FROM g_clientes ORDER BY ap_paterno ASC, ap_materno ASC, nombres ASC";
		  $cons_empresa = mysql_query($esql,$enlace);
		  $cont=0;
		  while (mysql_fetch_object ($cons_empresa))
		  {
		  	$total=0;
			$frut=mysql_result($cons_empresa, $cont, "rut");
		  	$fsql="SELECT * FROM g_movimientos WHERE rut_cliente='$frut'";
		  	$fcons = mysql_query($fsql,$enlace);
		  	$fcont=0;
		  	while (mysql_fetch_object ($fcons))
		  	{	
		  		$tipo=mysql_result($fcons, $fcont, "tipo");
				$valor=mysql_result($fcons, $fcont, "valor");
				if($tipo=="D")
				{
					$total=$total + $valor;
				}
				else
				{
					$total=$total - $valor;
				}
				$fcont=$fcont+1;
			}
			if($total!=0)
			{
				$total=$total*(-1);
		  	?>
				<tr>
		        <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong>
	            <? echo mysql_result($cons_empresa, $cont, "ap_paterno")." ".mysql_result($cons_empresa, $cont, "ap_materno")." ".mysql_result($cons_empresa, $cont, "nombres") ;?>
		          </strong></span></div></td>
				<?  
				  if($total<0)
				  {
				?>  
		        <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="center"><span class="Estilo2"><strong><?=number_format($total,2,',','.');?></strong></span></div></td>
				<?
				}
				else
				{
				?>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="center"><span class="Estilo1"><strong><?=number_format($total,2,',','.');?></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="movimientos.php?id=<?=mysql_result($cons_empresa, $cont, "rut");?>" target="mainFrame"><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>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px; border-left-width:1px"><div align="center"><a href="caja.php?id=<?=mysql_result($cons_empresa, $cont, "rut");?>" target="mainFrame"><img src="../Images/next_icon.gif" width="16" height="16" border="0" style="cursor:pointer" title="Click aqui para generar un pago del Cliente..."></a></div></td>
		        </tr>	
			<?
			}
			$cont = $cont + 1;
		  }
		}
		else
	  {
		  $esql="SELECT rut, nombres, ap_paterno, ap_materno FROM g_clientes WHERE ap_paterno LIKE '%$emp%' ORDER BY ap_paterno ASC, ap_materno ASC, nombres ASC";
		  //echo $esql;
		  $cons_empresa = mysql_query($esql,$enlace);
		  $cont=0;
		  while (mysql_fetch_object ($cons_empresa))
		  {
		  	$total=0;
			$frut=mysql_result($cons_empresa, $cont, "rut");
		  	$fsql="SELECT * FROM g_movimientos WHERE rut_cliente='$frut'";
		  	$fcons = mysql_query($fsql,$enlace);
		  	$fcont=0;
		  	while (mysql_fetch_object ($fcons))
		  	{	
		  		$tipo=mysql_result($fcons, $fcont, "tipo");
				$valor=mysql_result($fcons, $fcont, "valor");
				if($tipo=="D")
				{
					$total=$total + $valor;
				}
				else
				{
					$total=$total - $valor;
				}
				$fcont=$fcont+1;
			}
			if($total!=0)
			{
				$total=$total*(-1);
			?>
				<tr>
		        <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong>
	            <? echo mysql_result($cons_empresa, $cont, "ap_paterno")." ".mysql_result($cons_empresa, $cont, "ap_materno")." ".mysql_result($cons_empresa, $cont, "nombres") ;?>
		          </strong></span></div></td>
				<?  
				  if($total<0)
				  {
				?>  
		        <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="center"><span class="Estilo2"><strong><?=number_format($total,2,',','.');?></strong></span></div></td>
				<?
				}
				else
				{
				?>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-left-width:1px"><div align="center"><span class="Estilo1"><strong><?=number_format($total,2,',','.');?></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="movimientos.php?id=<?=mysql_result($cons_empresa, $cont, "rut");?>" target="mainFrame"><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>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px; border-left-width:1px"><div align="center"><a href="caja.php?id=<?=mysql_result($cons_empresa, $cont, "rut");?>" target="mainFrame"><img src="../Images/next_icon.gif" width="16" height="16" border="0" style="cursor:pointer" title="Click aqui para generar pago del Cliente..."></a></div></td>
		        </tr>	
			<?
			}
			$cont = $cont + 1;
		  }
		}
	  ?>
    </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 = '../index.html';
}
</script>