<?
session_start();
if ($_SESSION['estado'] != "ON")
		{
		?>
		<script>
			parent.location.href = '../index.php';
		</script>
		<? 
		exit();
		}
$id_empresa = $_SESSION['ss_empresa'];
$id_user = $_SESSION['ss_user'];
header('Content-type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=familiamesesclientes(kls).xls");
header("Pragma: no-cache");
header("Expires: 0");

?>
<!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>VentasMesesClientes(Kls)</title>
<?
include("../conex.php");
$txtfecha1=$_POST["txtfecha1"];

$dia1 = substr ($txtfecha1, 0, 2);
$mes1 = substr ($txtfecha1, 3, 2);

$aņo1 = substr ($txtfecha1, 6, 4);

$finx = $aņo1.'-'.$mes1.'-'.$dia1;
$inix = substr ($txtfecha1, 6, 4).'-01-01';


?>



<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="#000000" VLINK="#000000" ALINK="#000000">  <br>
<div align="center" class="titulos2">Informe VentasMesesClientes(Kls) HASTA EL <?=$txtfecha1?> </div>
<div align="center">

    <table width="90%" border="0">
      <tr bgcolor="#CCCCCC">
        <th width="8%" scope="col"><div align="center">RUT</div></th>
		<th width="10%" scope="col"><div align="center">Clientes</div></th>	
        <th width="6%" scope="col"><div align="center">Ene.</div></th>
		<th width="6%" scope="col"><div align="center">Feb.</div></th>
		<th width="7%" scope="col"><div align="center">Mar.</div></th>
		<th width="6%" scope="col"><div align="center">Abr.</div></th>
		<th width="7%" scope="col"><div align="center">May.</div></th>
		<th width="6%" scope="col"><div align="center">Jun.</div></th>
		<th width="6%" scope="col"><div align="center">Jul.</div></th>
		<th width="6%" scope="col"><div align="center">Ago.</div></th>
		<th width="6%" scope="col"><div align="center">Sep.</div></th>
		<th width="6%" scope="col"><div align="center">Oct.</div></th>
		<th width="7%" scope="col"><div align="center">Nov.</div></th>
		<th width="6%" scope="col"><div align="center">Dic.</div></th>
		<th width="7%" scope="col"><div align="center">Tot.</div></th>
     	
<?		
  
		$sqlfs = "SELECT nombres.rut, nombres.razon_social FROM movimiento, productos, nombres WHERE productos.codigo = movimiento.cod_producto AND  (movimiento.tipo_dcto = '1' OR movimiento.tipo_dcto = '2' OR movimiento.tipo_dcto = '6' or movimiento.tipo_dcto = '7') AND (movimiento.fecha>='$inix' AND movimiento.fecha <= '$finx') and movimiento.entrada_salida_traspaso='S' AND nombres.rut=movimiento.rut GROUP BY nombres.rut ORDER BY nombres.razon_social";
	

		
	$resfs = mysql_query($sqlfs,$enlace);	
	$cont=0;                     
	while (mysql_fetch_object ($resfs))
 		{
			
			$m[1]=$m[2]=$m[3]=$m[4]=$m[5]=$m[6]=$m[7]=$m[8]=$m[9]=$m[10]=$m[11]=$m[12]=0;
						
				$rut = mysql_result($resfs, $cont, "nombres.rut");
				$nombre = mysql_result($resfs, $cont, "nombres.razon_social");
	   	
		      $sql_fsm = "SELECT extract(month from fecha), SUM(cantidad*peso) AS totalmes FROM movimiento, productos WHERE  rut = '$rut'  AND (tipo_dcto = 1 OR tipo_dcto = 2 OR tipo_dcto = 6 or tipo_dcto = 7 OR tipo_dcto = 33 OR tipo_dcto = 56 or tipo_dcto = 61) AND (fecha>='$inix' and fecha<='$finx') AND entrada_salida_traspaso='S' AND productos.codigo = movimiento.cod_producto GROUP BY extract(month from fecha)";		
	
	
					$res_fsm = mysql_query($sql_fsm, $enlace);
					$rowk=0;
					while (mysql_fetch_object ($res_fsm))
				
 						{
						$me = mysql_result($res_fsm,$rowk , "extract(month from fecha)");
						$m[$me] = mysql_result($res_fsm,$rowk , "totalmes");
						$rowk+=1;
						}
						
	$totalmeses=$m[1]+$m[2]+$m[3]+$m[4]+$m[5]+$m[6]+$m[7]+$m[8]+$m[9]+$m[10]+$m[11]+$m[12];
	
				
			$totalmeses=number_format($totalmeses,0,',','.');
		
			$m[1]=number_format($m[1],0,',','.');
			$m[2]=number_format($m[2],0,',','.');
			$m[3]=number_format($m[3],0,',','.');
			$m[4]=number_format($m[4],0,',','.');
			$m[5]=number_format($m[5],0,',','.');
			$m[6]=number_format($m[6],0,',','.');
			$m[7]=number_format($m[7],0,',','.');
			$m[8]=number_format($m[8],0,',','.');
			$m[9]=number_format($m[9],0,',','.');
			$m[10]=number_format($m[10],0,',','.');
			$m[11]=number_format($m[11],0,',','.');
			$m[12]=number_format($m[12],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> <?=$m[1]?>	</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> <?=$m[2]?></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> <?=$m[3]?> </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> <?=$m[4]?></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> <?=$m[5]?> </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><?=$m[6]?></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> <?=$m[7]?></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> <?=$m[8]?> </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><?=$m[9]?></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> <?=$m[10]?> </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> <?=$m[11]?> </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> <?=$m[12]?> </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>  <?=$totalmeses?> </strong></span></div></td>
                
	        </tr>	
			<?			
			$cont+=1;
			}
			?>

    </table>
	<form name="form1" method="post" action="">

	  <input type="button" name="imprimir" id="imprimir" value="Imprimir" onClick="fun_imprimir();">
	 
    </form>
  </div>
</body>
</html>

<script language="javascript" type="text/javascript">
function cerrar()
{
window.top.open('','_parent','');
window.top.close();
}


function fun_imprimir()
	{
	document.getElementById('imprimir').style.display = "none";
	window.print();
	document.getElementById('imprimir').style.display = "block";
	};

</script>