<?php
include ("../conexion.php");
include ("../funciones/fechas.php");

 $cdgpro=$_POST["cdgpro"];
$dscpro=strtoupper($_POST["dscpro"]);
$combotipoplazo=$_POST["combotipoplazo"];

$cadena_busqueda=$_POST["cadena_busqueda"];

$where="1=1";
if ($cdgpro <> "") { $where.=" AND cdgpro='$cdgpro'"; }

if ($combotipoplazo == 1 ) { $where.=" AND al_dia > 0"; }
if ($combotipoplazo == 2 ) { $where.=" AND 0_15 > 0"; }
if ($combotipoplazo == 3 ) { $where.=" AND 15_30 > 0"; }
if ($combotipoplazo == 4 ) { $where.=" AND 30_60 > 0"; }
if ($combotipoplazo == 5 ) { $where.=" AND 60_90 > 0"; }
if ($combotipoplazo == 6 ) { $where.=" AND 90_mas > 0"; }
if ($dscpro <> "") { $where.=" AND dscpro like '%".$dscpro."%'"; }


 if ($result = $mysqli->query("SELECT * FROM cartera_cxp  WHERE  ".$where )) {
   $filas = $result->num_rows;
   $result->close();
}		
			
			
			//    cambio version   8.0.2 
			
					 $saldiatot= 0;
					 $salventot= 0;
					 $intmortot= 0;
					 $salpagtot= 0;
					 $salacttot= 0;
					 
					 
					 
					 	
					  ?>
		

<html>
	<head>
		<title>Clientes</title>
		<link href="../estilos/estilos.css" type="text/css" rel="stylesheet">
		<script language="javascript">
		
	
		
		      

		function inicio() {
			var numfilas=document.getElementById("numfilas").value;
			var indi=parent.document.getElementById("iniciopagina").value;
			var contador=1;
			var indice=0;
			if (indi>numfilas) { 
				indi=1; 
			}
			parent.document.form_busqueda.filas.value=numfilas;
			parent.document.form_busqueda.totalfacturas.value=document.getElementById("totalfacturas").value;
			parent.document.form_busqueda.paginas.innerHTML="";		
			while (contador<=numfilas) {
				texto=contador + "-" + parseInt(contador+15);
				if (indi==contador) {
					parent.document.form_busqueda.paginas.options[indice]=new Option (texto,contador);
					parent.document.form_busqueda.paginas.options[indice].selected=true;
				} else {
					parent.document.form_busqueda.paginas.options[indice]=new Option (texto,contador);
				}
				indice++;
				contador=contador+16;
			}
		}
		
		function CambiaColor(esto,fondo,texto)
		{
			colorfondo=esto.style.background;
			esto.style.background=fondo;
			esto.style.color=texto;
			return colorfondo;
		}
		
		function CambiaColor2(esto,texto)
		{
			esto.style.background=colorfondo;
			esto.style.color=texto;
		}
		
				function nobackbutton(){	
   window.location.hash="no-back-button";	
   window.location.hash="Again-No-back-button" //chrome	
   window.onhashchange=function(){window.location.hash="no-back-button";}	
}

function pon_prefijo(saldiatot,salventot,intmortot,salpagtot,salacttot) {
	parent.document.form_busqueda.saldiatot.value=saldiatot;
	parent.document.form_busqueda.salventot.value=salventot;
	parent.document.form_busqueda.intmortot.value=intmortot;
	parent.document.form_busqueda.salpagtot.value=salpagtot;
	parent.document.form_busqueda.salacttot.value=salacttot;
    
}
		
		</script>
	</head>

	<body onload= nobackbutton(); inicio()>	
		<div id="">
			<div id="zonaContenidoPP">
			<div align="center">
			<table class="fuente9" width="100%" cellspacing=0 cellpadding=0 border=0 ID="Table1">
            
			<input type="hidden" name="numfilas" id="numfilas" value="<?php echo $filas?>">
			<input type="hidden" name="totalfacturas" id="totalfacturas" value="<?php echo $totalfacturas?>">
				<?php
		$contador=0;
				
					if ($filas > 0) { 
					
					
	$consulta2 = "SELECT 	cdgpro,dscpro, sum(al_dia) as al_dia , sum(0_15) as 0_15, sum(15_30) as 15_30, sum(30_60) as 30_60 , sum(60_90) as 60_90  , sum(90_mas) as 90_mas , sum(intmor) as intmor, sum(salven) as salven FROM cartera_cxp WHERE  ".$where. "  GROUP BY cdgpro ORDER BY dscpro ASC " ;

if ($resultado = $mysqli->query($consulta2)) 

    while ($fila = $resultado->fetch_assoc()) {
        $al_dia  = $fila["al_dia"];
        $d0_15   = $fila["0_15"];
		$d15_30  = $fila["15_30"];
		$d30_60  = $fila["30_60"];
        $d60_90  = $fila["60_90"];
        $d90_mas = $fila["90_mas"];
        $salven  = $fila["salven"];
        $intmor  = $fila["intmor"];
		$salpag  = $salven+$al_dia+$intmor;
	    $salact  = $salven+$al_dia;
		$cdgpro  = $fila["cdgpro"];
		$cdgpro  = sprintf("%06d",$cdgpro); 
		$dscpro  = $fila["dscpro"];				 
				 
				 
				 
				 $salpag=$salven+$al_dia+$intmor;
				 $salact=$salven+$al_dia;
				 	
								if ($contador % 2) { $fondolinea="itemParTabla"; } else { $fondolinea="itemImparTabla"; }?>
								
								<tr class="<?php echo $fondolinea ?>" onMouseOver="CambiaColor(this,'#000000','#ffffff')" onMouseOut="CambiaColor2(this,'#000000')" >

								<td width="1%"><?php $contador+1;?></td>
								<td width="7%" align="right"><div align="left"><?php echo $cdgpro?></div></td>	
									<?php	  $dscpro = substr($dscpro, 0,45);  ?>	
								<td width="37%"><div align="left"><?php echo $dscpro ?></div></td>						 
								<td width="11%"><div align="right"><?php echo number_format($al_dia,2,".",",")?></div></td>
                                <td width="11%"><div align="right"><?php echo number_format($salven,2,".",",")?></div></td>
                                <td width="11%"><div align="right"><?php echo number_format($salact,2,".",",")?></div></td>
                                
                             

						</tr>
						</tr>	
						<?php $contador++;
							}
						?>			
					</table>
                <?php    
				
 $sel_resultado="SELECT  sum(intmor) as intmortot, sum(salven) as salventot , sum(al_dia) as saldiatot FROM cartera_cxp WHERE  ".$where ;

if ($result = $mysqli->query( $sel_resultado)) 

    while ($row = $result->fetch_assoc()) {
        $saldiatot  = $row["saldiatot"];
		$salventot  = $row["salventot"];
		$intmortot  = $row["intmortot"];
		
				 $salpagtot=$salventot+$saldiatot+$intmortot;
				 $salacttot=$salventot+$saldiatot;
				 			
						 $contador++; 
					 }
               } else { ?>
					<table class="fuente8" width="87%" cellspacing=0 cellpadding=3 border=0>
						<tr>
							<td width="100%" class="mensaje"><?php echo 	"No hay ninguna factura que cumpla con los criterios de b&uacute;squeda";?></td>
					    </tr>
					</table>					
					<?php } 
					
					 $saldiatot= number_format($saldiatot,2,".",",");
					 $salventot= number_format($salventot,2,".",",");
					 $intmortot= number_format($intmortot,2,".",",");
					 $salpagtot= number_format($salpagtot,2,".",",");
					 $salacttot= number_format($salacttot,2,".",",");
					
					?>	
                  	
                    
                 <script languaje="javascript">
	pon_prefijo("<?php echo $saldiatot ?>","<?php echo $salventot ?>","<?php echo $intmortot ?>","<?php echo $salpagtot ?>","<?php echo $salacttot ?>");
	
		</script>			
				</div>
			</div>
		  </div>			
		</div>
	</body>
</html>

