<?php
include('is_logged.php');
require_once ("../config/db.php");
require_once ("../config/conexion.php");

$tienda=$_SESSION['estado'];
$query=mysqli_query($con, "select * from tasa where id_tasa='1'");
while ($row=mysqli_fetch_array($query))
{
	$id_tasa=$row['id_tasa'];
	$tasa_actual=$row['tasa'];
	$ult_act=$row['fecha'];
}
$action = (isset($_REQUEST['action'])&& $_REQUEST['action'] !=NULL)?$_REQUEST['action']:'';
if($action == 'ajax')
{
	$q = mysqli_real_escape_string($con,(strip_tags($_REQUEST['q'], ENT_QUOTES)));
	$est = $_REQUEST['est'];
	$lugar = $_REQUEST['lugar'];
	$sTable = "fact_anuladas, clientes, users";
	
	
	if($tienda!="")
	{
		
			$sWhere=" WHERE fact_anuladas.almacen='".$tienda."' and fact_anuladas.id_cliente=clientes.id_cliente and fact_anuladas.id_vendedor=users.user_id";
			if ($_GET['q']!="")
			{
				$sWhere.=" and  (clientes.nombre_cliente like '%".$q."%' or fact_anuladas.numero_factura like '%".$q."%' or clientes.rif_cliente like '%".$q."%')";
			}
		
	}
	if($tienda=="")
	{
		
			$sWhere=" WHERE fact_anuladas.id_cliente=clientes.id_cliente and fact_anuladas.id_vendedor=users.user_id";
			if ($_GET['q']!="" && $_GET['est']=="")
			{
				$sWhere.=" and (clientes.nombre_cliente like '%$q%' or fact_anuladas.numero_factura like '%$q%' or clientes.rif_cliente like '%$q%')";
			}
			if ( $_GET['q']!="" && $_GET['est']!="")
			{
				$sWhere.=" and fact_anuladas.almacen='$est' and  (clientes.nombre_cliente like '%$q%' or fact_anuladas.numero_factura like '%$q%' or clientes.rif_cliente like '%$q%')";	
				
			}
			if ( $_GET['q']=="" && $_GET['est']!="")
			{
				
					$sWhere.=" and fact_anuladas.almacen='$est'";
				
			}
		
	}
	$sWhere.=" order by fact_anuladas.id_fact desc";
	include 'pagination.php'; //include pagination file
	//pagination variables
	$page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
	$per_page = 30; //how much records you want to show
	$adjacents  = 4; //gap between pages after number of adjacents
	$offset = ($page - 1) * $per_page;
	//Count the total number of row in your table*/
	$count_query   = mysqli_query($con, "SELECT count(*) AS numrows FROM $sTable  $sWhere") or die(mysqli_error($con));
	$row= mysqli_fetch_array($count_query);
	$numrows = $row['numrows'];
	$total_pages = ceil($numrows/$per_page);
	$reload = './fact_anuladas.php';
	//main query to fetch the data
	$sql="SELECT * FROM  $sTable $sWhere LIMIT $offset,$per_page";
	$query = mysqli_query($con, $sql);
	//loop through fetched data
	?>
    <div class="table-responsive">
       	<table class="table" style="font-size:12px;">
			<tr  class="warning">
				<th>Tienda</th>
				<th>Factura</th>
			    <th>Fecha</th>
                <th>Fecha Anulada</th>
                <th>Vendedor</th>
				<th>Cliente</th>
				<th>Cedula o Rif</th>
				<th class='text-right'>Total</th>
				<th class='text-right'>Acciones</th>
			</tr>
           
			<?php 
			if ($numrows>0)
			{
				while ($row=mysqli_fetch_array($query))
				{	
					$id_factura=$row['id_fact'];
					$numero_factura=$row['numero_factura'];
					$almacen=$row['almacen'];
					$tasa=$row['tasa'];
					$fecha=date("d/m/Y", strtotime($row['fecha_factura']));
					$fecha_anulada=date("d/m/Y", strtotime($row['fecha_anulada']));
					$nombre_cliente=$row['nombre_cliente'];
					$nombre_vendedor=$row['firstname']." ".$row['lastname'];
					$rif_cliente=$row['rif_cliente'];
					
					$total_venta=$row['total_venta'];
					$total_ventaf=number_format($total_venta,1,",", ".");
					
					$dolar_a_bolivar=$total_venta*$tasa;
					$dolar_a_bolivarf=number_format($dolar_a_bolivar,1,",", ".");
					
					$query_tienda=mysqli_query($con, "select * from tiendas where id_tienda='$almacen'");
					while ($row_tienda=mysqli_fetch_array($query_tienda))
					{
						$almacen=$row_tienda['nombre'];
						
					}
					?>
					<input type="hidden" value="<?php echo $numero_factura;?>" id="numero_factura<?php echo $id_factura;?>">
		
        			<tr>
						<td><?php echo $almacen; ?></td>
						<td><?php echo $numero_factura; ?></td>
           			 	<td><?php echo $fecha; ?></td>
						<td><?php echo $fecha_anulada; ?></td>
           		 		<td><?php echo $nombre_vendedor; ?></td>
						<td><a href="#" data-toggle="tooltip" data-placement="top" title="<i class='glyphicon glyphicon-phone'></i> <?php echo $telefono_cliente;?><br><i class='glyphicon glyphicon-envelope'></i>  <?php echo $email_cliente;?>" ><?php echo $nombre_cliente;?></a></td>
						<td><?php echo $rif_cliente; ?></td>
						<td class='text-right'>$.<?php echo $total_ventaf; ?> /Bsf.<?php echo $dolar_a_bolivarf ?></td>		
						<td class="text-right"><a href="ver_venta_anulada.php?id_factura=<?php echo $id_factura;?>" class='btn btn-default' title='Ver Venta Anulada'><i class="glyphicon glyphicon-eye-open"></i></a></td>
                	</tr>
	            <?php
				}
				?>
			<tr>
    	    	<td colspan=9><span class="pull-right"><?php echo paginate($reload, $page, $total_pages, $adjacents);?></span></td>
			</tr>
		</table>
	<?php
	}	
	?>
</div>
<?php
}
?>