<?php

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=Reporte_inventario_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"];
	   
	   $monto_impuesto  = 0 ;	
	   $monto_costo     = 0 ;
	   $total           = 0 ;	


?>
<!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>LISTA DE INVENTARIO</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 COSTO DE INVENTARIO POR FAMILIAS</strong></div></td>
 <td width="25%"></td>

  </tr>
     <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="16%"><div align="center"><strong>EXISTENCIA</strong></div></td>
    <td width="25%"><div align="center"><strong></strong></div></td>
	<td width="25%"><div align="center"><strong></strong></div></td>
	<td width="25%"><div align="center"><strong>TOTAL COSTO</strong></div></td>
	<td width="25%"><div align="center"><strong>TOTAL IMPUESTO</strong></div></td>
	<td width="25%"><div align="center"><strong>TOTAL</strong></div></td>
	<td width="25%"></td>

  </tr>
  
<?PHP

$monto_impuesto_venta="0";
$monto_impuesto="0";

	$subgratot ="0";
	$impgratot ="0";
	$totgratot ="0";
	$subexetot ="0";
	$impexetot ="0";
	$totexetot ="0";

$consulta= "select  CDGART,CDGBAR,DSCART, SUM(cosart * EXIACT) AS costo_total , cosart ,DESART,PRE001,CDGFAM,COB_IV, ivporc, SUM(EXIACT) AS EXIACT   from web_1001  WHERE tipart  <> 2 AND exiact > 0  AND COB_IV = 1  GROUP BY CDGFAM";
	if ($resultado = $mysqli->query($consulta)) 

  	while ($res = $resultado->fetch_assoc()) {		

	$codigo  = $res["CDGART"];
	$cdgbar  = $res["CDGBAR"];
	$cdgbar2 = "Cod. ".$cdgbar;
	$dscart  = $res["DSCART"];
	$costo_total = $res["costo_total"];
	$desart   = $res["DESART"];
	$pre001     = $res["PRE001"];
	$PRECIO     = $res["PRE001"];
	$existencia  = $res["EXIACT"];
	$ivporc   = $res["ivporc"];
	$exiact  = number_format ($existencia, 2);
	//$costo_total  = number_format ( $costo_total, 2);
	$cdgfam  = $res["CDGFAM"];
	$cdgfam  = sprintf("%03d",$cdgfam); 
	$cob_iv  = $res["COB_IV"];
	//$ivporc  = $res["ivporc"]/100+1;

	///////////////////////////////////////   CALCULA COSTOS//////////////////////////////////////////
	
     $ivporc=$ivporc/100+1;
	 
	 $impart = $costo_total * $ivporc ;  
	$impuesto_total = $impart - $costo_total;
	

	$subtotal = $costo_total  +  $impuesto_total;
	
//	$monto_impuesto= $monto_impuesto + $impuesto_total ;	
//	$monto_costo = $monto_costo + $costo_total ;
	

	$consulta5 = "SELECT  * FROM familias WHERE CDGFAM = $cdgfam";
    $resultado5 = $mysqli->query($consulta5);
    $row5 = $resultado5->fetch_array();
    $dscfam = $row5["dscfam"];
	
	$subgratot = $costo_total + $subgratot;
	$impgratot = $impuesto_total + $impgratot;
	$totgratot = $subtotal + $totgratot;
	
?>  

 <tr>
   <td></td>
   <td><div align="left"><?php echo $cdgfam; ?></div></td>
	<td><?php echo $dscfam; ?></td>
	<td><div align="center"><?php echo $exiact; ?></div></td>
	<td><div align="right"></div></td>  
	<td><div align="right"></div></td>  
	<td><div align="right"><?php echo number_format($costo_total ,2); ?></div></td> 
		<td><div align="right"><?php echo number_format($impuesto_total,2); ?></div></td> 
	<td><div align="right"><?php echo number_format($subtotal,2); ?></div></td> 
	
		<td></td> 
	             
 </tr> 
  <?php

}

  
  //////////////////////////////////////////////////////   si la familia es exenta  ////////////////////////////
  
  $consulta2= "select  CDGART,CDGBAR,DSCART, SUM(cosart * EXIACT) AS costo_total , cosart ,DESART,PRE001,CDGFAM,COB_IV, SUM(EXIACT) AS EXIACT   from web_1001  WHERE tipart  <> 2 AND exiact > 0  AND COB_IV = 2  GROUP BY CDGFAM";
	if ($resultado2 = $mysqli->query($consulta2)) 

  	while ($res = $resultado2->fetch_assoc()) {		

	$codigo  = $res["CDGART"];
	$cdgbar  = $res["CDGBAR"];
	$cdgbar2 = "Cod. ".$cdgbar;
	$dscart  = $res["DSCART"];
	$costo_total = $res["costo_total"];
	$desart   = $res["DESART"];
	$pre001     = $res["PRE001"];
	$PRECIO     = $res["PRE001"];
	$existencia  = $res["EXIACT"];
	$existencia  = $res["EXIACT"];
	$exiact  = number_format ($existencia, 2);
	//$costo_total  = number_format ( $costo_total, 2);
	$cdgfam  = $res["CDGFAM"];
	$cdgfam  = sprintf("%03d",$cdgfam); 
	$cob_iv  = $res["COB_IV"];

	///////////////////////////////////////   CALCULA COSTOS//////////////////////////////////////////
	

	$impart = 0 ;
	$impuesto_total = 0;
	

	$subtotal = $costo_total  +  $impuesto_total;
	
	$monto_impuesto= 0 ;	
	$monto_costo = $monto_costo + $costo_total ;
	

	$consulta5 = "SELECT  * FROM familias WHERE CDGFAM = $cdgfam";
    $resultado5 = $mysqli->query($consulta5);
    $row5 = $resultado5->fetch_array();
    $dscfam = $row5["dscfam"];
	
	$subexetot = $costo_total + $subexetot;
	$totexetot = $subtotal + $totexetot;
	
	
?>  

 <tr>
   <td></td>
   <td><div align="left"><?php echo $cdgfam; ?></div></td>
	<td><?php echo $dscfam."**"; ?></td>
	<td><div align="center"><?php echo $exiact; ?></div></td>
	<td><div align="right"></div></td>  
	<td><div align="right"></div></td>  
	<td><div align="right"><?php echo number_format($costo_total ,2); ?></div></td> 
		<td><div align="right"><?php echo number_format($impuesto_total,2); ?></div></td> 
	<td><div align="right"><?php echo number_format($subtotal,2); ?></div></td> 
	
		<td></td> 
	             
 </tr> 
  <?php

}
  ?>
  
  
    <?php

	$subtot = $subgratot + $subexetot;
	$imptot = $impgratot;
	$tottot = $totgratot + $totexetot;


  ?>
  
  
  
  
</table>

<table>

 <tr>
   <td></td>
   <td></td>
   <td></td>
   <td></td>
   <td>&nbsp;</td>
   <td>&nbsp;</td>
 </tr>
 <tr>
   <td width="6"></td>
   <td width="6"></td>
	<td width="6"></td>
	<td width="365"></td>
	<td width="116">TOTAL</td>  
	<td width="124"></td> 
	<td width="124"><?php echo number_format($subtot,2); ?></div></div></td> 
	<td width="124"><?php echo number_format($imptot,2); ?></div></div></td> 
	<td width="124"><?php echo number_format($tottot,2); ?></div></div></td>    
	<td width="6"></td>


	<td width="124"></td> 
	<td width="124"></td> 
	<td width="124"></td> 
	<td width="124"></td>   
		<td width="124"></td>   
			<td width="124"></td> 
	<td width="124"></td>                  
 </tr> 
 
  <tr>
   <td width="6"></td>
   <td width="6">** Familias Exentas</td>
	<td width="6"></td>
	<td width="365"></td>
	<td width="116"></td>  
	<td width="124"></td> 
	<td width="124"></div></td> 
	<td width="124"></div></td> 
	<td width="124"></div></td>    
	<td width="6"></td>


	<td width="124"></td> 
	<td width="124"></td> 
	<td width="124"></td> 
	<td width="124"></td>   
		<td width="124"></td>   
			<td width="124"></td> 
	<td width="124"></td>                  
 </tr>
</table>
</body>
</html>