xls_alumnosestado.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. setlocale(LC_TIME, 'es_MX.UTF-8');
  3. require_once("../../include/nocache.php");
  4. require_once("../../include/constantes.php");
  5. require_once("../../include/bd_pdo.php");
  6. require_once("../../include/util.php");
  7. require_once("../../classes/ValidaSesion.php");
  8. require "../../include/phpSpreadsheet/autoload.php";
  9. use PhpOffice\PhpSpreadsheet\IOFactory;
  10. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  11. use PhpOffice\PhpSpreadsheet\Style\Border;
  12. use PhpOffice\PhpSpreadsheet\Style\Fill;
  13. use PhpOffice\PhpSpreadsheet\Style\Style;
  14. use PhpOffice\PhpSpreadsheet\Shared\Date;
  15. function numberToLetter ($number){
  16. $temp = ($number-1) % 26;
  17. return chr($temp + 65);
  18. };
  19. //--- Objeto para validar usuario. El id de usuario lo lee desde sesión
  20. $objSesion = new ValidaSesion($pdo, 127, GEMA);
  21. if(!$objSesion->tieneAcceso()){
  22. echo "No tienes permiso de ver esta página. Revisa que tu sesión siga activa.";
  23. exit();
  24. }
  25. unset($objValida);
  26. if(!isset($_POST["fecha_corte"])){
  27. echo "No hay fechas de corte seleccionadas.";
  28. exit();
  29. }
  30. $fechasCorteArr = $_POST["fecha_corte"];
  31. //--------
  32. //
  33. // Create new Spreadsheet object
  34. $spreadsheet = new Spreadsheet();
  35. // Set document properties
  36. $spreadsheet->getProperties()->setCreator('Ingeniería La Salle')
  37. ->setLastModifiedBy('Ingeniería La Salle')
  38. ->setTitle('Estado de Alumnos')
  39. ->setDescription('Estado actual de alumnos.');
  40. $headerStyle = new Style();
  41. $headerStyle->applyFromArray(
  42. [
  43. 'fill' => [
  44. 'fillType' => Fill::FILL_SOLID,
  45. 'color' => ['argb' => 'FF001d68'],
  46. ],
  47. 'borders' => [
  48. 'bottom' => ['borderStyle' => Border::BORDER_THIN],
  49. 'right' => ['borderStyle' => Border::BORDER_MEDIUM],
  50. ],
  51. 'font' => [
  52. 'bold' => true,
  53. 'color' => ['argb' => 'FFFFFFFF'],
  54. ]
  55. ]
  56. );
  57. //--------
  58. $sheet = 0;
  59. // Rename worksheet
  60. //ordena de menor a mayor
  61. usort($fechasCorteArr, function ($item1, $item2) {
  62. return fechaGuion($item1) <=> fechaGuion($item2);
  63. });
  64. $prefijo = $_POST["prefijo"];
  65. foreach ($fechasCorteArr as $fecha_hoy){
  66. $query = "";
  67. if(isset($_POST["fecha_inicial"]) && $_POST["fecha_inicial"] !=""){
  68. $filter_fini = fechaGuion(trim(filter_input(INPUT_POST, "fecha_inicial", FILTER_SANITIZE_STRING,array('flags' => FILTER_FLAG_STRIP_LOW))));//limpia texto
  69. $query .= ":fini, ";
  70. }else{
  71. $query .= "NULL, ";
  72. }
  73. if(isset($_POST["fecha_final"]) && $_POST["fecha_final"] !="") {
  74. $filter_ffin = fechaGuion(trim(filter_input(INPUT_POST, "fecha_final", FILTER_SANITIZE_STRING,array('flags' => FILTER_FLAG_STRIP_LOW))));//limpia texto
  75. $query .= ":ffin, ";
  76. }else{
  77. $query .= "NULL, ";
  78. }
  79. $fecha_hoy = fechaGuion($fecha_hoy);
  80. $query .= ":fhoy, ";
  81. if(isset($_POST["estado"]) && $_POST["estado"] !=""){
  82. $filter_estado = filter_input(INPUT_POST, "estado", FILTER_SANITIZE_NUMBER_INT);//limpia texto
  83. $query .= ":edo, ";
  84. }else{
  85. $query .= "NULL, ";
  86. }
  87. if(isset($_POST["carrera"]) && $_POST["carrera"] !="" ){
  88. $filter_carrera = filter_input(INPUT_POST, "carrera", FILTER_SANITIZE_NUMBER_INT);//limpia texto
  89. $query .= ":carrera, ";
  90. }else{
  91. $query .= "NULL, ";
  92. }
  93. if(isset($_POST["revalida"]) && intval($_POST["revalida"]) == 1 ){
  94. $query .= "true, ";
  95. }else{
  96. $query .= "false, ";
  97. }
  98. $query .= "0, NULL";
  99. $stmt = $pdo->prepare('Select * from fs_alumnosestado('.$query.')');//sólo activos
  100. if(isset($filter_fini)) $stmt->bindParam(":fini", $filter_fini);
  101. if(isset($filter_ffin)) $stmt->bindParam(":ffin", $filter_ffin);
  102. if(isset($filter_estado)) $stmt->bindParam(":edo", $filter_estado);
  103. if(isset($filter_carrera)) $stmt->bindParam(":carrera", $filter_carrera);
  104. $stmt->bindParam(":fhoy", $fecha_hoy);
  105. if(!$stmt->execute()){
  106. echo "Ocurrió un error al cargar los alumnos.";
  107. //print_r($stmt->errorInfo());
  108. exit();
  109. }
  110. $alumno_rs = $stmt->fetchAll();
  111. $stmt->closeCursor();
  112. $spreadsheet->setActiveSheetIndex($sheet)->setTitle('Corte '. fechaGuion($fecha_hoy));
  113. $row = 1;
  114. $col = 1;
  115. $spreadsheet->getActiveSheet()
  116. ->setCellValueByColumnAndRow($col++, $row, "Estado")
  117. ->setCellValueByColumnAndRow($col++, $row, "Subestado")
  118. ->setCellValueByColumnAndRow($col++, $row, "Fecha estado")
  119. ->setCellValueByColumnAndRow($col++, $row, "Clave")
  120. ->setCellValueByColumnAndRow($col++, $row, "Apellidos")
  121. ->setCellValueByColumnAndRow($col++, $row, "Nombre")
  122. ->setCellValueByColumnAndRow($col++, $row, "Generación")
  123. ->setCellValueByColumnAndRow($col++, $row, "CURP")
  124. ->setCellValueByColumnAndRow($col++, $row, "Correo")
  125. ->setCellValueByColumnAndRow($col++, $row, "Carrera")
  126. ->setCellValueByColumnAndRow($col++, $row, "Plan");
  127. $colMax = $col;
  128. $colLetter = numberToLetter($colMax-1);
  129. $spreadsheet->getActiveSheet()->duplicateStyle($headerStyle, 'A1:'.$colLetter.'1');
  130. $row = 2;
  131. $startRow = $row;
  132. foreach($alumno_rs as $alumno){
  133. $col = 1;
  134. //$spreadsheet->setActiveSheetIndex($sheet)
  135. $spreadsheet->getActiveSheet()
  136. ->setCellValueByColumnAndRow($col++, $row, $alumno["EstadoAlumno_desc"])
  137. ->setCellValueByColumnAndRow($col++, $row, $alumno["SubEstadoAlumno_desc"])
  138. ->setCellValueByColumnAndRow($col++, $row, fechaSlash($alumno["SEA_fecha"]))
  139. ->setCellValueByColumnAndRow($col++, $row, $alumno["Usuario_claveULSA"])
  140. ->setCellValueByColumnAndRow($col++, $row, $alumno["Usuario_apellidos"])
  141. ->setCellValueByColumnAndRow($col++, $row, $alumno["Usuario_nombre"])
  142. ->setCellValueByColumnAndRow($col++, $row, fechaMonthPicker($alumno["Alumno_generacion"]))
  143. ->setCellValueByColumnAndRow($col++, $row, $alumno["Usuario_curp"])
  144. ->setCellValueByColumnAndRow($col++, $row, $alumno["Contacto_correo"])
  145. ->setCellValueByColumnAndRow($col++, $row, $alumno["Carrera_desc"])
  146. ->setCellValueByColumnAndRow($col++, $row, $alumno["PlanEstudio_desc"]);
  147. $spreadsheet->getActiveSheet()->getStyle(numberToLetter(1).$row)->applyFromArray(['font' => [ 'color' => ['argb' => str_replace("#", "FF", $alumno["EstadoAlumno_color"])] ] ]);
  148. $spreadsheet->getActiveSheet()->getStyle(numberToLetter(3).$row)->getNumberFormat()
  149. ->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DDMMYYYY);
  150. $spreadsheet->getActiveSheet()->getStyle(numberToLetter(4).$row)->getNumberFormat()->setFormatCode('000000');
  151. $row++;
  152. }
  153. $row--;
  154. //Auto ajustar anchos
  155. for($col = 1; $col < $colMax; $col++)
  156. $spreadsheet->getActiveSheet()->getColumnDimension(numberToLetter($col))->setAutoSize(true);
  157. //$spreadsheet->getActiveSheet()->getStyle('A1:H1')->getFont()->setBold(true);
  158. $spreadsheet->getActiveSheet()->setAutoFilter($spreadsheet->getActiveSheet()->calculateWorksheetDimension());
  159. $sheet++;
  160. if($sheet < count($fechasCorteArr)){
  161. $spreadsheet->createSheet();
  162. }
  163. }
  164. //---------
  165. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  166. $spreadsheet->setActiveSheetIndex(0);
  167. // Redirect output to a client’s web browser (Xlsx)
  168. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  169. header('Content-Disposition: attachment;filename="alumnos'.$prefijo.'.xlsx"');
  170. header('Cache-Control: max-age=0');
  171. // If you're serving to IE 9, then the following may be needed
  172. header('Cache-Control: max-age=1');
  173. // If you're serving to IE over SSL, then the following may be needed
  174. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  175. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
  176. header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  177. header('Pragma: public'); // HTTP/1.0
  178. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  179. $writer->save('php://output');
  180. exit;