<?php
require_once '../librerias/PHPExcel/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();	

$fc_inicio = $_POST["fechainicio"];
$fc_termino =  $_POST["fechatermino"];
$idacepta = $_POST["idacepta"];
$departamento = $_POST["departamento"]; 

function processCurlJsonrequestPOST($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;
}	


$url = 'http://200.24.13.60/~program1/power-time-api/public/listarestadoaprobacionaceptajuridico';
$ch = curl_init($url); //create a new cURL resource
$payload = json_encode(array('idacepta' => $idacepta, 'fecha_inicio' => $fc_inicio, 'fecha_termino' => $fc_termino, 'departamento' => $departamento));
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);//attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); //set the content type to application/json
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //return response instead of outputting
$result = curl_exec($ch); //execute the POST request
$array = json_decode($result);

	
$objPHPExcel->getProperties()
->setCreator("CCBSOLUTIONS PERMISOS")
->setLastModifiedBy("CCBSOLUTIONS PERMISOS")
->setTitle("LISTADO DE PERMISOS")
->setSubject("LISTADO DE PERMISOS")
->setDescription("LISTADO DE PERMISOS")
->setKeywords("LISTADO DE PERMISOS")
->setCategory("LISTADO DE PERMISOS");


$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'CODIGO ACEPTA')
->setCellValue('B1', 'N FOLIO')
->setCellValue('C1', 'TIPO PERMISO')
->setCellValue('D1', 'FECHA INICIO')
->setCellValue('E1', 'FECHA TÉRMINO')
->setCellValue('F1', 'DIAS HÁBILES')						
->setCellValue('G1', 'RUT')	
->setCellValue('H1', 'NOMBRE')	
->setCellValue('I1', 'Área')	
->setCellValue('J1', 'Centro')	
->setCellValue('K1', 'ESTADO FIRMA ELECTRÓNICA');		
	
$i=2;

$estados = array("PENDIENTE", "EN PROCESO DE FIRMA", "FIRMADO POR TODOS", "RECHAZADO");
foreach ($array[0] as $row) {

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


	$objPHPExcel->setActiveSheetIndex(0)
	->setCellValue('A'.$i, $row->pdfacepta)
	->setCellValue('B'.$i, $row->id)
	->setCellValue('C'.$i, utf8_decode($row->tipo))
	->setCellValue('D'.$i, date("d/m/Y", strtotime($row->fecha_inicio )))
	->setCellValue('E'.$i, date("d/m/Y", strtotime($row->fecha_termino)))
	->setCellValue('F'.$i, $row->cantidad)						
	->setCellValue('G'.$i, $row->rut)
	->setCellValue('H'.$i, $row->nombre ." ". $row->apellido)
	->setCellValue('I'.$i, $array2[0][0]->sucursal)
	->setCellValue('J'.$i, $array2[0][0]->departamento)
	->setCellValue('K'.$i, $estados[$row->idacepta]);


    $i++;
}  


$objPHPExcel->getActiveSheet()->setTitle('CCBSOLUTIONS CONTRATOS');
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="InformePermsisos.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;	

?>