<?php 	
	session_start();	
	include 'conexion.php';

$id_empresa = $_SESSION["s_empresa"];
$nombre = $_SESSION["s_nombre"];
$usuario = $_SESSION["s_usuario"];
$camposSelect = "Select f.id_empresa,f.anio Anio, f.mes Mes, f.cant_empleados Empleados, f.imp_art_100 'Articulo 100',f.imp_art_101 'Articulo 101', f.imp_otros Otros,sum( imp_art_100 + f.imp_art_101 + f.imp_otros) Total, date_format(f.fecha,'%d-%m-%Y')   'Emision' ,f.bancodepo Banco ,f.id_tipo_formulario,CASE f.id_tipo_formulario  WHEN 1 THEN 'Original'  WHEN 2 THEN 'Rectificativa'  end tipo_form,";

$camposSelect .= "(Select count(*) from det_cobros d where d.id_empresa =  f.id_empresa and ";
$camposSelect .= "d.mes = f.mes and d.anio = f.anio and d.id_tipo_formulario = f.id_tipo_formulario ) estado";

$miTabla = " from com_formularios f";
$miWhere = " where id_empresa = " .$id_empresa;
$miGrupo = " group by id_empresa,anio,mes,id_tipo_formulario ";
//$miGrupo .= " order by id_empresa,anio,mes,id_tipo_formulario ";
$miGrupo .= " order by concat(anio, lpad(mes,2,'0')) desc ";

$sql = $camposSelect . $miTabla . $miWhere . $miGrupo;



 ?>	
<!DOCTYPE html>
<html lang="es">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" type="text/css" href="css/style.css">
	<link rel="stylesheet" href="../css/bootstrap.min.css">
	<script src="https://kit.fontawesome.com/d73eb638c2.js" crossorigin="anonymous"></script>
	
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
	<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/bootstrap-table.min.css">
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
	<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/bootstrap-table.min.js"></script>
	<script type="text/javascript" src="js/funciones.js"> </script>	
	<title>Consulta de Formularios</title>
</head>
<body>
	<section id="container" class="div-responsive">
	
		<h3> Empresa: <?php echo $nombre;?> </h3> 
		<h3>Lista de Formularios Emitidos</h3>
		
		<form action="boletas.php" method="get" class="form_busqueda">			
		</form>			

		<table data-toggle="table" data-click-to-select="true"  class="table-hover table-responsive" id="tableRoomList">
	      	<thead>
				<tr>
					
					<th>Año</th>
					<th>Mes</th>
					<th>Emp.</th>
					<th style="text-align: right">Art. 100</th>
					<th style="text-align: right">Art. 101</th>
					<th style="text-align: right">Otros</th>
					<th style="text-align: right">Total</th>
					<th>Emision</th>
					<th>Banco</th>
					<th>T.Formulario</th>
					
				</tr>
			</thead>
			<?php	
			    $cadena = "SELECT count(*) cantidad from com_formularios where id_empresa = ".$id_empresa;
				
				//$cadena = "SELECT count(*) cantidad from com_formularios where //id_empresa = ".$id_empresa." ORDER BY fecha desc";

				
				$sql_cantidad = mysqli_query($coneccion ,	$cadena);
				$registros = mysqli_fetch_array($sql_cantidad);
				$total_registros = $registros['cantidad'];
				
				// parametro a ajustar despues 
				$por_pagina = 12;

				if(empty($_GET['pagina'])){
					$pagina =1;
				}else{
					$pagina = $_GET['pagina'];
				}
				$desde =($pagina -1) * $por_pagina;
				$total_paginas = ceil($total_registros  / $por_pagina);

				$sql = $camposSelect . $miTabla . $miWhere . $miGrupo;
				$sql .=" LIMIT $desde, $por_pagina";	

				$query = mysqli_query($coneccion ,$sql);	

				$resul_q = mysqli_num_rows($query);	
				mysqli_close($coneccion);
				if($resul_q > 0) {
					while ($data = mysqli_fetch_array($query)){
			?>
						<tr> 

							<td><?php echo $data['Anio'] ?></td>
							<td><?php echo $data['Mes'] ?></td> 
							<td><?php echo $data['Empleados'] ?></td>
							<td align="right"><?php echo $data['Articulo 100'] ?></td> 
							<td align="right"><?php echo $data['Articulo 101'] ?></td> 
							<td align="right"><?php echo $data['Otros'] ?></td> 
							<td align="right"><?php echo $data['Total'] ?></td> 
							<td><?php echo $data['Emision'] ?></td>
							<td><?php echo $data['Banco'] ?></td>
							<td><?php echo $data['tipo_form'] ?></td>
							
							<td>
								<a class="btn btn-secondary" style="color:#1D1AEE;" href="php/gcomprobante.php?id_empresa=<?php echo $data['id_empresa'].'&mes='. $data['Mes'].'&anio='.$data['Anio'] .'&tipo_f='.$data['id_tipo_formulario'] ?>"> Ver.Comp.<i class="fa fa-search" aria-hidden="true"></i></a>
								
							</td>
						</tr>
			<?php
					}
				}	
			
			?>
			

		</table>
		<div class="paginador">

			<ul>
				<?php
				if($pagina != 1) 
				{
				?>
				<li><a href="?pagina=<?php echo 1;?>">|<</a></li>
				<li><a href="?pagina=<?php echo $pagina -1; ?>"><<</a></li>				
				<?php 
					}
					for ($i=1; $i <=$total_paginas ; $i++) { 
						if($i == $pagina){
							echo'<li class="pageSelected">'.$i.'</li>';
						}else{
							echo'<li><a href="?pagina='.$i.'">'.$i.'</a></li>';
						}						
					}
				?>
				<?php
				if($pagina != $total_paginas) 
				{
				?>
				<li><a href="?pagina=<?php echo $pagina + 1; ?>">>></a></li>
				<li><a href="?pagina=<?php echo $total_paginas ;?>">>|</a></li>
				<?php 
					}
				?>
			</ul>
			
		</div>
		<div> 
			<a href="menu.php" target="_self"><img src="images/regresar2.png" width="150"  height="50" border="0"></a>
		</div>

	</section>
	

</body>
</html>