<?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_ventas$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 ventas</h1>
<table>
    <thead>
        <tr>
           <th style="text-align:center;">Documento Nº</th>
            <th style="text-align:left;">Cliente</th>
            <th style="text-align:left;">Fecha</th>
            <th style="text-align:left;">Metodo pago</th>
            <th style="text-align:right;">Total</th>
            
        </tr>
    </thead>
	<?php
		
	$daterange = mysqli_real_escape_string($con,(strip_tags($_REQUEST['range'], ENT_QUOTES)));
	$sale_by=intval($_REQUEST['sale_by']);
	$customer_id=intval($_REQUEST['customer_id']);
	
	$tables="sales, users";
	$campos="*";
	$sWhere="users.user_id=sales.sale_by";
	if ($sale_by>0){
		$sWhere.=" and sales.sale_by = '".$sale_by."'";
	}
	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 sales.sale_date between '$fecha_inicial' and '$fecha_final' ";
	}
	if ($customer_id>0){
		$sWhere .= " and sales.customer_id = '$customer_id' ";
	}
	$sWhere.=" order by sales.sale_id";
		
		$query = mysqli_query($con,"SELECT $campos FROM  $tables  where $sWhere ");
		$sumador_total=0;
		while($row = mysqli_fetch_array($query)){	          					
                $sale_number=$row['sale_number'];
				$customer_id=$row['customer_id'];
				$payment=$row['payment_method'];
				$sql_customer=mysqli_query($con,"select name from customers where id='".$customer_id."'");
				$rw_customer=mysqli_fetch_array($sql_customer);
				$customer_name=$rw_customer['name'];
				$date_added=$row['sale_date'];
				list($date,$hora)=explode(" ",$date_added);
				list($Y,$m,$d)=explode("-",$date);
				$fecha=$d."-".$m."-".$Y;
				$total=$row['total'];
				$sumador_total+=$total;	
		?>	
						<tr>                            
							
                            <td style="text-align:center"><?php echo $sale_number;?></td>
                           	<td><?php echo $customer_name;?></td>
							<td><?php echo $fecha;?></td>
							<td><?php echo $payment;?></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_total,$currency_format['precision_currency'],$currency_format['decimal_separator'],$currency_format['thousand_separator']);?></td>
		</tr>
</table>  
</body>
</html> 