123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?php
- $fecha = date('d_m_Y');
- header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
- header("Content-Disposition: attachment;filename=horario_$fecha.xlsx");
- header("Cache-Control: max-age=0");
- require_once "../vendor/autoload.php";
- use PhpOffice\PhpSpreadsheet\Spreadsheet;
- use PhpOffice\PhpSpreadsheet\Style\Border;
- use PhpOffice\PhpSpreadsheet\Style\Color;
- use PhpOffice\PhpSpreadsheet\Style\Fill;
- use PhpOffice\PhpSpreadsheet\Style\Alignment;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- $spreadsheet = new Spreadsheet();
- $sheet = $spreadsheet->getActiveSheet();
- // Image settings
- $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
- $drawing->setName('La Salle')
- ->setDescription('La Salle')
- ->setPath('../imagenes/logo.png')
- ->setCoordinates('B1')
- ->setHeight(100)
- ->setOffsetX(10)
- ->setWorksheet($spreadsheet->getActiveSheet());
- $json = file_get_contents('php://input');
- $data = json_decode($json, true);
- empty($data) and die(json_encode(['error' => 'No se recibieron datos', 'data' => $data]));
- $data_excel = array(
- "FECHA" => 'registro_fecha_ideal',
- "CLAVE" => 'profesor_clave',
- "PROFESOR" => 'profesor_nombre',
- "CORREO" => 'profesor_correo',
- "FACULTAD" => 'facultad',
- "MATERIA" => 'materia',
- "CARRERA / PROGRAMA" => 'carrera',
- "GRUPO" => 'horario_grupo',
- "HORARIO" => 'horario_hora_completa',
- "SALÓN" => 'salon',
- "REGISTRO PROFESOR" => 'asistencia',
- "HORA DE REGISTRO" => 'registro_fecha',
- "NOMBRE SUPERVISOR" => 'usuario_nombre',
- "REGISTRO SUPERVISOR" => 'nombre',
- "HORA DE SUPERVISIÓN" => 'registro_fecha_supervisor',
- "OBSERVACIONES" => 'comentario',
- "JUSTIFICACIÓN" => 'justificacion',
- ); // Same as before
- const ROW = 6;
- // Merge cells from A1 to C+ ROW
- $sheet->mergeCells('A1:C' . (ROW - 1));
- // Merge cells from D1 to size of $data_excel + 1
- $sheet->mergeCells('D1:' . chr(65 + count($data_excel) - 1) . (ROW - 1));
- // Set the title in D1 Sistema de Auditoría de Asistencia
- $sheet->setCellValue('D1', 'Sistema de Auditoría de Asistencia');
- $sheet->getStyle('D1')->applyFromArray([
- 'font' => [
- 'bold' => true,
- 'size' => 30,
- 'name' => 'Indivisa Text Sans',
- 'color' => ['argb' => '001d68'],
- ],
- 'alignment' => [
- 'vertical' => Alignment::VERTICAL_CENTER,
- ],
- ]);
- $lastColumnLetter = chr(65 + count($data_excel) - 1);
- $headers_range = 'A' . ROW . ':' . $lastColumnLetter . ROW;
- $keys = array_keys($data_excel);
- array_walk($keys, function ($key, $index) use ($sheet) {
- $sheet->setCellValue(chr(65 + $index) . ROW, $key);
- });
- // Apply the header styles
- $sheet->getStyle($headers_range)->applyFromArray([
- 'font' => [
- 'bold' => true,
- 'size' => 15,
- 'name' => 'Indivisa Text Sans',
- 'color' => ['argb' => Color::COLOR_WHITE],
- ],
- 'alignment' => [
- 'horizontal' => Alignment::HORIZONTAL_CENTER,
- 'vertical' => Alignment::VERTICAL_CENTER,
- ],
- 'fill' => [
- 'fillType' => Fill::FILL_SOLID,
- 'startColor' => ['argb' => '001d68'],
- ]
- ]);
- // set filters
- $sheet->setAutoFilter($headers_range);
- // Styles that are common for all rows can be set outside the loop
- const DEFAULT_FONT = [
- 'size' => 12,
- 'name' => 'Indivisa Text Sans',
- 'color' => ['argb' => '001d68']
- ];
- const DEFAULT_STYLE = [
- 'alignment' => [
- 'vertical' => Alignment::VERTICAL_CENTER,
- 'wrapText' => true,
- ],
- 'font' => DEFAULT_FONT,
- 'borders' => [
- 'outline' => [
- 'borderStyle' => Border::BORDER_THIN,
- 'color' => ['argb' => Color::COLOR_WHITE],
- ]
- ]
- ];
- function getFormattedValue($key, $registro)
- {
- return match ($key) {
- 'asistencia' => is_null($registro['registro_fecha'])
- ? "Sin registro"
- : ($registro['registro_retardo'] ? "Retardo " : "Asistencia "),
- 'registro_fecha', 'registro_fecha_supervisor' =>
- is_null($registro[$key])
- ? 'Sin registro'
- : date('H:i', strtotime($registro[$key])),
- 'nombre' => $registro[$key] ?? "Sin registro",
- 'horario_hora_completa' => "{$registro['horario_hora']} - {$registro['horario_fin']}",
- 'usuario_nombre', 'justificacion', 'comentario' =>
- $registro[$key] ?? "Sin registro",
- default => $registro[$key]
- };
- }
- foreach ($data as $index => $registro) {
- $pair = $index % 2 == 0;
- $cellRange = 'A' . (ROW + $index + 1) . ':' . $lastColumnLetter . (ROW + $index + 1);
- $styleArray = DEFAULT_STYLE;
- $styleArray['fill'] = [
- 'fillType' => Fill::FILL_SOLID,
- 'startColor' => ['argb' => $pair ? 'd4d9dd' : 'f6f7f8'],
- ];
- $sheet->getStyle($cellRange)->applyFromArray($styleArray);
- $values = array_values($data_excel);
- array_walk($values, function ($row, $column_index) use ($sheet, $index, $registro) {
- $value = getFormattedValue($row, $registro);
- $cellLocation = chr(65 + $column_index) . (ROW + $index + 1);
- $sheet->setCellValue($cellLocation, $value);
- if ($value == "Sin registro") {
- $sheet->getStyle($cellLocation)->applyFromArray(['font' => ['color' => ['argb' => 'ea0029']]]);
- }
- });
- }
- foreach ($sheet->getColumnIterator() as $column) {
- $sheet->getColumnDimension($column->getColumnIndex())->setAutoSize(true);
- }
- $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
- $writer->save('php://output');
|