<?php

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=Reporte Venta de inventario por familias.xls");

 include ("../conexion.php");	

       $parametros = "SELECT * FROM web_0000 ";
       $result_parametros = $mysqli->query($parametros);
       $row_para = $result_parametros->fetch_array();
     
	   $NombreComercial =  $row_para["NOMCOM"];
	   $Razon           =  $row_para["RAZSOC"];
	   $Direccion       =  $row_para["DIREMP"];
	   $Cedula          =  $row_para["CEDJUR"];
	   $Telefono        =  $row_para["NUMTEL"];
	   
    $costo_total_existencia = 0;
	$costo_total = 0 ;
	$subtot    = 0 ;	
    $descuento = 0 ;	
	$impven    = 0 ;	
	$total     = 0 ;	

echo $cdgpro=$_GET["cdgpro"];

$contador =  0;

$fecini = $_GET["fechainicio"];
$fecfin     = $_GET["fechafin"];


 $fechainicio = implode("-", array_reverse(explode("-", $fecini))); 
 $fechafin    = implode("-", array_reverse(explode("-", $fecfin))); 

if ($cdgpro=="") {
	 
	 $proveedor="TODOS LOS PROVEEDORES";
	 
	     } else {


	   $query_codcaj="SELECT * FROM proveedores  WHERE cdgpro=$cdgpro ";
       $resultado = $mysqli->query( $query_codcaj);
       $row = $resultado->fetch_array();
        
		$proveedor    = $row["dscpro"];
     }


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Reporte de Ventas por Proveedor</title>
</head>
<body>
<table width="50%" border="0" cellspacing="0" cellpadding="0">
    <tr>   </tr>
  <tr>
     <td></td>
 <td colspan="8"  bgcolor="skyblue"><div align="center"><strong>REPORTE VENTAS POR PROVEEDOR</strong></div></td>
  </tr>
   
  <tr>
  
  <tr>
       <td></td>
       <td colspan="8"   border="1"><div align="center"><font size="3" >PROVEEDOR <?php echo $cdgpro; ?> -  <?php echo $proveedor; ?></font></div></td>
  </tr>
	 
       <tr>
       <td></td>
       <td colspan="8"   border="1"><div align="center"><font size="3" >Fecha del <?php echo $fecini; ?> Al <?php echo $fecfin; ?> </font></div></td>
     </tr>
	 
	  <tr>   </tr>
	   
      <td width="10%"></td>
    <td  border="1" width="10%"><div align="center"><strong>CODIGO</strong></div></td>
    <td width="37%"><div align="center"><strong>DESCRIPCION</strong></div></td>
	<td width="37%"><div align="center"><strong>FAMILIA</strong></div></td>
	<td width="37%"><div align="center"><strong>LINEA</strong></div></td>
	<td width="16%"><div align="center"><strong>EXISTENCIA</strong></div></td>
	<td width="16%"><div align="center"><strong>COSTO EXISTENCIA</strong></div></td>
    <td width="16%"><div align="center"><strong>CANTIDAD VENDIDA</strong></div></td>
	<td width="16%"><div align="center"><strong>COSTO VENDIDO</strong></div></td>

  </tr>
  
<?php


$fechafin=trim($fechafin);
$fechainicio=trim($fechainicio);
$where="1=1";

$where2="1=1";

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

//
if (($fechainicio<>"") and ($fechafin<>"")) {
	 $where.=" AND fecfac between '".$fechainicio."' AND '".$fechafin."'";
} else {
	if ($fechainicio<>"") {
	 	$where.=" AND fecfac >='".$fechainicio."'";
	} else {
		if ($fechafin<>"") {
		$where.=" AND fecfac <='".$fechafin."'";
		}
	}
}




 if ($result = $mysqli->query("SELECT CDGBAR FROM web_1001 WHERE estart=1  AND ".$where2)) {
   $filas = $result->num_rows;
   $result->close();
   
	}


	if ($filas > 0) {
					
								   					
				$consulta = "SELECT CDGART,CDGBAR,DSCART,EXIACT,cosart,impart, CDGFAM, CDGLIN FROM web_1001  WHERE  estart=1  AND ".$where2. "  ORDER BY   dscart ASC  " ;
				if ($resultado = $mysqli->query($consulta)) 

  			   while ($fila = $resultado->fetch_assoc()) {		
					       
					 	    $cdgart   = $fila["CDGART"];
							$cdgbar   = $fila["CDGBAR"];
							$dscart   = $fila["DSCART"];
							$exiact   = $fila["EXIACT"];
							$cosart   = $fila["cosart"];
							$impart   = $fila["impart"];
							$cdgfam   = $fila["CDGFAM"];
							$cdglin   = $fila["CDGLIN"];
							
							
															

	$queryfam = "SELECT cdgfam, dscfam FROM familias  WHERE cdgfam = $cdgfam";
	$resultadofam=$mysqli->query($queryfam);
	$rowfam = $resultadofam->fetch_assoc();
	$dscfam   = $rowfam["dscfam"];

	$querylin = "SELECT cdglin, dsclin FROM lineas WHERE cdgfam = $cdgfam AND cdglin = $cdglin ";
	$resultadolin=$mysqli->query($querylin);
	$rowlin = $resultadolin->fetch_assoc();
	$dsclin   = $rowlin["dsclin"];
							
							$costo_real= $impart +  $cosart;
							
							$cantidad_existencia_total = $exiact * $costo_real;
				
$consulta2 = "SELECT SUM(cantidad) as cantidad  FROM  pedidos_lineas WHERE codigo = '$cdgbar'   AND ".$where. "  GROUP BY codigo";

					if ($resultado2 = $mysqli->query($consulta2)) 
					$row = $resultado2->fetch_assoc();
					$cantidad=$row["cantidad"];	
		
			 
	                $costo_real_total = $cantidad  *  $costo_real ;
?>  

 <tr>
   <td></td>
   <td><div align="left"><?php echo "COD.".$cdgbar; ?></div></td>
	<td><?php echo $dscart; ?></td>
	<td><?php echo $dscfam; ?></td>
	<td><?php echo $dsclin; ?></td>


	
								<td width="4%"><div align="right"><?php echo number_format($exiact,2)  ?></div></td>	
								<td width="10%"><div align="right"><?php echo number_format($cantidad_existencia_total,2)  ?></div></td>	
								<td width="4%"><div align="right"><?php echo number_format($cantidad,2)  ?></div></td>	
								<td width="10%"><div align="right"><?php echo number_format($costo_real_total,2)  ?></div></td>
                 
 </tr> 
  <?php
  
    $costo_total_existencia = $cantidad_existencia_total +  $costo_total_existencia ;
    $costo_total = $costo_real_total +  $costo_total ;
}}


  ?>
</table>

<table>

 <tr>
   <td></td>
   <td></td>
   <td></td>
   <td colspan="2"></td>
   <td colspan="2">&nbsp;</td>
   <td>&nbsp;</td>
 </tr>
 <tr>
   <td width="6"></td>
   <td width="6"></td>
   <td width="6"></td>
    <td width="6"></td>
	<td width="365"><div align="right">TOTAL</div></td>
	<td width="57">&nbsp;</td>
	<td width="57"><div align="right"><?php echo number_format($costo_total_existencia,2); ?></div></td>
	<td width="61">&nbsp;</td>
	<td width="61"><div align="right"><?php echo number_format($costo_total,2); ?></div></td>
 </tr> 
</table>
</body>
</html>