<?php
require_once ("libraries/inventory.php");//Contiene funcion que controla stock en el inventario
$tipo = 'excel';
$extension = '.xls';

header("Content-type: application/vnd.ms-$tipo; ");
header("Content-Disposition: attachment; filename=reporte_inventario$extension");
header("Pragma: no-cache");
header("Expires: 0");

?>
<!DOCTYPE html>
<html>
  <head>
	<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  </head>
 <body> 
<h1>Reporte de inventario</h1>
<table>
    <thead>
        <tr>
            <th style="text-align:center;">Código </th>
            <th style="text-align:left;">Producto</th>
            <th style="text-align:left;">Categoría</th>
            <th style="text-align:center;">Existencias</th>
			<th style="text-align:right;">Costo</th>
			<th style="text-align:right;">Total</th>
            
        </tr>
    </thead>
	<?php
		
	$query = mysqli_real_escape_string($con,(strip_tags($_REQUEST['query'], ENT_QUOTES)));
	$product_code = mysqli_real_escape_string($con,(strip_tags($_REQUEST['product_code'], ENT_QUOTES)));
	$category_id=intval($_REQUEST['category_id']);
	
	$tables="products, categories";
	$campos="products.product_id, products.product_name, products.status, products.image_path, products. product_code, products.buying_price, categories.name";
	$sWhere="products.category_id=categories.id";
	$sWhere.=" and products.product_name LIKE '%".$query."%'";
	if (!empty($product_code)){
		$sWhere.=" and products.product_code LIKE '%".$product_code."%'";
	}
	if ($category_id>0){
		$sWhere.=" and products.category_id = '".$category_id."'";
	}
	$sWhere.=" order by products.product_name";
		
		$query = mysqli_query($con,"SELECT $campos FROM  $tables  where $sWhere ");
		$sumador=0;
		while($row = mysqli_fetch_array($query)){	          					
            $product_id=$row['product_id'];
			$product_code=$row['product_code'];
			$product_name=$row['product_name'];
			$category_name=$row['name'];
			$buying_price=$row['buying_price'];
			$buying_price=number_format($buying_price,$currency_format['precision_currency'],'.','');
			$total=$buying_price*get_stock($product_id);
			$total=number_format($total,$currency_format['precision_currency'],'.','');
			$sumador+=$total;
		?>	
			<tr>
					<td style="width: 15%;text-align:center"><?php echo $product_code;?></td>
					<td  style="width: 40%;"><?php echo $product_name;?></td>
					<td  style="width: 15%;"><?php echo $category_name;?></td>
					<td  style="width: 10%;text-align:center"><?php  echo get_stock($product_id);?></td>
					<td  style="width: 10%;text-align:right"><?php  echo number_format($buying_price,$currency_format['precision_currency'],$currency_format['decimal_separator'],$currency_format['thousand_separator']);?></td>
					<td  style="width: 10%;text-align:right"><?php  echo number_format($total,$currency_format['precision_currency'],$currency_format['decimal_separator'],$currency_format['thousand_separator']);?></td>
			</tr>
		<?php }?>
		<tr>
			<td colspan=5 style='text-align:right;'><strong>Total</strong></td>
			<td style='text-align:right;'><?php echo number_format($sumador,$currency_format['precision_currency'],$currency_format['decimal_separator'],$currency_format['thousand_separator']);?></td>
		</tr>
</table>   
</body>
</html>