<?php

header("Content-type: application/vnd.ms-excel; name='excel'");
header("Content-Disposition: filename=CXC_resumen.xls");
header("Pragma: no-cache");
header("Expires: 0");

$fechai=$_REQUEST["fechai"];  
$fechaf=$_REQUEST["fechaf"];  

if($fechai<>"" && $fechaf<>""){$sqlfecha=" and fecsis between '".$fechai."' and '".$fechaf."'";}else{$sqlfecha="";}

include "../conexion.php";
 echo "<table width='100%' border='1' bordercolor='#000000' cellpadding='0' cellspacing='0'  >";
							echo "<tr ><td colspan='9'><CENTER>DETALLE DE VENTA CLIENTE MODALIDAD CREDITO</CENTER></td></tr>";
							echo "<tr >";
							echo "<td align='center'>#</td>";
							echo "<td align='center'>Documento/Nit</td>";
							echo "<td align='center'>Direccion</td>";
							echo "<td align='center'>Telefono</td>";
							echo "<td align='center'>Codigo Cliente</td>";
							echo "<td align='center'>Cliente</td>";
							echo "<td align='center'>Valor Credito</td>";							
							echo "<td align='center'>Total Abonos</td>";	
							echo "<td align='center'>Saldo</td>";		
							echo "</tr>";

		$c=1;	$sumaiva=0; $sumag=0;	$sumaI=0;		$sumacr=0;	 $sumas=0;$sumapagos=0;$sumac=0;
		$sumact=0;	$sumapt=0;	$sumasaldot=0;	

					  
		$sql_datoscli=mysqli_query($conexion,"SELECT nombre,direccion,telefono,documento,id	from cliente
												where estado='A'
												order by nombre asc");
				
		while ($datoscli= mysqli_fetch_row($sql_datoscli))//04e
			{		
				  $nombrec=$datoscli[0];  
				  $direccionc=$datoscli[1];  
				  $telefonoc=$datoscli[2];  
				  $documentoc=$datoscli[3];
				  $cliente=$datoscli[4];
				
				 $sql_datos=mysqli_query($conexion,"SELECT sum(valor),factura   from credito
													where cliente='".$cliente."'
													$sqlfecha
													group by factura");
					$sumaep=0;$sumaep=0;$spc=0;$sumavc=0;$sumaep=0;
				   while ($datos= mysqli_fetch_row($sql_datos))//04e
					  { 
							$valor=$datos[0];
							$factura=$datos[1];
							
							$efectivopagado=0;$saldo=0;
							 $sql_consulta=mysqli_query($conexion,"select count(*) as contar from venta_credito 
											  where cliente='".$cliente."' and factura='".$factura."'
											$sqlfecha"); 
							 while($datosrs= mysqli_fetch_row($sql_consulta)){ $contar=$datosrs[0];}
							 if($contar>=1)
							  {
								$sql_consulta=mysqli_query($conexion,"select sum(valor) as efectivopag from venta_credito 
												where cliente='".$cliente."' and factura='".$factura."'
												$sqlfecha"); 
								while($datosrs= mysqli_fetch_row($sql_consulta)){ $efectivopagado=$datosrs[0];}
							  }
							$sumavc=$sumavc+$valor;
							$sumaep=$sumaep+$efectivopagado;
					  }	//04
					$spc=$sumavc-$sumaep;
					
					 echo "<tr>
							  <td >$c</td>
							  <td >$documentoc</td>
							  <td >$direccionc</td>
							  <td >$telefonoc</td>
							  <td >$cliente</td>
							  <td >$nombrec</td>
							  <td align='right' >".number_format($sumavc)."</td>	
							  <td align='right'>".number_format($sumaep)."</td>	
							  <td align='right'>".number_format($spc)."</td>	
							</tr>";
							
				$sumact=$sumact+$sumavc;
				$sumapt=$sumapt+$sumaep;	
				$sumasaldot=$sumact-$sumapt;	
			}
								 
					echo "<tr >";
						  echo "																					
						  <td colspan=6><div align='right'>TOTAL CREDITO</div></td>
						  <td bgcolor='#00CC66'><strong>".number_format($sumact)."</strong></td>
						  <td bgcolor='#00CC66'><strong>".number_format($sumapt)."</strong></td>
						  <td bgcolor='#00CC66'><strong>".number_format($sumasaldot)."</strong></td>
						</tr>";		
					echo "</table>";

?>