<?session_start();

//RESTRICCIÓN DE ACCESO A TRABAJADOR (ACCESO INDIVIDUAL) 
if (isset($_SESSION['tipousuario']))
		{if ($_SESSION['tipousuario']=='1')
		header ('location:menu_personal.php');
		}
		
if  (trim($_SESSION['nombre_validar'])=='') header ('location:index.php');
include ("fphp.php");
$enlace=cargar_conexion();
$consulta=mysqli_query($enlace,"select * from periodo where id='".$_SESSION['id_validar']."' order by nomina,periodo desc limit 1");
if (mysqli_num_rows($consulta)!=0){
	$row=mysqli_fetch_array($consulta);
	$per=mostrar_fecha($row['desde']).' al '.mostrar_fecha($row['hasta'])." (Nom:".$row['nomina'].")";
} else $per=''; 


////////////////////REGISTRO DE liquidaciones Procesados
if ($_POST['treporte']==1) {
 $query='';
 		//$consulta = "select * from personal where id='".$_SESSION['id_validar']."' and status<>'I' $query order by codigo";
		$consulta = "SELECT cedula, CONCAT(nombres,' ',nombres1,' ',apellidos,' ',apellidos1),
		fechai,
		fecha_retiro, 
		fecha_pago, 
		tipoe, 
		REPLACE(REPLACE(FORMAT(salar_integral, 2), ',', ''), '.', ',') AS salar_integral,
		REPLACE(REPLACE(FORMAT(sueldos_pagar, 2), ',', ''), '.', ',') AS sueldos_pagar,
		REPLACE(REPLACE(FORMAT(valor_ct_total, 2), ',', ''), '.', ',') AS valor_ct_total,
		REPLACE(REPLACE(FORMAT(valor_vaca_venc, 2), ',', ''), '.', ',') AS valor_vaca_venc,
		REPLACE(REPLACE(FORMAT(valor_bonovac_venc, 2), ',', ''), '.', ',') AS valor_bonovac_venc,
		REPLACE(REPLACE(FORMAT(bono_post_vaca, 2), ',', ''), '.', ',') AS bono_post_vaca,
		REPLACE(REPLACE(FORMAT(valor_dias_vac_fraccion, 2), ',', ''), '.', ',') AS valor_dias_vac_fraccion,
		REPLACE(REPLACE(FORMAT(valor_bonovac_fraccion, 2), ',', ''), '.', ',') AS valor_bonovac_fraccion,
		REPLACE(REPLACE(FORMAT(valor_utilid_pagar, 2), ',', ''), '.', ',') AS valor_utilid_pagar,
		REPLACE(REPLACE(FORMAT(prestaciones_pagar, 2), ',', ''), '.', ',') AS prestaciones_pagar,
		REPLACE(REPLACE(FORMAT(valor_indem, 2), ',', ''), '.', ',') AS valor_indem,
		ccon01, 
		REPLACE(REPLACE(FORMAT(ncon01, 2), ',', ''), '.', ',') AS ncon01,
		REPLACE(REPLACE(FORMAT(pago_fondo_ahor, 2), ',', ''), '.', ',') AS pago_fondo_ahor,
		REPLACE(REPLACE(FORMAT(intereses_prestac, 2), ',', ''), '.', ',') AS intereses_prestac,
		REPLACE(REPLACE(FORMAT(total_asig, 2), ',', ''), '.', ',') AS total_asig,
		
		REPLACE(REPLACE(FORMAT(adeuti, 2), ',', ''), '.', ',') AS adeuti,
		REPLACE(REPLACE(FORMAT(saldo_prestamo, 2), ',', ''), '.', ',') AS saldo_prestamo,
		REPLACE(REPLACE(FORMAT(fideicomiso, 2), ',', ''), '.', ',') AS fideicomiso,
		ccon02,
		REPLACE(REPLACE(FORMAT(ncon02, 2), ',', ''), '.', ',') AS ncon02,
		REPLACE(REPLACE(FORMAT(valor_sso, 2), ',', ''), '.', ',') AS valor_sso,
		REPLACE(REPLACE(FORMAT(valor_rpe, 2), ',', ''), '.', ',') AS valor_rpe,
		REPLACE(REPLACE(FORMAT(faov, 2), ',', ''), '.', ',') AS faov,
		REPLACE(REPLACE(FORMAT(inces, 2), ',', ''), '.', ',') AS inces,
		REPLACE(REPLACE(FORMAT(valor_islr, 2), ',', ''), '.', ',') AS valor_islr,
		
		REPLACE(REPLACE(FORMAT(sso_empresa, 2), ',', ''), '.', ',') AS sso_empresa,
		REPLACE(REPLACE(FORMAT(sso_empresa_cxp, 2), ',', ''), '.', ',') AS sso_empresa_cxp,
		
		REPLACE(REPLACE(FORMAT(rpe_empresa, 2), ',', ''), '.', ',') AS rpe_empresa,
		REPLACE(REPLACE(FORMAT(rpe_empresa_cxp, 2), ',', ''), '.', ',') AS rpe_empresa_cxp,
		
		REPLACE(REPLACE(FORMAT(aporte_emp_rpvh, 2), ',', ''), '.', ',') AS aporte_emp_rpvh,
		REPLACE(REPLACE(FORMAT(aporte_emp_rpvh_cxp, 2), ',', ''), '.', ',') AS aporte_emp_rpvh_cxp,

		REPLACE(REPLACE(FORMAT(inces_empresa, 2), ',', ''), '.', ',') AS inces_empresa,
		REPLACE(REPLACE(FORMAT(inces_empresa_cxp, 2), ',', ''), '.', ',') AS inces_empresa_cxp,
		
		REPLACE(REPLACE(FORMAT(reserva_utilidades, 2), ',', ''), '.', ',') AS reserva_utilidades,
		REPLACE(REPLACE(FORMAT(reserva_utilidades_cxp, 2), ',', ''), '.', ',') AS reserva_utilidades_cxp,
		
		REPLACE(REPLACE(FORMAT(total_deduccion, 2), ',', ''), '.', ',') AS total_deduccion,
		
		REPLACE(REPLACE(FORMAT(neto_pagar_liq, 2), ',', ''), '.', ',') AS neto_pagar_liq,
		estado_liquidacion,
		fecha_pago
		
		FROM liquidaciones
		WHERE liquidaciones.id=".$_SESSION['id_validar']." AND fecha_retiro>='".substr($_POST['per1'],0,10)."'  order by xxx";
		
		$resultado = mysqli_query($enlace,$consulta) or die('verificaci&oacute; del c&oacute;digo fall&oacute;: ' . mysqli_error($enlace));
		
		$cont=0;
		$data=array();
		$cabecera=array("Cedula",
		"Nombre y Apellidos Trabajador",
		"Fech_Ing",
		"Fech_Retiro",
		"Fecha_pago",
		"Motivo_retiro",
		"Salar_integ_dia",
		"Pgdo_Sueldos",
		"Pgdo_CT",
		"Pgdo_Vac_Venc",
		"Pgdo_BonVac_Venc",
		"Pgdo_BonPostVac",
		"Pgdo_Vac_fracc",
		"Pgdo_BonVac_fracc",
		"Pgdo_Utilid",
		"Pgdo_Prestac",
		"Pgdo_Indemn",
		"Otras_Asig",
		"Pgdo_Otras_Asig",
		"Pgdo_FondAhor",
		"Pgdo_Interes",
		"Total_Asig",
		
		"Desc_Util_Pagadas",
		"Desc_Prestamo",
		"Desc_Fideic",
		"Otras_Deduc",
		"Desc_OtrasDed",
		"SSO",
		"RPE",
		"RPVH",
		"INCES",
		"ISLR",
		
		"SSO_empresa",
		"SSO_empresa_cxp",
		
		"RPE_empresa",
		"RPE_empresa_cxp",
		
		"RPVH_empresa",
		"RPVH_empresa_cxp",
		
		"INCES_empresa",
		"INCES_empresa_cxp",
		
		"Rsrv_Util",
		"Rsrv_Util_cxp",
		
		"Total_Deducc",
		
		"Neto_Pgdo_Liquid",
		"Status_Liquid",
		"Fecha_Pago");
		$cabecera=implode(";",$cabecera);
		while ($row=mysqli_fetch_row($resultado)) {
		     $cont++;
			 $data[]=implode(";",$row);
		}
		$data=implode("\n",$data);
$arch="Liquidaciones_Procesadas.csv";		
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=".$arch);
header("Pragma: no-cache");
header("Expires: 0");
echo "Empresa: ".$_SESSION['empresa_validar']."\n";
echo " \n";
echo "Reporte de Liquidaciones Por Retiro Procesadas \n";
echo "\n";
//echo substr($_POST['per1'],19,10)."\n";
echo $cabecera."\n";
echo utf8_decode($data);		
		
 exit();
}


?>
<html>
	<head><link rel='stylesheet' type='text/css' href='styles.css' />
	</head>
	<body>	
		<?
		menu();
		?>
		<br><table border=0 cellpadding=0 cellspacing=0 width='100%'>
			<tr>
				<td  width='100%' align='left'><font size=2 color='black' face='verdana'>Empresa: <b><?echo $_SESSION['empresa_validar'];?></b>  - Per&iacute;odo: <b><font color=red><?echo $per;?></font></b>  -  Usuario: <b><?echo $_SESSION['nombre_validar'];?></b></font></td> 
			</tr>
		</table><hr>	
<?	echo "<tr align='center'>";	
	echo "<h1 align=center><font face='Verdana, Arial, Helvetica, sans-serif' size=4>Generar Reporte de Liquidaciones Procesadas</font></h1>";
	echo "</tr>";
	echo "<font face='verdana' size=2>";
	echo "
	<br><br><form name='entrada' action='".$_SERVER['PHP_SELF']."' target='blank' method='POST' enctype='multipart/form-data'>
	<table align='center'>";
			if ($per!='') {
					echo "<tr><td align='righta'>Per&iacute;odo Actual:</td>
						  <td align='left'><font color=red>".$per."</font></td>
						  </tr>";
			}			
			echo "
				<tr><td align='righta'>Tipo de Archivo:</td>
				<td align='left'>
					<select style='width:300px; border-radius:8px; padding:4px;' name='treporte'>
						<option value='1'>Reporte de Liquidaciones Pagadas</option>

					</select>
				</td>
				</tr>";
			
			$consulta = mysqli_query($enlace,"select * from periodo where id='".$_SESSION['id_validar']."' AND nomina=200 order by nomina,periodo DESC") or die ("select * from periodo order by nomina,periodo DESC");	
			$numr = mysqli_num_rows($consulta);
			echo "<td align = ''> <font color=red>Per&iacute;odo Desde: </font></td>";
			echo "<td align = 'left'> <select style='width:300px; border-radius:8px; padding:4px;' name = 'per1'>";
			for ($x=1;$x<=$numr;$x++) {
				$row1 = mysqli_fetch_array($consulta); 
				if ($x!=1) {
					echo "<option>".$row1['desde'];
				}	
			}
			echo "</select>";
			echo "</td></tr>";
			
			/*$consulta = mysqli_query($enlace,"select * from periodo where id='".$_SESSION['id_validar']."' order by nomina,periodo DESC") or die ("select * from periodo order by nomina,periodo DESC");	
			$numr = mysqli_num_rows($consulta);
			echo "<td align = ''> <font color=red>Per&iacute;odo Hasta: </font></td>";
			echo "<td align = 'left'> <select style='width:300px;' name = 'per2'>";
			for ($x=1;$x<=$numr;$x++) {
				$row1 = mysqli_fetch_array($consulta); 
				if ($x!=1) {
					echo "<option>".$row1['periodo']." - Nomina:".$row1['nomina']." - ".$row1['desde']." al ".$row1['hasta'];
				}	
			}
			echo "</select>";
			echo "</td></tr>";
			*/
			
			/*
			
			echo "<tr>";
			$consulta="Select * from tablas where tabla=ucase('GERENCIA') and id='".$_SESSION['id_validar']."'  order by codigo,detalle";
			$consulta = mysqli_query($enlace,$consulta) or die ("no se pudo realizar la consulta...".$consulta.'='.mysqli_error($enlace));	
			$resultado = mysqli_num_rows($consulta);
			echo "<td align = 'righta'> Gerencia: </td>";
			echo "<td align = 'left'> <select style='width:300px;' name = 'gerencia'><option>";
			if ($resultado != 0) {
				for ($i=0;$i<$resultado;$i++) {
					$row1 = mysqli_fetch_array($consulta);
					echo "<option width = '30%'>".$row1['codigo']." - ".$row1['detalle'];
				}
			}	
			echo "</select>";
			echo "</td>";
			echo "</tr>";
								
			echo "<tr>";
			$consulta="Select * from tablas where tabla=ucase('DIVISION') and id='".$_SESSION['id_validar']."' order by codigo,detalle";
			$consulta = mysqli_query($enlace,$consulta) or die ("no se pudo realizar la consulta...".$consulta.'='.mysqli_error($enlace));	
			$resultado = mysqli_num_rows($consulta);
			echo "<td align = 'righta'> Divisi&oacute;n: </td>";
			echo "<td align = 'left'> <select style='width:300px;' name = 'division'><option>";
			if ($resultado != 0) {
				for ($i=0;$i<$resultado;$i++) {
					$row1 = mysqli_fetch_array($consulta);
					echo "<option width = '30%'>".$row1['codigo']." - ".$row1['detalle'];
				}
			}	
			echo "</select>";
			echo "</td>";
			echo "</tr>";
			
			echo "<tr>";
			$consulta="Select * from tablas where tabla=ucase('DEPARTAMENTO') and id='".$_SESSION['id_validar']."' order by codigo,detalle";
			$consulta = mysqli_query($enlace,$consulta) or die ("no se pudo realizar la consulta...".$consulta.'='.mysqli_error($enlace));	
			$resultado = mysqli_num_rows($consulta);
			echo "<td align = 'righta'> Departamento: </td>";
			echo "<td align = 'left'> <select style='width:300px;' name = 'departamento'><option>";
			if ($resultado != 0) {
				for ($i=0;$i<$resultado;$i++) {
					$row1 = mysqli_fetch_array($consulta);
					echo "<option width = '30%'>".$row1['codigo']." - ".$row1['detalle'];
				}
			}	
			echo "</select>";
			echo "</td>";
			*/
								
			echo "<tr><td colspan='4' align='center'>";
			echo "<input name='ver' type='submit' class='boton' value='Generar Reporte'>";
			echo "</td></tr>";	
			echo "</table></form></font>";
?>
		
	<style>
	.boton {
		border-radius: 8px;      /* Bordes redondeados */
		padding: 8px 16px;       /* Más espacio interno */
		border: none;            /* Quita el borde feo por defecto */
		cursor: pointer;         /* Mano al pasar */
		transition: 0.2s ease;   /* Animación suave */
		}

		.boton:hover {
		transform: scale(1.03);  /* Efecto sutil al pasar el mouse */
		}

	</style>
	</body>
</html>