supervisor_excel.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. $fecha = date('d_m_Y');
  3. header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  4. header("Content-Disposition: attachment;filename=horario_$fecha.xlsx");
  5. header("Cache-Control: max-age=0");
  6. require_once "../vendor/autoload.php";
  7. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  8. use PhpOffice\PhpSpreadsheet\Style\Border;
  9. use PhpOffice\PhpSpreadsheet\Style\Color;
  10. use PhpOffice\PhpSpreadsheet\Style\Fill;
  11. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  12. use PhpOffice\PhpSpreadsheet\IOFactory;
  13. $spreadsheet = new Spreadsheet();
  14. $sheet = $spreadsheet->getActiveSheet();
  15. // Image settings
  16. $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
  17. $drawing->setName('La Salle')
  18. ->setDescription('La Salle')
  19. ->setPath('../imagenes/logo.png')
  20. ->setCoordinates('B1')
  21. ->setHeight(100)
  22. ->setOffsetX(10)
  23. ->setWorksheet($spreadsheet->getActiveSheet());
  24. $json = file_get_contents('php://input');
  25. $data = json_decode($json, true);
  26. empty($data) and die(json_encode(['error' => 'No se recibieron datos', 'data' => $data]));
  27. $data_excel = array(
  28. "FECHA" => 'registro_fecha_ideal',
  29. "CLAVE" => 'profesor_clave',
  30. "PROFESOR" => 'profesor_nombre',
  31. "CORREO" => 'profesor_correo',
  32. "FACULTAD" => 'facultad',
  33. "MATERIA" => 'materia',
  34. "CARRERA / PROGRAMA" => 'carrera',
  35. "GRUPO" => 'horario_grupo',
  36. "HORARIO" => 'horario_hora_completa',
  37. "SALÓN" => 'salon',
  38. "REGISTRO PROFESOR" => 'asistencia',
  39. "HORA DE REGISTRO" => 'registro_fecha',
  40. "NOMBRE SUPERVISOR" => 'usuario_nombre',
  41. "REGISTRO SUPERVISOR" => 'nombre',
  42. "HORA DE SUPERVISIÓN" => 'registro_fecha_supervisor',
  43. "OBSERVACIONES" => 'comentario',
  44. "JUSTIFICACIÓN" => 'justificacion',
  45. ); // Same as before
  46. const ROW = 6;
  47. // Merge cells from A1 to C+ ROW
  48. $sheet->mergeCells('A1:C' . (ROW - 1));
  49. // Merge cells from D1 to size of $data_excel + 1
  50. $sheet->mergeCells('D1:' . chr(65 + count($data_excel) - 1) . (ROW - 1));
  51. // Set the title in D1 Sistema de Auditoría de Asistencia
  52. $sheet->setCellValue('D1', 'Sistema de Auditoría de Asistencia');
  53. $sheet->getStyle('D1')->applyFromArray([
  54. 'font' => [
  55. 'bold' => true,
  56. 'size' => 30,
  57. 'name' => 'Indivisa Text Sans',
  58. 'color' => ['argb' => '001d68'],
  59. ],
  60. 'alignment' => [
  61. 'vertical' => Alignment::VERTICAL_CENTER,
  62. ],
  63. ]);
  64. $lastColumnLetter = chr(65 + count($data_excel) - 1);
  65. $headers_range = 'A' . ROW . ':' . $lastColumnLetter . ROW;
  66. $keys = array_keys($data_excel);
  67. array_walk($keys, function ($key, $index) use ($sheet) {
  68. $sheet->setCellValue(chr(65 + $index) . ROW, $key);
  69. });
  70. // Apply the header styles
  71. $sheet->getStyle($headers_range)->applyFromArray([
  72. 'font' => [
  73. 'bold' => true,
  74. 'size' => 15,
  75. 'name' => 'Indivisa Text Sans',
  76. 'color' => ['argb' => Color::COLOR_WHITE],
  77. ],
  78. 'alignment' => [
  79. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  80. 'vertical' => Alignment::VERTICAL_CENTER,
  81. ],
  82. 'fill' => [
  83. 'fillType' => Fill::FILL_SOLID,
  84. 'startColor' => ['argb' => '001d68'],
  85. ]
  86. ]);
  87. // set filters
  88. $sheet->setAutoFilter($headers_range);
  89. // Styles that are common for all rows can be set outside the loop
  90. const DEFAULT_FONT = [
  91. 'size' => 12,
  92. 'name' => 'Indivisa Text Sans',
  93. 'color' => ['argb' => '001d68']
  94. ];
  95. const DEFAULT_STYLE = [
  96. 'alignment' => [
  97. 'vertical' => Alignment::VERTICAL_CENTER,
  98. 'wrapText' => true,
  99. ],
  100. 'font' => DEFAULT_FONT,
  101. 'borders' => [
  102. 'outline' => [
  103. 'borderStyle' => Border::BORDER_THIN,
  104. 'color' => ['argb' => Color::COLOR_WHITE],
  105. ]
  106. ]
  107. ];
  108. function getFormattedValue($key, $registro)
  109. {
  110. return match ($key) {
  111. 'asistencia' => is_null($registro['registro_fecha'])
  112. ? "Sin registro"
  113. : ($registro['registro_retardo'] ? "Retardo " : "Asistencia "),
  114. 'registro_fecha', 'registro_fecha_supervisor' =>
  115. is_null($registro[$key])
  116. ? 'Sin registro'
  117. : date('H:i', strtotime($registro[$key])),
  118. 'nombre' => $registro[$key] ?? "Sin registro",
  119. 'horario_hora_completa' => "{$registro['horario_hora']} - {$registro['horario_fin']}",
  120. 'usuario_nombre', 'justificacion', 'comentario' =>
  121. $registro[$key] ?? "Sin registro",
  122. default => $registro[$key]
  123. };
  124. }
  125. foreach ($data as $index => $registro) {
  126. $pair = $index % 2 == 0;
  127. $cellRange = 'A' . (ROW + $index + 1) . ':' . $lastColumnLetter . (ROW + $index + 1);
  128. $styleArray = DEFAULT_STYLE;
  129. $styleArray['fill'] = [
  130. 'fillType' => Fill::FILL_SOLID,
  131. 'startColor' => ['argb' => $pair ? 'd4d9dd' : 'f6f7f8'],
  132. ];
  133. $sheet->getStyle($cellRange)->applyFromArray($styleArray);
  134. $values = array_values($data_excel);
  135. array_walk($values, function ($row, $column_index) use ($sheet, $index, $registro) {
  136. $value = getFormattedValue($row, $registro);
  137. $cellLocation = chr(65 + $column_index) . (ROW + $index + 1);
  138. $sheet->setCellValue($cellLocation, $value);
  139. if ($value == "Sin registro") {
  140. $sheet->getStyle($cellLocation)->applyFromArray(['font' => ['color' => ['argb' => 'ea0029']]]);
  141. }
  142. });
  143. }
  144. foreach ($sheet->getColumnIterator() as $column) {
  145. $sheet->getColumnDimension($column->getColumnIndex())->setAutoSize(true);
  146. }
  147. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  148. $writer->save('php://output');