<?php 

set_time_limit(0);


$dbname = "powertime";
$user = "root";
$password = "";
$dsn = "mysql:host=localhost;dbname=$dbname";

$dbh = new PDO($dsn, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);		


		
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;
}

function _data_last_month_day() { 
  $month = date('m');
  $year = date('Y');
  $day = date("d", mktime(0,0,0, $month+1, 0, $year));

  return date('Y-m-d', mktime(0,0,0, $month, $day, $year));
} 

function _data_first_month_day() {
  $month = date('m');
  $year = date('Y');
  return date('Y-m-d', mktime(0,0,0, $month, 1, $year));
}
  
$fecha_inicio = _data_first_month_day()."T00:00:01";
$fecha_termino = _data_last_month_day()."T23:59:00";

echo $fecha_inicio."<br />";
echo $fecha_termino;

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

if($array->respuesta == -6){
	echo "rango de busqueda no puede ser mayor a 31 dias";
}else{
	foreach ($array->marcaciones as $o) {
		$rs= $dbh->prepare("select * from personas WHERE rut = :rut ");
		$rs->execute(array(':rut' => $o->rut));
		echo $rs->rowCount()." -- ".$o->codigo;
		while ($datos = $rs->fetch(PDO::FETCH_BOTH , PDO::FETCH_ORI_NEXT)){
			echo $datos["rut"]."<br />";
		}		
	}
}

 
 

	
?>			
