xls_estados.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. function numberToLetter ($number){
  15. $temp = ($number-1) % 26;
  16. return chr($temp + 65);
  17. };
  18. //--- Objeto para validar usuario. El id de usuario lo lee desde sesión
  19. $objSesion = new ValidaSesion($pdo, 121, GEMA);
  20. if(!$objSesion->tieneAcceso()){
  21. echo "No tienes permiso de ver esta página. Revisa que tu sesión siga activa.";
  22. exit();
  23. }
  24. unset($objValida);
  25. $filter_periodo = $_SESSION["periodo_id"];
  26. if(isset($_POST["plan"]) && isset($_POST["fini"], $_POST["ffin"]) && isset($_SESSION["nivel_id"])) {
  27. $filter_plan = filter_input(INPUT_POST, "plan", FILTER_SANITIZE_NUMBER_INT);//limpia texto
  28. $filter_revalida = filter_input(INPUT_POST, "revalida", FILTER_SANITIZE_NUMBER_INT);//limpia texto
  29. $filter_fini = fechaGuion(trim(filter_input(INPUT_POST, "fini", FILTER_SANITIZE_STRING,array('flags' => FILTER_FLAG_STRIP_LOW))));//limpia texto
  30. $filter_ffin = fechaGuion(trim(filter_input(INPUT_POST, "ffin", FILTER_SANITIZE_STRING,array('flags' => FILTER_FLAG_STRIP_LOW))));//limpia texto
  31. $nom = trim(filter_input(INPUT_POST, "prefijo", FILTER_SANITIZE_STRING,array('flags' => FILTER_FLAG_STRIP_LOW)));//limpia texto
  32. }else{
  33. echo "No se recibieron los datos.";
  34. exit();
  35. }
  36. $query = "NULL";
  37. if($filter_plan != 0)
  38. $query = ":plan";
  39. if($filter_revalida == 1)
  40. $stmt = $pdo->prepare('Select * from fs_estadoalumno_cambios(:nivel, '.$query.', :fini, :ffin, true)');//sólo no revalidadas
  41. else
  42. $stmt = $pdo->prepare('Select * from fs_estadoalumno_cambios(:nivel, '.$query.', :fini, :ffin, false)');//sólo no revalidadas
  43. $stmt->bindParam(":nivel", $_SESSION["nivel_id"]);
  44. $stmt->bindParam(":fini", $filter_fini);
  45. $stmt->bindParam(":ffin", $filter_ffin);
  46. if($filter_plan != 0){ $stmt->bindParam(":plan", $filter_plan); }
  47. if(!$stmt->execute()){
  48. echo "Ocurrió un error al cargar el total de estados.";
  49. //print_r($stmt->errorInfo());
  50. exit();
  51. }else{
  52. $estado_rs = $stmt->fetchAll();
  53. $estadoArr = array();
  54. $estado_last = -1;
  55. $subestadoArr = array();
  56. $e = 0;
  57. $total = 0;
  58. foreach($estado_rs as $estado){
  59. if($estado_last != $estado["EstadoAlumno_id"]){
  60. if($estado_last != -1){
  61. $estadoArr[$e]["subestadoArr"]=$subestadoArr;
  62. $estadoArr[$e]["total"] = $total;
  63. $e++;
  64. $total = 0;
  65. }
  66. $estado_last = $estado["EstadoAlumno_id"];
  67. $subestadoArr = array();
  68. }
  69. $estadoArr[$e] = array("id" => $estado["EstadoAlumno_id"], "desc" =>$estado["EstadoAlumno_desc"], "color"=>$estado["EstadoAlumno_color"], "total"=>0, "subestadoArr"=>array());
  70. $subestadoArr[] = array("id" => $estado["SubEstadoAlumno_id"], "desc" =>$estado["SubEstadoAlumno_desc"], "total"=>$estado["SubEstadoAlumno_total"]);
  71. $total+= $estado["SubEstadoAlumno_total"];
  72. }
  73. if($estado_last != -1){
  74. $estadoArr[$e]["subestadoArr"]=$subestadoArr;
  75. $estadoArr[$e]["total"] = $total;
  76. }
  77. unset($e);
  78. }
  79. $stmt->closeCursor();
  80. //--------
  81. //
  82. // Create new Spreadsheet object
  83. $spreadsheet = new Spreadsheet();
  84. // Set document properties
  85. $spreadsheet->getProperties()->setCreator('Ingeniería La Salle')
  86. ->setLastModifiedBy('Ingeniería La Salle')
  87. ->setTitle('Estado de alumnos')
  88. ->setDescription('Cambio de estado de alumnos.');
  89. $alumnoStyle = new Style();
  90. $alumnoStyle->applyFromArray(
  91. [
  92. 'font' => [
  93. 'bold' => true,
  94. 'color' => ['argb' => 'FF001d68'],
  95. 'size' => 14,
  96. ]
  97. ]
  98. );
  99. $headerStyle = new Style();
  100. $headerStyle->applyFromArray(
  101. [
  102. 'fill' => [
  103. 'fillType' => Fill::FILL_SOLID,
  104. 'color' => ['argb' => 'FF001d68'],
  105. ],
  106. 'borders' => [
  107. 'bottom' => ['borderStyle' => Border::BORDER_THIN],
  108. 'right' => ['borderStyle' => Border::BORDER_MEDIUM],
  109. ],
  110. 'font' => [
  111. 'bold' => true,
  112. 'color' => ['argb' => 'FFFFFFFF'],
  113. ]
  114. ]
  115. );
  116. //--------
  117. $sheet = 0;
  118. // Rename worksheet
  119. $spreadsheet->getActiveSheet()->setTitle('Programas');
  120. $row = 1;
  121. $spreadsheet->setActiveSheetIndex($sheet)
  122. ->setCellValueByColumnAndRow(1, $row++, "Programa: ". $nom)
  123. ->setCellValueByColumnAndRow(1, $row++, fechaSlash($filter_fini)." al ". fechaSlash($filter_ffin));
  124. //$spreadsheet->getActiveSheet()->mergeCells('A1:B1');
  125. $spreadsheet->getActiveSheet()->duplicateStyle($alumnoStyle, 'A1:A2');
  126. $row = 4;
  127. $col = 1;
  128. $spreadsheet->setActiveSheetIndex($sheet)
  129. ->setCellValueByColumnAndRow($col++, $row, "Estado")
  130. ->setCellValueByColumnAndRow($col++, $row, "Subestado")
  131. ->setCellValueByColumnAndRow($col++, $row, "Total")
  132. ->setCellValueByColumnAndRow($col++, $row, "Porcentaje");
  133. $colMax = $col;
  134. $colLetter = numberToLetter($colMax-1);
  135. $spreadsheet->getActiveSheet()->duplicateStyle($headerStyle, 'A'.$row.':'.$colLetter.$row);
  136. $row++;
  137. $rowStart = $row;
  138. foreach($estadoArr as $edo){
  139. $spreadsheet->setActiveSheetIndex($sheet)
  140. ->setCellValueByColumnAndRow(1, $row, $edo["desc"]);
  141. if(count($edo["subestadoArr"]) > 1)
  142. $spreadsheet->getActiveSheet()->mergeCells('A'.$row.':A'.($row+count($edo["subestadoArr"])-1));
  143. $row += count($edo["subestadoArr"]);
  144. }
  145. $colStart = 2;
  146. $row = $rowStart;
  147. foreach($estadoArr as $edo){
  148. $total_edo = $edo["total"];
  149. foreach($edo["subestadoArr"] as $subedo){
  150. $col = $colStart;
  151. $spreadsheet->setActiveSheetIndex($sheet)
  152. ->setCellValueByColumnAndRow($col++, $row, $subedo["desc"])
  153. ->setCellValueByColumnAndRow($col++, $row, $subedo["total"])
  154. ->setCellValueByColumnAndRow($col++, $row, $subedo["total"]/$total_edo);
  155. $colLetter = numberToLetter($col-1);
  156. $spreadsheet->setActiveSheetIndex($sheet)->getStyle($colLetter.$row.':'.$colLetter.$row)->getNumberFormat()
  157. ->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_PERCENTAGE_00);
  158. $row++;
  159. }
  160. }
  161. $spreadsheet->getActiveSheet()->duplicateStyle($headerStyle, 'A'.$rowStart.':A'.($row-1));
  162. $spreadsheet->getActiveSheet()->getStyle('B'.$rowStart.':B'.($row-1))->getFont()->setBold(true);
  163. //Auto ajustar anchos
  164. for($col = 1; $col < $colMax; $col++)
  165. $spreadsheet->getActiveSheet()->getColumnDimension(numberToLetter($col))->setAutoSize(true);
  166. //$spreadsheet->getActiveSheet()->setAutoFilter($spreadsheet->getActiveSheet()->calculateWorksheetDimension());
  167. //---------
  168. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  169. $spreadsheet->setActiveSheetIndex(0);
  170. // Redirect output to a client’s web browser (Xlsx)
  171. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  172. header('Content-Disposition: attachment;filename="estadosAlumnos_'.$nom.'.xlsx"');
  173. header('Cache-Control: max-age=0');
  174. // If you're serving to IE 9, then the following may be needed
  175. header('Cache-Control: max-age=1');
  176. // If you're serving to IE over SSL, then the following may be needed
  177. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  178. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
  179. header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  180. header('Pragma: public'); // HTTP/1.0
  181. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  182. $writer->save('php://output');
  183. exit;