<?php
//Exportar datos de php a Excel
header("Content-Type: application/vnd.ms-excel" );
header("Expires: 0" );
header("Cache-Control: must-revalidate, post-check=0, pre-check=0" );
header("content-disposition: attachment;filename=Reportes.xls" );
?>
<HTML LANG="es">
<TITLE>::. Exportacion de Datos .::</TITLE>
</head>
<body>
<?php
require_once('../../../../conf/connection.php');
$familia=$_GET["AcboFamilias"];
$informe=$_GET["informe"]; // 1= stock normal | 2=stock critico
$desde =$_GET["desde"];
$hasta =$_GET["hasta"];

$sucu=$_GET["sucu"];
	   $query_nom="SELECT * FROM sucu WHERE codsucu='$sucu'";
	   $res_nom=mysql_query($query_nom);
	   $nom_sucu=mysql_result($res_nom,0,"nombre");

//suma de costos 
$query_costo="SELECT sum(costo) AS t_costo, sum(valor_total) AS t_venta FROM detalles_pedido WHERE fecha BETWEEN '$desde' AND '$hasta'";
$rs_query_costo=mysql_query($query_costo);
$costo=mysql_result($rs_query_costo,0,"t_costo");
if($costo==""){$costo="0";}else{}

//valores de venta
$venta=mysql_result($rs_query_costo,0,"t_venta");



?>
<tr><td>SUCURSAL: <? echo $nom_sucu ; ?></td></tr>
<BR><BR>
<table width="200" border="0" class="table">
  <tr>
    <td colspan="2" bgcolor="#69BFE0">
    RESUMEN MENSUAL
    </td>
  </tr>
  <tr>
    <td bgcolor="#69BFE0">ITEM</td>
    <td bgcolor="#69BFE0">VALOR</td>
  </tr>
  <tr>
    <td width="86">VENTA</td>
    <td width="98" align="right"><? echo $format_number = number_format($venta, 0, ',', '.'); ?></td>
  </tr>
  <tr>
    <td>COSTO</td>
    <td align="right"><? echo $format_number = number_format($costo, 0, ',', '.');   ?></td>
  </tr>
  <tr>
    <td bgcolor="#69BFE0">UTILIDAD</td>
    <td align="right" bgcolor="#69BFE0">
	<? $utilidad= $venta-$costo;  echo $format_number = number_format($utilidad, 0, ',', '.');?></td>
  </tr>
</table>
<BR>

<TABLE BORDER=1 align="center" CELLPADDING=1 CELLSPACING=1>
<TR>
<TD bgcolor="#69BFE0">FAMILIA</TD>
<TD bgcolor="#69BFE0">VENTA</TD>
<TD bgcolor="#69BFE0">COMPRA</TD>
<TD bgcolor="#69BFE0">UTILIDAD</TD>
</TR>
<?php
if($familia==0){
	$sql = "SELECT *, sum(detalles_pedido.costo) AS t_costo, sum(detalles_pedido.valor_total) AS t_venta FROM 
	articulos INNER JOIN  familias ON articulos.codfamilia=familias.codfamilia
	LEFT JOIN detalles_pedido ON  detalles_pedido.codarticulo=articulos.referencia
	WHERE articulos.sucu='$sucu' and detalles_pedido.fecha BETWEEN '$desde' AND '$hasta' GROUP BY familias.codfamilia ";
    $result=mysql_query($sql);

while($row = mysql_fetch_array($result)) {
	$tf_venta=$row["t_venta"];
	$tf_costo=$row["t_costo"];
	$t_familia=$tf_venta-$tf_costo;
 ?>
<tr>
<td><? echo $row["nombre"] ?> </td>
<td><? echo $row["t_venta"] ?></td>
<td><? echo $row["t_costo"] ?></td>
<td><? echo $t_familia ?></td>
</tr>
<? } ?>
<tr>
<td> </td>
<td bgcolor="#69BFE0"><? echo $venta ?></td>
<td bgcolor="#69BFE0"><? echo $costo ?></td>
<td bgcolor="#69BFE0"><? echo $utilidad ?></td>
</tr>
 <?
mysql_free_result($result);
mysql_close(); //Cierras la Conexión
}else{
	
		$sql = "SELECT *, sum(detalles_pedido.costo) AS t_costo, sum(detalles_pedido.valor_total) AS t_venta FROM 
	articulos INNER JOIN  familias ON articulos.codfamilia=familias.codfamilia
	LEFT JOIN detalles_pedido ON  detalles_pedido.codarticulo=articulos.referencia
	WHERE articulos.sucu='$sucu' and familias.codfamilia='$familia' and detalles_pedido.fecha BETWEEN '$desde' AND '$hasta' GROUP BY familias.codfamilia ";
    $result=mysql_query($sql);

while($row = mysql_fetch_array($result)) {
	$tf_venta=$row["t_venta"];
	$tf_costo=$row["t_costo"];
	$t_familia=$tf_venta-$tf_costo;
 ?>
<tr>
<td><? echo $row["nombre"] ?> </td>
<td><? echo $row["t_venta"] ?></td>
<td><? echo $row["t_costo"] ?></td>
<td><? echo $t_familia ?></td>
</tr>
<? } ?>

 <? }
?>
</table>
</body>
</html>