<?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_compras$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 compras</h1>
<table>
    <thead>
        <tr>
            <th style="text-align:center;">Compra Nº </th>
            <th style="text-align:left;">Proveedor</th>
            <th style="text-align:left;">Fecha</th>
            <th style="text-align:right;">Neto</th>
			<th style="text-align:right;">IVA</th>
			<th style="text-align:right;">Total</th>
            
        </tr>
    </thead>
	<?php
		
	$daterange = mysqli_real_escape_string($con,(strip_tags($_REQUEST['daterange'], ENT_QUOTES)));
	$purchase_by=intval($_REQUEST['purchase_by']);
	$supplier_id=intval($_REQUEST['supplier_id']);
	
	$tables="purchases,  users";
	$campos="*";
	$sWhere="users.user_id=purchases.purchase_by";
	if ($purchase_by>0){
		$sWhere.=" and purchases.purchase_by = '".$purchase_by."'";
	}
	if ($supplier_id>0){
		$sWhere.=" and purchases.supplier_id = '".$supplier_id."'";
	}
	if (!empty($daterange)){
		list ($f_inicio,$f_final)=explode(" - ",$daterange);//Extrae la fecha inicial y la fecha final en formato espa?ol
		list ($dia_inicio,$mes_inicio,$anio_inicio)=explode("/",$f_inicio);//Extrae fecha inicial 
		$fecha_inicial="$anio_inicio-$mes_inicio-$dia_inicio 00:00:00";//Fecha inicial formato ingles
		list($dia_fin,$mes_fin,$anio_fin)=explode("/",$f_final);//Extrae la fecha final
		$fecha_final="$anio_fin-$mes_fin-$dia_fin 23:59:59";
		
		$sWhere .= " and purchases.purchase_date between '$fecha_inicial' and '$fecha_final' ";
	}
	$sWhere.=" order by purchases.purchase_id";
		
		$query = mysqli_query($con,"SELECT $campos FROM  $tables  where $sWhere ");
		$sumador_subtotal=0;$sumador_tax=0;$sumador_total=0;
		while($row = mysqli_fetch_array($query)){	          					
                $purchase_order_number=$row['purchase_order_number'];
				$supplier_id=$row['supplier_id'];
				$sql_supplier=mysqli_query($con,"select name from suppliers where id='".$supplier_id."'");
				$rw_supplier=mysqli_fetch_array($sql_supplier);
				$supplier_name=$rw_supplier['name'];
				$date_added=$row['purchase_date'];
				$user_fullname=$row['fullname'];
				$subtotal=$row['subtotal'];
				$tax=$row['tax'];
				$total=$row['total'];
				
				$sumador_subtotal+=$subtotal;
				$sumador_tax+=$tax;
				$sumador_total+=$total;
				
				
				list($date,$hora)=explode(" ",$date_added);
				list($Y,$m,$d)=explode("-",$date);
				$fecha=$d."-".$m."-".$Y;
		?>	
						<tr>                            
							
                            <td style="text-align:center"><?php echo $purchase_order_number;?></td>
                           	<td><?php echo $supplier_name;?></td>
							<td><?php echo $fecha;?></td>
							<td style="text-align:right"><?php echo number_format($subtotal,$currency_format['precision_currency'],$currency_format['decimal_separator'],$currency_format['thousand_separator']);?></td>
                            <td style="text-align:right"><?php echo number_format($tax,$currency_format['precision_currency'],$currency_format['decimal_separator'],$currency_format['thousand_separator']);?></td>				
							<td style="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=3 style='text-align:right;'><strong>Totales</strong></td>
			<td style='text-align:right;'><?php echo number_format($sumador_subtotal,$currency_format['precision_currency'],$currency_format['decimal_separator'],$currency_format['thousand_separator']);?></td>
			<td style='text-align:right;'><?php echo number_format($sumador_tax,$currency_format['precision_currency'],$currency_format['decimal_separator'],$currency_format['thousand_separator']);?></td>
			<td style='text-align:right;'><?php echo number_format($sumador_total,$currency_format['precision_currency'],$currency_format['decimal_separator'],$currency_format['thousand_separator']);?></td>
		</tr>
</table>   
</body>
</html>