<?php

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=Reporte_Facturacion.xls");

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

?>
<!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>COSTO 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</strong></div></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>COSTO</strong></div></td>
	<td width="25%"><div align="center"><strong>IMPUESTO</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>
  </tr>
  
<?PHP
$cdgfam  = 000;
$cdgfam2 = 000;		
$monto_impuesto = 0 ;	
$monto_costo    = 0 ;


	
 $consulta= "select CDGART, CDGBAR, DSCART, sum(cantom) AS exiact, cosart, COB_IV, IVPORC  from toma_fisica  WHERE  cantom > 0 GROUP BY cdgbar ORDER BY   dscart";
	if ($resultado = $mysqli->query($consulta)) 

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

	$codigo      = $res["CDGART"];
	$cdgbar      = $res["CDGBAR"];
	$cdgbar2     = "Cod. ".$cdgbar;
	$dscart      = $res["DSCART"];
	$costo       = $res["cosart"];
	$existencia  = $res["exiact"];
	$exiact      = number_format ($existencia, 2);
	$cosart      = number_format ( $costo, 2);
	$cdgfam      = sprintf("%03d",$cdgfam); 
	$cob_iv      = $res["COB_IV"];
	$ivporc      = $res["IVPORC"];
	
	if ($cob_iv==1)	{	
	
	$impart = $costo * (( $ivporc/100 )+ 1)  ;
	$impart = $impart - $costo;
	
	}else {	
	
	$impart = 0;
	
	}
	
	$costo_total =  $costo *  $existencia; 
	$impuesto_total =  $impart *  $existencia; 
	$subtotal = $costo_total  +  $impuesto_total;
	
	$monto_impuesto= $monto_impuesto + $impuesto_total ;	
	$monto_costo = $monto_costo + $costo_total ;
	 
	
?>  

 <tr>
   <td></td>
   <td><div align="left"><?php echo $codigo; ?></div></td>
	<td><?php echo $dscart; ?></td>
	<td><div align="center"><?php echo $exiact; ?></div></td>
	<td><div align="right"><?php echo  $cosart; ?></div></td>  
	<td><div align="right"><?php echo number_format($impart,2); ?></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> 

                 
 </tr> 
  <?php
  
}
$monto_total = $monto_impuesto + $monto_costo;
  ?>
</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"><div align="right"><?php echo number_format($monto_costo,2); ?></div></td> 
	<td width="124"><div align="right"><?php echo number_format($monto_impuesto,2); ?></div></td> 
	<td width="124"><div align="right"><?php echo number_format($monto_total,2); ?></div></td>                    
 </tr> 
</table>
</body>
</html>