<?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();
$accion=$_REQUEST["accion"];
$cedula=$_REQUEST["cedula"];
$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=''; 
menu();
set_time_limit(3000); 

///////////////////////////////////////////////// MOSTRAR DATA A INGRESAR ////////////////////////////////////////////////////////////////
if ($accion==1) {
	include ("calendario/calendario.php");
	echo "<b>Opciones:</b> &nbsp;&nbsp;<img border = 0 src='images/b_regresa.png' alt='Regresar'><a href='archivo_trabajador.php'>Regresar</a>";
	echo "<hr><h2>Constancia de Trabajo para el Personal</h2>";	

	echo "<form name = 'fentrada' action = 'archivo_trabajador_constancia_imprimir.php' method = 'POST' target='_blank' enctype='multipart/form-data'>";
	echo "<input type = 'hidden' name = 'grabar' value = '1'>";

	//====================== buscar datos de parametros	
	$consulta=mysqli_query($enlace,"select * from empresa where id='".$_SESSION['id_validar']."' ");
	if (mysqli_num_rows($consulta)!=0){
		$row=mysqli_fetch_array($consulta);
		$dvac=$row['dvac'];
		$dbon=$row['dbon'];
		$con01=$row['constante01'];
		$con02=$row['constante02'];
		$con03=$row['constante03'];
		$con04=$row['constante04'];
	}   
	
	$consulta="select * from personal where id='".$_SESSION['id_validar']."' and codigo='".$cedula."' ";
	$resultado = mysqli_query($enlace,$consulta) or die('verificaci&oacute; del c&oacute;digo fall&oacute;: ' . mysqli_error($enlace));
	$num_rows = mysqli_num_rows($resultado);
	if ($num_rows!=0) {
		$row=mysqli_fetch_array($resultado);
		$cedula = $row['codigo'];
		
		echo "<input type = 'hidden' name='fechai' value = '".$row['fechai']."'>";
		echo "<table width='70%' rules=none border=0 cellpadding=2 cellspacing=0 align='center' style='border-width: 1px; border-color: 75263C;border-style: solid;'>";

		//=================================================================================================///
		echo "<tr bgcolor='#43bced' height='25'>";
		echo "<td colspan=2 align = center><font color='white'> Datos Personales </font></td>";
		echo "</tr>";
		
		
		echo "<tr><td width = '20%' align = 'right'> C&eacute;dula: </td>";
		echo "<td width = '36%' align = 'left'> <input type = 'text' name = 'cedula' size = '10' maxlength = '8' value = '".$row['codigo']."' readonly> * </td>";
		echo "</tr>";

		echo "<tr><td width = '15%' align = 'right'> Nombres: </td>";
		echo "<td width = '29%' align = 'left'> <input type = 'text' name = 'nombres' size = '50' maxlength = '50' value = '".strtoupper(trim($row['nombres'])." ".$row['nombres1'])."' readonly> * </td>";
		echo "</tr>";

		echo "<tr><td align = 'right'> Apellidos: </td>";
		echo "<td align = 'left'> <input type = 'text' name = 'apellidos' size = '50' maxlength = '50' value = '".strtoupper(trim($row['apellidos'])." ".$row['apellidos1'])."' readonly> * </td>";
		echo "</tr>";

		echo "<tr><td align = 'right' > Fecha de Ingreso: </td>";
		echo "<td align = 'left' > <input type = 'text' name = 'fec' size = '30' maxlength = '50' value = '".fecha_larga(mostrar_fecha($row['fechai']))."' readonly></td>";
		echo "</tr>";

		echo "<tr><td align = 'right' > Sueldo Base: </td>";
		echo "<td align = 'left' > <input type = 'text' name = 'smes' size = '30' maxlength = '50' value = '".$row['smes']."' readonly></td>";
		echo "</tr>";
		
		//====================== buscar tipo de nomina	
		$tipo=1;$multiplo=0;
		$consulta1="select tipo from nomina where id='".$_SESSION['id_validar']."' and codigo='".$row['nomina']."'";
		$resultado1 = mysqli_query($enlace,$consulta1) or die($consulta1 . mysqli_error($enlace));
		while ($row1 = mysqli_fetch_array($resultado1)) $tipo=$row1['tipo'];
		if ($tipo==1) $multiplo=12;
		else if ($tipo==2) $multiplo=6;
			 else $multiplo=3;
	    if ($icomi==1) $multiplo=$multiplo/2;		
		
		//====================== buscar monto constante	
		$mon01=0;$mon02=0;$mon03=0;$mon04=0;	
		if ($con01!='') {
			$consultax="select monto from constante_individual where id='".$_SESSION['id_validar']."' and codigo='".$con01."' and cedula='".$cedula."' and nomina='".$row['nomina']."'";
			$resultadox = mysqli_query($enlace,$consultax) or die($consultax . mysqli_error($enlace));
			if (mysqli_num_rows($resultadox)!=0) {
				$rowx=mysqli_fetch_array($resultadox);
				if ($tipo==1) $mon01=$rowx['monto']/7*30;
				else if ($tipo==2) $mon01=$rowx['monto']*2;
					else $mon01=$rowx['monto'];
			}	 
		}	
		if ($con02!='') {
			$consultax="select monto from constante_individual where id='".$_SESSION['id_validar']."' and codigo='".$con02."' and cedula='".$cedula."' and nomina='".$row['nomina']."'";
			$resultadox = mysqli_query($enlace,$consultax) or die($consultax . mysqli_error($enlace));
			if (mysqli_num_rows($resultadox)!=0) {
				$rowx=mysqli_fetch_array($resultadox);
				if ($tipo==1) $mon02=$rowx['monto']/7*30;
				else if ($tipo==2) $mon02=$rowx['monto']*2;
					else $mon02=$rowx['monto'];
			}	
		}	

		//====================== buscar monto conceptos
		//if ($con03!='') {
			/*$consultax="select sum(monto) as total ,count(monto) as cantidad from historico where id='".$_SESSION['id_validar']."' and concepto='".$con03."' and cedula='".$cedula."' and nomina='".$row['nomina']."' order by xxx desc limit ".$multiplo;
			$resultadox = mysqli_query($enlace,$consultax) or die($consultax . mysqli_error($enlace));
			$rowx=mysqli_fetch_array($resultadox);
			if ($rowx['cantidad']>0) {
				if ($rowx['cantidad']==1) {
					$mon03=$rowx['total'];
				} else {	
					if ($tipo==1) $mon03=$rowx['total'] / ($rowx['cantidad'] / 4);
					if ($tipo==2) $mon03=$rowx['total'] / ($rowx['cantidad'] / 2);
					if ($tipo==3) $mon03=$rowx['total'] / ($rowx['cantidad'] / 1);
				}	
			}	
			$consultax="select monto from historico where id='".$_SESSION['id_validar']."' and (substring(concepto,1,3)='109' OR substring(concepto,1,1)='7') and cedula='".$cedula."' and nomina='".$row['nomina']."' order by xxx desc limit ".$multiplo;
			$resultadox = mysqli_query($enlace,$consultax) or die('verificaci&oacute; del c&oacute;digo fall&oacute;: ' . mysqli_error($enlace));
			$mpaty=0; $cpaty=0;
			while ($rowx=mysqli_fetch_array($resultadox)) {
				$mpaty +=$rowx['monto'];	
				$cpaty ++;
				//echo $rowx['monto']."</br>";
			}
			
			if ($cpaty>0) {
				if ($cpaty==1) {
					$mon03=$mpaty;
				} else {	
					$mon03= $mpaty / $cpaty;
				}	
			}*/
			
			$consultax="select round(avg(monto),2) as monto from  (select sum(sueldo) as monto from personal_sueldo where id='".$_SESSION['id_validar']."' and codigo='".$cedula."' group by fec order by xxx desc limit ".$multiplo.") as prueba";
			$resultadox = mysqli_query($enlace,$consultax) or die('verificaci&oacute; del c&oacute;digo fall&oacute;: ' . mysqli_error($enlace));
			$mpaty=0; $cpaty=0;
			if ($rowx=mysqli_fetch_array($resultadox)) {
				$mon03 = $rowx['monto'];	////trae promedio Quincenal
				$mon03 = $mon03/15*30; ///Para llevar el promedio a mensual
			}
			
		//}	
		if ($con04!='') {
			$consultax="select sum(monto) as total from historico where id='".$_SESSION['id_validar']."' and substring(concepto,1,".strlen($con04).")='".$con04."' and cedula='".$cedula."' and nomina='".$row['nomina']."' group by periodo order by xxx desc limit ".$multiplo;
			$resultadox = mysqli_query($enlace,$consultax) or die($consultax . mysqli_error($enlace));
			$can = mysqli_num_rows($resultadox);
			if ($can>0) {
				$tot=0;
				while ($rowx=mysqli_fetch_array($resultadox)) $tot += $rowx['total'];
				if ($tipo==1) $mon04=$tot / ($can / 4);
				if ($tipo==2) $mon04=$tot / ($can / 2);
				if ($tipo==3) $mon04=$tot / ($can / 1);
			}	
		}	
		
		$smes=$mon03;
		echo "<tr><td align = 'right' > Sueldo Promedio (ult. 3 meses): </td>";
		echo "<td align = 'left' > <input type = 'text' name = 'smesx' size = '30' maxlength = '50' value = '".$smes."' ></td>";
		echo "</tr>";
		
		$consulta1="select * from tablas where tabla='CARGO' and codigo='".$row['cargo']."' and id='".$_SESSION['id_validar']."' ";
		$resultado1 = mysqli_query($enlace,$consulta1) or die($consulta1 . mysqli_error($enlace));
		$row1=mysqli_fetch_array($resultado1);

		echo "<tr><td align = 'right' > Cargo: </td>";
		echo "<td align = 'left' > <input type = 'text' name = 'cargo' size = '50' maxlength = '50' value = '".strtoupper($row1['detalle'])."' readonly></td>";
		echo "</tr>";

		echo "<tr><td align = 'right' > Dirigido a: </td>";
		echo "<td align = 'left' > <input type = 'text' name = 'dirigido' size = '50' maxlength = '50'></td>";
		echo "</tr>";

		
		echo "<tr><td align = 'right' > </td>";
		echo "<td align = 'left'>";
		echo "<input name='boton' type = 'submit' value = 'Visualizar Constancia'>";
		echo "</td></tr>";
		
		echo "</tr>";
		echo "</table>";

	}
	echo "	<script language='javascript' type='text/javascript'>";
	echo "		document.fentrada.dirigido.focus()";
	echo "	</script>";
	echo "</form>"; 

	}		
echo "<br><br><br><br></body>";
echo "</html>";
mysqli_close($enlace);
?>