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

$con_tasa=mysqli_query($con,"select * from tasa where id_tasa='1'");
while ($dato_tasa=mysqli_fetch_array($con_tasa))
{
	$id_tasa=$dato_tasa['id_tasa'];
	$tasa_actual=$dato_tasa['tasa'];
	$ult_act=$dato_tasa['fecha'];
}
$dato = $_POST['dato'];		
$tienda=$_SESSION['estado'];
$sTable = " ";
$sWhere = " ";
$cond="Liquidar";
$credito="SI";



if($tienda=="")
{
	if($_POST['dato']!="") 
	{
		$cliente=mysqli_query($con,"select * from clientes where nombre_cliente like '%$dato%'");
		$res_cliente=mysqli_fetch_array($cliente);
		$id_cli=$res_cliente['id_cliente'];
	
		$sTable.= "facturas ";
		$sWhere.= "WHERE (id_cliente = '$id_cli') AND estado_factura='3' AND (credito='$credito') ORDER BY id_factura ASC";
	}
	else
	{
		$sTable.= "facturas ";
		$sWhere.= "WHERE estado_factura='3' AND (credito='$credito')";	
	}
}
else
{
	if($_POST['dato']!="") 
	{
		$cliente=mysqli_query($con,"select * from clientes where nombre_cliente like '%$dato%'");
		$res_cliente=mysqli_fetch_array($cliente);
		$id_cli=$res_cliente['id_cliente'];
	
		$sTable.= "facturas ";
		$sWhere.= "WHERE (id_cliente = '$id_cli') AND estado_factura='1' AND (credito='$credito') AND almacen='$tienda' ORDER BY id_factura ASC";
	}
	else
	{
		$sTable.= "facturas ";
		$sWhere.= "WHERE estado_factura='3' AND (credito='$credito') AND almacen='$tienda'";	
	}
}

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");
$row= mysqli_fetch_array($count_query);
$numrows = $row['numrows'];
$total_pages = ceil($numrows/$per_page);
$reload = './reportes.php';
//main query to fetch the data
$sql="SELECT * FROM  $sTable $sWhere LIMIT $offset,$per_page";
$query = mysqli_query($con, $sql);

?>
    		<table class="table-responsive" align="center">
			  <table class="table">
                  <tr>
              		<td colspan="8" class="text-right">
     					<a href="imp_ctas_cobrar.php?dato=<?php echo $dato;?>"><button type="button" class="btn btn-info"><span class="glyphicon glyphicon-print" ></span> DESCARGAR PDF</button></a>
                     </td>
                </tr>
				<tr  class="info" style="font-size:12px">
					<td>#</strong></td> 
					<th>TIENDA (ESTADO)</th>
    				<th>RAZON SOCIAL</th>
    				<th>FECHA FACTURA</th>
					<th>NUMERO FACTURA</th>
					<th>MONTO TOTAL</th>
					<th>CONDICION</th>
					<th>ACCION</th>
				</tr>
           	<?php
				$item=0;
				$total=0;
		if($numrows>0)
			{
				while ($dato=mysqli_fetch_array($query))
				{
					$cli=$dato['id_cliente'];
					$con_cliente = mysql_query("select * from clientes where id_cliente='$cli'");
					$da=mysql_fetch_array($con_cliente);
					$tipo_cliente=$da['organizacion_cliente'];
					$estado_cliente=$da['estado_cliente'];
					$dir_cliente=$da['direccion_cliente'];
					$nom_cliente=$da['nombre_cliente'];
					$fecha=$dato['fecha_factura'];
					$id_factura=$dato['id_factura'];
					$almacen=$dato['almacen'];
					$num=$dato['numero_factura'];
					$condicion_factura=$dato['condiciones'];
					$estado_factura=$dato['estado_factura'];
					$cre=$dato['credito'];
					
   					//$base=$dato['total_venta']/(1+(TAX/100));
					$base=$dato['total_venta'];
					$basef=number_format($base,2,",", ".");
					//$iva=($base*TAX)/100;
					//$ivaf=number_format($iva,2,",", ".");
					$por=$dato['total_venta'];
					$porf=number_format($por,2,",", ".");
					$id=$dato['id_factura'];
					$item=$item+1;
					
					if($condicion_factura=="Liquidar")
					{
						if ($estado_factura==1)
						{
							if ($cre=="SI")
							{
							$text_estado="Credito Autorizado";$label_class='label-primary';
							}
							else
							{
							$text_estado="Pendiente";$label_class='label-success';
							}
						}
						if ($estado_factura==3)
						{
							$text_estado="Financiamiento";$label_class='label-danger';
							
						}
					}
					
					?>
				    <tr style="font-size:12px">
    					<td><?php echo $item;?></td>
						<td><?php echo $almacen;?></td>
    					<td><?php echo $nom_cliente;?></td>
    					<td><?php echo $fecha;?></td>
    					<td><?php echo $num;?></td>
    					<td>$<?php echo $porf;?></td>
						<td style="font-size:14px;"><span class="label <?php echo $label_class;?>"><?php echo $text_estado; ?></span></td>
    					<td>
						<?php if($_SESSION['rol']=="ventas" || $_SESSION['rol']=="pagos"|| $_SESSION['rol']=="gerencia" || $_SESSION['rol']=="supervisor")
							{ 
							?>	
								<a href="procesar_cobro.php?id_factura=<?php echo $id_factura;?>" class='btn btn-default' title='Procesar Cobro'><i class="glyphicon glyphicon-credit-card"></i></a>
  							<?php
							}
							?>
							<a href="#" class='btn btn-default' title='Ver Contrato' onclick="imprimir_contrato('<?php echo $id_factura;?>');"><i class="glyphicon glyphicon-file"></i></a>
							<a href="#" class='btn btn-default' title='Tabla de Amortizacion' onclick="imprimir_tabla('<?php echo $id_factura;?>');"><i class="glyphicon glyphicon-indent-left"></i></a>
						</td>
    			   </tr>
                   <?php
				   $total=$total+$por;
				   $totalf=number_format($total,2,",", ".");
				}
				?>
           		<tr>
					<td></td>
					<td></td>
					<td></td>
					<td></td>
					<th>Monto Por Cobrar</th>
					<th>$<?php echo $totalf;?></th>
                    <td></td>
				</tr>
				<?php
			}
			else
			{
			?>
				<tr>
				<td colspan="7">No se encontraron resultados</td>
			</tr>
            <?php 
			} 
			?>
            	<tr>
				
					<td colspan=7>
                    	<span class="pull-right">
						<?php
					 		echo paginate($reload, $page, $total_pages, $adjacents);
						?>
                        </span>
                    </td>
				</tr>
			</table>
