<?
session_start();
if ($_SESSION['estado'] != "ON")
		{
		?>
		<script>
			parent.location.href = '../index.php';
		</script>
		<? 
		exit();
		}
$id_empresa = $_SESSION['ss_empresa'];
$id_user = $_SESSION['ss_user'];
$id_autoriza=7;
?>
<?
/*Sesiones*/
include("../conex.php");
include("../autoriza.php");
?>
<!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>Facturas Pendientes de Pago Clientes</title>
<?

function restaFechas($dFecIni, $dFecFin)
{
// Fecha en formato dd/mm/yyyy o dd-mm-yyyy retorna la diferencia en dias
// Ej.: $resultado_resta = restaFechas( $fecha_articulo , date('d-m-Y') );
    $dFecIni = str_replace("-","",$dFecIni);
    $dFecIni = str_replace("/","",$dFecIni);
    $dFecFin = str_replace("-","",$dFecFin);
    $dFecFin = str_replace("/","",$dFecFin);

    ereg( "([0-9]{1,2})([0-9]{1,2})([0-9]{2,4})", $dFecIni, $aFecIni);
    ereg( "([0-9]{1,2})([0-9]{1,2})([0-9]{2,4})", $dFecFin, $aFecFin);

    $date1 = mktime(0,0,0,$aFecIni[2], $aFecIni[1], $aFecIni[3]);
    $date2 = mktime(0,0,0,$aFecFin[2], $aFecFin[1], $aFecFin[3]);

    return round(($date2 - $date1) / (60 * 60 * 24));
}


$txtfecha = date(d)."/".date(m)."/".date(Y);

$dia = substr ($txtfecha, 0, 2);

$mes = substr ($txtfecha, 3, 2);

$aņo = substr ($txtfecha, 6, 4);


?>


<style type="text/css">
<!--
.Estilo1 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px;}
.Estilo4 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; font-weight: bold; }
-->
</style>
</head>
<body LINK="#FFFFFF" VLINK="#FFFFFF" ALINK="#FFFFFF"><br>
<div align="center" class="titulos2"> ESTADO CLIENTES AL <?=$dia.'/'.$mes.'/'.$aņo?> </div>
<div align="center">
    <table width="100% "border="0">
      <tr bgcolor="#CCCCCC">
        <th width="5%" scope="col"><div align="center">RUT</div></th>
        <th width="20%" scope="col"><div align="center">Cliente</div></th>
        <th width="6%" scope="col"><div align="center">+22   Dias</div></th>
        <th width="6%" scope="col"><div align="center">15-21 Dias</div></th>
        <th width="6%" scope="col"><div align="center">08-14 Dias</div></th>
        <th width="6%" scope="col"><div align="center">01-07 Dias</div></th>
        <th width="7%" scope="col"><div align="center">Total Vencido</div></th>
        <th width="7%" scope="col"><div align="center">00-30 Dias</div></th>
        <th width="6%" scope="col"><div align="center">31-61 Dias</div></th>
        <th width="6%" scope="col"><div align="center">61-90 Dias</div></th>
        <th width="6%" scope="col"><div align="center">+90   Dias</div></th>
        <th width="7%" scope="col"><div align="center">Total Por Vencer</div></th>
        <th width="12%" scope="col"><div align="center">Totales Deuda</div></th>
      </tr>
		<?	
		
		$ttotal_vencido=0;
		$ttotal_por_vencer=0;
		$ttotal_deuda=0;
		$total_por_vencer=0;
		$total_vencido=0;
		$total_deuda=0;
		
	$sqlfs = "SELECT  nombres.rut as rut, razon_social  FROM nombres  ORDER BY razon_social";

   
	  	  	$resfs = mysql_query($sqlfs,$enlace);	
			$cont=0;
		
 while (mysql_fetch_object ($resfs))
	{
		 $total_vencido=0;
		$rut = mysql_result($resfs, $cont, "rut");
		$nombre = mysql_result($resfs, $cont, "razon_social");
	
		//**********  No dcto **************22
	    $conti=0;
		$dias22=0;
		
		$unixtime = time();
		$settime = $unixtime  - ( 86400* 22);
		$fecha = date("Y",$settime)."-".date("m",$settime)."-".date("d",$settime); 
	
		$sqldcto = "select  sum((total-pagada)*factor) as monto FROM salida  WHERE rut_cliente='$rut' AND fecha_vencimiento<='$fecha'";
		$resdcto = mysql_query($sqldcto,$enlace);	
		while (mysql_fetch_object ($resdcto)) 
		{
		$dias22 = mysql_result($resdcto, $conti, "monto");
		$conti+=1;
		}	
		//********** Dcto **************
	    $conti=0;	
		$sqldcto = "select  monto FROM documentos_clientes WHERE rut='$rut' AND cobrado='N' AND fecha_ven<='$fecha' ";
		$resdcto = mysql_query($sqldcto,$enlace);	
		while (mysql_fetch_object ($resdcto)) 
		{
		$cheque = mysql_result($resdcto, $conti, "monto");
		$dias22+=$cheque;
		$conti+=1;
		}	
		$total_vencido+=$dias22;
		$dias22=number_format($dias22,0,',','.');
	
	//**********  No dcto **************15-21
	    $conti=0;
		$dias1521 =0;
		$unixtime = time();
		$settime = $unixtime  - ( 86400* 21);
		$fecha21 = date("Y",$settime)."-".date("m",$settime)."-".date("d",$settime); 
	     $settime = $unixtime  - ( 86400* 15);
		$fecha15 = date("Y",$settime)."-".date("m",$settime)."-".date("d",$settime); 
		$sqldcto = "select  sum((total-pagada)*factor) as monto FROM salida  WHERE rut_cliente='$rut' AND fecha_vencimiento>='$fecha21' AND fecha_vencimiento<='$fecha15'";
		$resdcto = mysql_query($sqldcto,$enlace);	
		while (mysql_fetch_object ($resdcto)) 
		{
		$dias1521 = mysql_result($resdcto, $conti, "monto");
		$conti+=1;
		}	
		//********** Dcto **************
	    $conti=0;	
		$sqldcto = "select  monto FROM documentos_clientes WHERE rut='$rut' AND cobrado='N' AND AND fecha_ven>='$fecha21' AND fecha_ven<='$fecha15'";
		$resdcto = mysql_query($sqldcto,$enlace);	
		while (mysql_fetch_object ($resdcto)) 
		{
		$cheque = mysql_result($resdcto, $conti, "monto");
		$dias1521+=$cheque;
		$conti+=1;
		}
		$total_vencido+=$dias1521;	
		$dias1521=number_format($dias1521,0,',','.');	
		
		//**********  No dcto **************08-14
	    $conti=0;
		$dias0814 =0;
		$unixtime = time();
		$settime = $unixtime  - ( 86400* 14);
		$fecha21 = date("Y",$settime)."-".date("m",$settime)."-".date("d",$settime); 
	     $settime = $unixtime  - ( 86400* 8);
		$fecha15 = date("Y",$settime)."-".date("m",$settime)."-".date("d",$settime); 
		$sqldcto = "select  sum((total-pagada)*factor) as monto FROM salida  WHERE rut_cliente='$rut' AND fecha_vencimiento>='$fecha21' AND fecha_vencimiento<='$fecha15'";
		$resdcto = mysql_query($sqldcto,$enlace);	
		while (mysql_fetch_object ($resdcto)) 
		{
		$dias0814 = mysql_result($resdcto, $conti, "monto");
		$conti+=1;
		}	
		//********** Dcto **************
	    $conti=0;	
		$sqldcto = "select  monto FROM documentos_clientes WHERE rut='$rut' AND cobrado='N' AND fecha_ven>='$fecha21' AND fecha_ven<='$fecha15'";
		$resdcto = mysql_query($sqldcto,$enlace);	
		while (mysql_fetch_object ($resdcto)) 
		{
		$cheque = mysql_result($resdcto, $conti, "monto");
		$dias0814+=$cheque;
		$conti+=1;
		}
		$total_vencido+=$dias0814;	
		$dias0814=number_format($dias0814,0,',','.');	
		
	//**********  No dcto **************01-07
	    $conti=0;
		$dias0107 =0;
		$unixtime = time();
		$settime = $unixtime  - ( 86400* 7);
		$fecha21 = date("Y",$settime)."-".date("m",$settime)."-".date("d",$settime); 
	     $settime = $unixtime  - ( 86400* 1);
		$fecha15 = date("Y",$settime)."-".date("m",$settime)."-".date("d",$settime); 
		$sqldcto = "select  sum((total-pagada)*factor) as monto FROM salida  WHERE rut_cliente='$rut' AND fecha_vencimiento>='$fecha21' AND fecha_vencimiento<='$fecha15'";
		$resdcto = mysql_query($sqldcto,$enlace);	
		while (mysql_fetch_object ($resdcto)) 
		{
		$dias0107 = mysql_result($resdcto, $conti, "monto");
		$conti+=1;
		}	
		//********** Dcto **************
	    $conti=0;	
		$sqldcto = "select  monto FROM documentos_clientes WHERE rut='$rut' AND cobrado='N' AND fecha_ven>='$fecha21' AND fecha_ven<='$fecha15'";
		$resdcto = mysql_query($sqldcto,$enlace);	
		while (mysql_fetch_object ($resdcto)) 
		{
		$cheque = mysql_result($resdcto, $conti, "monto");
		$dias0107+=$cheque;
		$conti+=1;
		}
		$total_vencido+=$dias0107;	
		$dias0107=number_format($dias0107,0,',','.');		
			
		
	//***********************	 
         $ttotal_vencido+=$total_vencido;
		
		  $ttotal_deuda+=$total_vencido;
       
		 
	//**********  No dcto **************00-30
	    $conti=0;
		$dias1521 =0;
		$unixtime = time();
		$settime = $unixtime  + ( 86400* 0);
		$fecha21 = date("Y",$settime)."-".date("m",$settime)."-".date("d",$settime); 
	     $settime = $unixtime  + ( 86400* 30);
		$fecha15 = date("Y",$settime)."-".date("m",$settime)."-".date("d",$settime); 
		$sqldcto = "select  sum((total-pagada)*factor) as monto FROM salida  WHERE rut_cliente='$rut' AND fecha_vencimiento>='$fecha21' AND fecha_vencimiento<='$fecha15'";
		$resdcto = mysql_query($sqldcto,$enlace);	
		while (mysql_fetch_object ($resdcto)) 
		{
		$dias0030 = mysql_result($resdcto, $conti, "monto");
		$conti+=1;
		}	
		//********** Dcto **************
	    $conti=0;	
		$sqldcto = "select  monto FROM documentos_clientes WHERE rut='$rut' AND cobrado='N' AND AND fecha_ven>='$fecha21' AND fecha_ven<='$fecha15'";
		$resdcto = mysql_query($sqldcto,$enlace);	
		while (mysql_fetch_object ($resdcto)) 
		{
		$cheque = mysql_result($resdcto, $conti, "monto");
		$dias0030+=$cheque;
		$conti+=1;
		}
		$total_por_vencer+=$dias0030;	
		$dias0030=number_format($dias0030,0,',','.');	
		
		//**********  No dcto **************31-61
	    $conti=0;
		$dias3161 =0;
		$unixtime = time();
		$settime = $unixtime  - ( 86400* 31);
		$fecha21 = date("Y",$settime)."-".date("m",$settime)."-".date("d",$settime); 
	     $settime = $unixtime  - ( 86400* 61);
		$fecha15 = date("Y",$settime)."-".date("m",$settime)."-".date("d",$settime); 
		$sqldcto = "select  sum((total-pagada)*factor) as monto FROM salida  WHERE rut_cliente='$rut' AND fecha_vencimiento>='$fecha21' AND fecha_vencimiento<='$fecha15'";
		$resdcto = mysql_query($sqldcto,$enlace);	
		while (mysql_fetch_object ($resdcto)) 
		{
		$dias3161 = mysql_result($resdcto, $conti, "monto");
		$conti+=1;
		}	
		//********** Dcto **************
	    $conti=0;	
		$sqldcto = "select  monto FROM documentos_clientes WHERE rut='$rut' AND cobrado='N' AND fecha_ven>='$fecha21' AND fecha_ven<='$fecha15'";
		$resdcto = mysql_query($sqldcto,$enlace);	
		while (mysql_fetch_object ($resdcto)) 
		{
		$cheque = mysql_result($resdcto, $conti, "monto");
		$dias3161+=$cheque;
		$conti+=1;
		}
		$total_por_vencer+=$dias3161;	
		$dias3161=number_format($dias3161,0,',','.');	
		
	//**********  No dcto **************61-90
	    $conti=0;
		$dias6190 =0;
		$unixtime = time();
		$settime = $unixtime  - ( 86400* 61);
		$fecha21 = date("Y",$settime)."-".date("m",$settime)."-".date("d",$settime); 
	     $settime = $unixtime  - ( 86400* 90);
		$fecha15 = date("Y",$settime)."-".date("m",$settime)."-".date("d",$settime); 
		$sqldcto = "select  sum((total-pagada)*factor) as monto FROM salida  WHERE rut_cliente='$rut' AND fecha_vencimiento>='$fecha21' AND fecha_vencimiento<='$fecha15'";
		$resdcto = mysql_query($sqldcto,$enlace);	
		while (mysql_fetch_object ($resdcto)) 
		{
		$dias6190 = mysql_result($resdcto, $conti, "monto");
		$conti+=1;
		}	
		//********** Dcto **************
	    $conti=0;	
		$sqldcto = "select  monto FROM documentos_clientes WHERE rut='$rut' AND cobrado='N' AND fecha_ven>='$fecha21' AND fecha_ven<='$fecha15'";
		$resdcto = mysql_query($sqldcto,$enlace);	
		while (mysql_fetch_object ($resdcto)) 
		{
		$cheque = mysql_result($resdcto, $conti, "monto");
		$dias6190+=$cheque;
		$conti+=1;
		}
		$total_por_vencer+=$dias6190;	
		$dias6190=number_format($dias6190,0,',','.');		
			
		//**********  No dcto **************90+
	    $conti=0;
		$dias90=0;
		
		$unixtime = time();
		$settime = $unixtime  + ( 86400* 90);
		$fecha = date("Y",$settime)."-".date("m",$settime)."-".date("d",$settime); 
	
		$sqldcto = "select  sum((total-pagada)*factor) as monto FROM salida  WHERE rut_cliente='$rut' AND fecha_vencimiento<='$fecha'";
		$resdcto = mysql_query($sqldcto,$enlace);	
		while (mysql_fetch_object ($resdcto)) 
		{
		$dias90 = mysql_result($resdcto, $conti, "monto");
		$conti+=1;
		}	
		//********** Dcto **************
	    $conti=0;	
		$sqldcto = "select  monto FROM documentos_clientes WHERE rut='$rut' AND cobrado='N' AND fecha_ven<='$fecha' ";
		$resdcto = mysql_query($sqldcto,$enlace);	
		while (mysql_fetch_object ($resdcto)) 
		{
		$cheque = mysql_result($resdcto, $conti, "monto");
		$dias90+=$cheque;
		$conti+=1;
		}	
		$total_por_vencer+=$dias90;
		$dias90=number_format($dias90,0,',','.');		
	//***********************	 	 
		 $total_deuda=$total_vencido+$total_por_vencer;
		 
		
		if ($total_deuda!=0)
		 {
		   $total_deuda=number_format( $total_deuda,0,',','.');
		     $total_por_vencer=number_format($total_por_vencer,0,',','.');
		     $total_vencido=number_format($total_vencido,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  style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong><?=$nombre?></strong></span></div></td>
				<td  style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong><?=$dias22?></strong></span></div></td>
                <td  style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong><?=$dias1521?></strong></span></div></td>
                <td  style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong><?=$dias0814?></strong></span></div></td>
                <td  style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong><?=$dias0107?></strong></span></div></td>             
				<td  style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong><?=$total_vencido?></strong></span></div></td>
                <td  style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong><?=$dias0030?></strong></span></div></td>
                <td  style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong><?=$dias3161?></strong></span></div></td>
                <td  style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong><?=$dias6190?></strong></span></div></td>
                <td  style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong><?=$dias90?></strong></span></div></td>
                <td  style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong><?=$total_por_vencer?></strong></span></div></td>
				 <td  style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong><?=$total_deuda?></strong></span></div></td>
				
	        </tr>	
			<?
			}
			$cont+=1;
		}
			
		
			$total_pagado=number_format($total_pagado,0,',','.');;
			$total_total=number_format($total_total,0,',','.');;
			$total_saldo=number_format($total_saldo,0,',','.');;
			?>
			<tr>
		        <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1">
	            &nbsp;&nbsp;&nbsp;&nbsp;	
				</span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1">
	            	<strong>Total general:</strong>
				</span></div></td>
				<td  style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong><?=$tdias22?></strong></span></div></td>
                <td  style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong><?=$tdias1521?></strong></span></div></td>
                <td  style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong><?=$tdias0814?></strong></span></div></td>
                <td  style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong><?=$tdias0107?></strong></span></div></td>             
				<td  style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong><?=$ttotal_vencido?></strong></span></div></td>
                <td  style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong><?=$tdias0030?></strong></span></div></td>
                <td  style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong><?=$tdias3161?></strong></span></div></td>
                <td  style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong><?=$tdias6190?></strong></span></div></td>
                <td  style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong><?=$tdias90?></strong></span></div></td>
                <td  style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong><?=$ttotal_por_vencer?></strong></span></div></td>
				 <td  style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong><?=$ttotal_deuda?></strong></span></div></td>
	        </tr>

    </table>
	
	
	<form name="form1" method="post" action="">
	<table width="150" border="0" align="center">
	<tr>
		<td width="30%" align="rigth">
			<input type="submit" name="salir" value="Salir" onClick="fun_salir();"/>
		</td>
		<td width="70%" align="right">
			<input type="button" name="imprimir" id="imprimir" value="Imprimir" onClick="fun_imprimir();">
		</td>
	</tr>
</table>
		<input name="idsalidaant" type="text" id="idsalidaant" style="visibility:hidden" size="1">
    </form>


  </div>
</body>
</html>

<script language="javascript" type="text/javascript">
function cerrar()
{
window.top.open('','_parent','');
window.top.close();
}
function fun_eliminar(cual)
	{
	document.form1.idsalidaant.value=cual;
	document.form1.action="anular.php";
	document.form1.target="_self";
	document.form1.submit();
	}
function fun_salir()
	{
	window.top.open('','_parent','');window.top.close();
	}
function fun_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>