<? session_start();
  ob_start();
  date_default_timezone_set('America/Bogota');

  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");
  include ("calendario/calendario.php");
  require_once 'PHPExcel/PHPExcel.php';

  $enlace=cargar_conexion();

  $query = "select cedula, nombresxx, apellidosxx, tip, fec_ingreso, periodo, sueldo, antiguedad, fdesde, fhasta, dvac, valor_dvac, dias_ext, valor_dias_ext, dbon, valor_dbon, otros, valor_otros_dias, dias_dinero, valor_dias_dinero from vacacion where id=".$_SESSION['id_validar']." order by cedula, fdesde  ";
  $resultados = mysqli_query($enlace,$query) or die ("no se pudo realizar la consulta...");
  
  $item_count = mysqli_num_rows($resultados);
  
  
  $objPHPExcel = new PHPExcel();

  $objPHPExcel->getProperties()->setCreator("Saireh")
    ->setLastModifiedBy("Saireh")->setTitle("Reporte de aportes a la caja de ahorros")
    ->setSubject("Reporte de aportes a la caja de ahorros ")->setDescription("Reporte")
    ->setKeywords("reporte")->setCategory("Reporte Ahorros");
  
  /*$objPHPExcel->getActiveSheet()
    ->setCellValue("A1","cedula")
    ->setCellValue("B1","nombres")
    ->setCellValue("C1","apellidos")
    ->setCellValue("D1","tipo_nomina")
    ->setCellValue("E1","fec_ingreso")
    ->setCellValue("F1","periodo")
    ->setCellValue("G1","sueldo")
    ->setCellValue("H1","antiguedad")
    ->setCellValue("I1","fdesde")
    ->setCellValue("J1","fhasta")
    ->setCellValue("K1","dias_vac")
    ->setCellValue("L1","valor_diasvac")
    ->setCellValue("M1","dias_adic")
    ->setCellValue("N1","valor_dias_adic")
    ->setCellValue("O1","dias_bono")
    ->setCellValue("P1","valor_dias_bono")
    ->setCellValue("Q1","dias_desc_festiv")
    ->setCellValue("R1","valor_desc_festiv")
    ->setCellValue("S1","dias_dinero")
    ->setCellValue("T1","valor_dias_dinero");
	*/ 
	
	$objPHPExcel->getActiveSheet()
    ->setCellValue("A1","CEDULA")
    ->setCellValue("B1","NOMBRES")
    ->setCellValue("C1","APELLIDOS")
	->setCellValue("D1","NOMINA")
    ->setCellValue("E1","FECHA_INGRESO")
    ->setCellValue("F1","PERIODO_VACAC")
    ->setCellValue("G1","ANTIGUEDAD")
    ->setCellValue("H1","FECHA_INIC_VACAC")
    ->setCellValue("I1","FECHA_FIN_VACAC")
    ->setCellValue("J1","VACACIONES_Art190")
    ->setCellValue("K1","DIAS_ADIC_VACAC")
    ->setCellValue("L1","BONO_VACAC_Art192")
	->setCellValue("M1","DIAS_ADIC_BONO_VACAC");
   
   

  for ($it=0; $it < $item_count; $it++) {
    $i = $it+2;
    $row = mysqli_fetch_array($resultados); 
	$dias_bono_vac = $row['dbon'] - $row['antiguedad'] + 1;
	$dias_adic_bono_vac = $row['dbon'] - $dias_bono_vac ;
    
    /*$objPHPExcel->getActiveSheet()
    ->setCellValue("A".$i,$row['cedula'])
    ->setCellValue("B".$i,$row['nombresxx'])
    ->setCellValue("C".$i,$row['apellidosxx'])
    ->setCellValue("D".$i,$row['tip'])
    ->setCellValue("E".$i,$row['fec_ingreso'])
    ->setCellValue("F".$i,$row['periodo'])
    ->setCellValue("G".$i,$row['sueldo'])
    ->setCellValue("H".$i,$row['antiguedad'])
    ->setCellValue("I".$i,$row['fdesde'])
    ->setCellValue("J".$i,$row['fhasta'])
    ->setCellValue("K".$i,$row['dvac'])
    ->setCellValue("L".$i,$row['valor_dvac'])
    ->setCellValue("M".$i,$row['dias_ext'])
    ->setCellValue("N".$i,$row['valor_dias_ext'])
    ->setCellValue("O".$i,$row['dbon'])
    ->setCellValue("P".$i,$row['valor_dbon'])
    ->setCellValue("Q".$i,$row['otros'])
    ->setCellValue("R".$i,$row['valor_otros_dias'])
    ->setCellValue("S".$i,$row['dias_dinero'])
    ->setCellValue("T".$i,$row['valor_dias_dinero']);
	*/
	
	$objPHPExcel->getActiveSheet()
    ->setCellValue("A".$i,$row['cedula'])
    ->setCellValue("B".$i,$row['nombresxx'])
    ->setCellValue("C".$i,$row['apellidosxx'])
    ->setCellValue("D".$i,$row['tip'])
	->setCellValue("E".$i,$row['fec_ingreso'])
    ->setCellValue("F".$i,$row['periodo'])
    ->setCellValue("G".$i,$row['antiguedad'])
    ->setCellValue("H".$i,$row['fdesde'])
    ->setCellValue("I".$i,$row['fhasta'])
    ->setCellValue("J".$i,$row['dvac'])
	->setCellValue("K".$i,$row['dias_ext'])
    ->setCellValue("L".$i,$dias_bono_vac)
	->setCellValue("L".$i,$dias_adic_bono_vac);
   
   
	
	
  }

  $objPHPExcel->getActiveSheet()->setTitle('VACACIONES');
  $objPHPExcel->setActiveSheetIndex(0);

  $sheet = $objPHPExcel->getActiveSheet();
  $cellIterator = $sheet->getRowIterator()->current()->getCellIterator();
  $cellIterator->setIterateOnlyExistingCells(true);
  
  foreach ($cellIterator as $cell) {
      $sheet->getColumnDimension($cell->getColumn())->setAutoSize(true);
  }

  $fecha = date("dmY-His");
  $fileName = "vacaciones-".$fecha.".xlsx";


  $nombrearch="vacaciones-".$fecha.".xlsx";
  //header('Content-Type: application/vnd.ms-excel');
  header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  header('Content-Disposition: attachment;filename="'.$fileName.'"');
  header('Cache-Control: max-age=0');
  header('Cache-Control: max-age=1');
  header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
  header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  header ('Pragma: public'); // HTTP/1.0
  //$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  $objWriter->save('php://output');
  exit;
  
?>