
<?php

	include('is_logged.php');//Archivo verifica que el usario que intenta acceder a la URL esta logueado
	/* Connect To Database*/
	require_once ("../config/db.php");//Contiene las variables de configuracion para conectar a la base de datos
	require_once ("../config/conexion.php");//Contiene funcion que conecta a la base de datos
	
	
	$action = (isset($_REQUEST['action'])&& $_REQUEST['action'] !=NULL)?$_REQUEST['action']:'';
	if (isset($_GET['id']))
	{
		$numero_proforma=intval($_GET['id']);
		$consulta=mysqli_query($con,"select * from proformas where numero_proforma='$numero_proforma'");
		$dato=mysqli_fetch_array($consulta);
		$fecha_prof=$dato['fecha_proforma'];
		$hoy=date("Y-m-d");
		
		$consulta2=mysqli_query($con,"select * from detalle_proforma where numero_proforma='$numero_proforma'");
		while($dato2=mysqli_fetch_array($consulta2)){
		$id_pro=$dato2['id_producto'];
		$cant=$dato2['cantidad'];
		$precio=$dato2['precio_venta'];

		$consulta4=mysqli_query($con,"select * from facturas where numero_proforma='$numero_proforma'");
		while($dato4=mysqli_fetch_array($consulta4))
		$numero_factura=$dato2['numero_factura'];
	}
		
		
		$del1="delete from proformas where numero_proforma='".$numero_proforma."'";
		$del2="delete from detalle_proforma where numero_proforma='".$numero_proforma."'";
		if ($delete1=mysqli_query($con,$del1) and $delete2=mysqli_query($con,$del2)){
			?>
			<div class="alert alert-success alert-dismissible" role="alert">
			  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
			  <strong>Aviso!</strong> Datos eliminados exitosamente
			</div>
			<?php 
		}else {
			?>
			<div class="alert alert-danger alert-dismissible" role="alert">
			  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
			  <strong>Error!</strong> No se puedo eliminar los datos
			</div>
            
            
            
			<?php
			
		}
	}
	if($action == 'ajax'){
		// escaping, additionally removing everything that could be (html/javascript-) code
         $q = mysqli_real_escape_string($con,(strip_tags($_REQUEST['q'], ENT_QUOTES)));
		 $sTable = "proformas, clientes, users";
		 $sWhere = "";
		 $sWhere.=" WHERE proformas.id_cliente=clientes.id_cliente and proformas.id_vendedor=users.user_id";
		if ( $_GET['q'] != "" )
		{
		$sWhere.= " and  (clientes.nombre_cliente like '%$q%' or proformas.numero_proforma like '%$q%')";
			
		}
		
		$sWhere.=" order by proformas.id_proforma desc";
		include 'pagination.php'; //include pagination file
		//pagination variables
		$page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
		$per_page = 10; //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");
		$row= mysqli_fetch_array($count_query);
		$numrows = $row['numrows'];
		$total_pages = ceil($numrows/$per_page);
		$reload = './proformas.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
		if ($numrows>0){
			echo mysqli_error($con);
			?>
       
			<div class="table-responsive">
            

			  <table class="table">
              <tr>
              <td colspan="8" class='text-right'>
               <a href="imp_proforma.php?q=<?php echo $q?>"><button type='button' class="btn btn-info"><span class="glyphicon glyphicon-print" ></span> Descargar PDF</button></a>
              </td>
              </tr>
				<tr  class="warning">
					<th>#</th>
					<th>Fecha</th>
					<th>Usuario</th>
					<th>Tienda</th>
					<th>Cliente</th>
					<th>Estado</th>
					<th class='text-right'>Total</th>
					<th class='text-right'>Acciones</th>
					
				</tr>
				<?php
				while ($row=mysqli_fetch_array($query)){
						$id_proforma=$row['id_proforma'];
						$numero_proforma=$row['numero_proforma'];
						$vendedor=$row['id_vendedor'];
						$tienda=$row['almacen'];
						$fecha=date("d/m/Y", strtotime($row['fecha_proforma']));
						$nombre_cliente=$row['nombre_cliente'];
						$telefono_cliente=$row['telefono_cliente'];
						$email_cliente=$row['email_cliente'];
						$nombre_vendedor=$row['firstname']." ".$row['lastname'];
						$payment_id=$row['payment_id'];
						$estado_proforma=$row['estado_proforma'];
						if ($estado_proforma==1){$text_estado="Vigente";$label_class='label-success';}
						else{$text_estado="Procesada";$label_class='label-warning';}
						$total_venta=$row['total_venta'];
						
						$query_tienda=mysqli_query($con, "select * from tiendas where id_tienda='$tienda'");
					while ($row_tienda=mysqli_fetch_array($query_tienda))
					{
						$tienda=$row_tienda['nombre'];
						
					}
					
		
		$query2=mysqli_query($con, "select * from tasa where id_tasa='1'");
	while ($row2=mysqli_fetch_array($query2)){
						$id_tasa=$row2['id_tasa'];
						$tasa_actual=$row2['tasa'];
						$ult_act=$row2['fecha'];
	}	
		$total_ventaf=number_format($total_venta,2,",", ".");
		$dolar_a_bolivar=$total_venta*$tasa_actual;
		$dolar_a_bolivarf=number_format($dolar_a_bolivar,2,",", ".");
			
		$consulta_fact=mysqli_query($con,"select * from facturas where numero_proforma='$numero_proforma'");
		$dato_fact=mysqli_fetch_array($consulta_fact);
			$estado_fact=$dato_fact['estado_factura'];
			$numero_fact=$dato_fact['numero_factura'];
			
			$consulta_despacho=mysqli_query($con,"select * from despachos where factura='$numero_fact'");
			
					?>
					<tr>
						<td><?php echo $numero_proforma; ?></td>
						<td><?php echo $fecha; ?></td>
						<td><?php echo $nombre_vendedor; ?></td>
						<td><?php echo $tienda; ?></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><span class="label <?php echo $label_class;?>"><?php echo $text_estado; ?></span></td>
						<td class='text-right'>$. <?php echo $total_ventaf; ?> / Bsf. <?php echo $dolar_a_bolivarf; ?></td>					
					
                    <td class="text-right">
                    <?php if($estado_proforma==1 && $payment_id==""){?>
                    <a href="ajax/procesar_factura2.php?id_proforma=<?php echo $id_proforma;?>" class='btn btn-default' title='Enviar a Facturacion' ><i class="glyphicon glyphicon-credit-card"></i></a>
                    
                    <a href="editar_proforma.php?id_proforma=<?php echo $id_proforma;?>" class='btn btn-default' title='Editar' ><i class="glyphicon glyphicon-edit"></i></a>
                    <?php } ?>
                    <a href="#" class='btn btn-default' title='Ver Cotizacion' onclick="imprimir_proforma('<?php echo $id_proforma;?>');"><i class="glyphicon glyphicon-download"></i></a>
                    
                    <?php if($estado_proforma=="1"){?>
                    <a href="#" class='btn btn-default' title='Anular Cotizaicon' onclick="eliminar('<?php echo $numero_proforma; ?>')"><i class="glyphicon glyphicon-trash"></i> </a>
                    <?php } ?>
					</td>
						
					</tr>
					<?php
				}
				?>
				<tr>
					<td colspan=7><span class="pull-right"><?php
					 echo paginate($reload, $page, $total_pages, $adjacents);
					?></span></td>
				</tr>
			  </table>
			</div>
			<?php
		}
	}
?>