<? 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 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");

  for ($it=0; $it < $item_count; $it++) {
    $i = $it+2;
    $row = mysqli_fetch_array($resultados); 
    
    $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()->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="Asientos_Utilidades";
  //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;
  
?>