<?php
require_once ('../librerias/dompdf/autoload.inc.php');
use Dompdf\Dompdf;



$ch4 = curl_init('http://200.24.13.60/~program1/power-time-api/public/obtenerpersonarut'); //create a new cURL resource
$payload4 = json_encode(array('rut' => $_POST["txtRut"]));
curl_setopt($ch4, CURLOPT_POSTFIELDS, $payload4);//attach encoded JSON string to the POST fields
curl_setopt($ch4, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); //set the content type to application/json
curl_setopt($ch4, CURLOPT_RETURNTRANSFER, true); //return response instead of outputting
$result4 = curl_exec($ch4); //execute the POST request
$array4 = json_decode($result4);

$id_persona = $array4[0]->id;
$personaHTML = "<tr>";			
$personaHTML .= "<td>".STRTOUPPER($array4[0]->rut) ."</td>";		
$personaHTML .= "<td>".STRTOUPPER($array4[0]->nombre ." ". $array4[0]->apellido ) ."</td>";	
$personaHTML .= "</tr>";		

 		 
curl_close($ch4); 	

/***************************************************/

$ch3 = curl_init('http://200.24.13.60/~program1/power-time-api/public/obtenercargoasignado'); //create a new cURL resource
$payload3 = json_encode(array('id_persona' => $id_persona));
curl_setopt($ch3, CURLOPT_POSTFIELDS, $payload3);//attach encoded JSON string to the POST fields
curl_setopt($ch3, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); //set the content type to application/json
curl_setopt($ch3, CURLOPT_RETURNTRANSFER, true); //return response instead of outputting
$result3 = curl_exec($ch3); //execute the POST request
$array3 = json_decode($result3);

 
$cargo = $array3[0]->descripcion;			

curl_close($ch3); 	

/***********************************************************************/

$ch4 = curl_init('http://200.24.13.60/~program1/power-time-api/public/obtenerdepartamentoasignado'); //create a new cURL resource
$payload4 = json_encode(array('id_persona' => $id_persona));
curl_setopt($ch4, CURLOPT_POSTFIELDS, $payload4);//attach encoded JSON string to the POST fields
curl_setopt($ch4, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); //set the content type to application/json
curl_setopt($ch4, CURLOPT_RETURNTRANSFER, true); //return response instead of outputting
$result4 = curl_exec($ch4); //execute the POST request
$array4 = json_decode($result4);

$cargoHTML = "";				
foreach ($array4[0] as $row) {
	$cargoHTML .= "<tr>";			
	$cargoHTML .= "<td>".STRTOUPPER($row->sucursal) ."</td>";		
	$cargoHTML .= "<td>".STRTOUPPER($row->descripcion) ."</td>";		
	$cargoHTML .= "</tr>";		
}
 		 

curl_close($ch4); 	
			
/***************************************************/


$ch4 = curl_init('http://200.24.13.60/~program1/power-time-api/public/turnopersona'); //create a new cURL resource
$payload4 = json_encode(array('id_persona' => $id_persona));
curl_setopt($ch4, CURLOPT_POSTFIELDS, $payload4);//attach encoded JSON string to the POST fields
curl_setopt($ch4, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); //set the content type to application/json
curl_setopt($ch4, CURLOPT_RETURNTRANSFER, true); //return response instead of outputting
$result4 = curl_exec($ch4); //execute the POST request
$array4 = json_decode($result4);

$turnoHTML = "";				
foreach ($array4[0] as $row) {
	$turnoHTML .= "<tr>";			
	$turnoHTML .= "<td>".STRTOUPPER($row->descripcion) ."</td>";		
	$turnoHTML .= "</tr>";		
}
 		 

curl_close($ch4); 	

/***************************************************/
$marcajeHTML = "";

function processCurlJsonrequest($URL, $fieldString)
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Accept: application/json'));
	curl_setopt($ch, CURLOPT_URL, $URL);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
	curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
	curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fieldString));
	curl_setopt($ch, CURLOPT_POST, 1);
	$resulta = curl_exec($ch);
	if (curl_errno($ch)) {
		print curl_error($ch);
	} else {
		curl_close($ch);
	}
	return $resulta;
}


$fecha = $_POST["txtFecha_inicio"]."T00:00:01";
$fecha_termino = $_POST["txtFecha_termino"]."T23:59:00";
$identificador = $_POST["txtRut"];		

$data_string = array(
	'apiKey' => 'fd2b105003aa78c2751a71f4dd3f79b7',
	'inicio' => $fecha,
	'termino' => $fecha_termino,
	'identificador' =>  [$identificador]  
);
$data =  processCurlJsonrequest('https://app.relojcontrol.com/api/consultaMarcaciones/consulta',$data_string);
$array = json_decode($data);

if($array->respuesta > 0){
 
		$marcajeHTML .= "<tr>";			
		$marcajeHTML .= "<td colspan='6'>No Se Registran Marcajes</td>";				
		$marcajeHTML .= "</tr>";	
 
}else{
	foreach ($array->marcaciones as $o) {
		
		$tipo = "ENTRADA";
		if(intval($o->tipo) == 1) { $tipo = "SALIDA"; }
		if(intval($o->tipo) == 2) { $tipo = "SALIDA EXTRAORDINARIA"; }
		if(intval($o->tipo) == 3) { $tipo = "ENTRADA EXTRAORDINARIA"; }
		if(intval($o->tipo) == 4) { $tipo = "SALIDA A DESCANSO"; }
		if(intval($o->tipo) == 5) { $tipo = "ENTRADA DESPUES DE DESCANSO"; }
		
		$array = explode("T", $o->fechaHora);

		$marcajeHTML .= "<tr>";			
		$marcajeHTML .= "<td>". $array[0]  ."</td>";	
		$marcajeHTML .= "<td>". $array[1]  ."</td>";		
		$marcajeHTML .= "<td>".STRTOUPPER($o->nombres ." ".$o->apellidos) ."</td>";	
		$marcajeHTML .= "<td>".STRTOUPPER($o->departamento) ."</td>";	
		$marcajeHTML .= "<td>".STRTOUPPER($o->sucursal) ."</td>";		
		$marcajeHTML .= "<td>".STRTOUPPER($tipo) ."</td>";				
		$marcajeHTML .= "</tr>";				
		
	}				

	
}

 
			
/***************************************************/
				
$html = '
<style>
body{
	font-size:10px;
}
h3{
	font-size:20px;
	display:inline;
}
h4{
	font-size:14px;
	display:inline;	
}
.tbl-marcaje th {
  color: #000000;
  background-color: #CCCCCC;
  border:solid 1px #000000;
}
.tbl-marcaje td {
	border:solid 1px #000000;
}
	
</style>
<div>
	<img style="max-height:50px;" src="../img/002.png" />
</div> 
<table width="100%">
	<tr>
		<td width="70%"><b>COMPA&Ntilde;IA :</b> CORP MUNICIPAL PARA EL DESARROLLO SOCIAL</td>	
		<td><b>Fecha Inicio :</b> '.$_POST["txtFecha_inicio"].' </td>			
	</tr>
	<tr>		
		<td width="70%"><b>RUT :</b> 70.983.600-5</td>		
		<td><b>Fecha T&eacute;rmino :</b>  '.$_POST["txtFecha_termino"].'</td>				
	</tr>	
</table><br />
<center><h3>Consulta de Marcajes Trabajador  </h3></center>
 
<h4>Informaci&oacute;n de Trabajador:</h4>
<table class="tbl-marcaje" width="100%">
	<tr>
		<th><b>RUT </b></th>
		<th><b>NOMBRE </b></th>		
	</tr>
	'.$personaHTML.'		
		
</table>   
<br /> 	
<h4>Informaci&oacute;n de Turno Laboral:</h4>
<table class="tbl-marcaje" width="30%">
	<tr>
		<th><b>TURNO </b></th>			
	</tr>
	'.$turnoHTML.'		
		
</table>   
<br /> 	
<h4>Sucursal / Departamento:</h4>
<table class="tbl-marcaje" width="100%">
	<tr>
		<th width="30%"><b>SUCURSAL </b></th>
		<th><b>DEPARTAMENTO </b></th>			
	</tr>
	'.$cargoHTML.'		
		
</table>   
<br /> 	
		
<br /> 	
<h4>Listado de Marcajes :</h4>
<table class="tbl-marcaje" width="100%">
	<tr>
		<th><b>FECHA </b></th>
		<th><b>HORA </b></th>	
		<th><b>TRABAJADOR </b></th>		
		<th><b>DEPARTAMENTO </b></th>				
		<th><b>SUCURSAL </b></th>	
		<th><b>TIPO MOVIMIENTO </b></th>			
	</tr>
	'.$marcajeHTML.'			
</table>  



';
 
$pdf = new DOMPDF();
$pdf->set_paper("A4", "portrait");
$pdf->load_html(utf8_encode($html));
$pdf->render();
$pdf->stream("marcajes.pdf", array("Attachment" => false));
exit(0);
 
?>
