days = array_values($days); $this->rows = array_values($rows); } public function title(): string { return 'Field Work'; } public function array(): array { $identityCount = count(self::IDENTITY); $lastColumn = $identityCount + count($this->days) * 2 + 1; $title = array_fill(0, $lastColumn, ''); $title[0] = 'Monthly Field Work Report'; $header = array_fill(0, $lastColumn, ''); foreach (self::IDENTITY as $index => $label) { $header[$index] = $label; } foreach ($this->days as $index => $day) { $header[$identityCount + $index * 2] = $this->dayLabel((string) $day); } $header[$lastColumn - 1] = 'RBM/ZBM wise Total'; $sub = array_fill(0, $lastColumn, ''); foreach ($this->days as $index => $day) { $column = $identityCount + $index * 2; $sub[$column] = 'Link'; $sub[$column + 1] = 'Time'; } $grid = [$title, $header, $sub]; foreach ($this->rows as $row) { $line = array_fill(0, $lastColumn, ''); $line[0] = (string) ($row['name'] ?? ''); $line[1] = $this->codeValue($row['employee_code'] ?? ''); $line[2] = (string) ($row['role_name'] ?? ''); $line[3] = (string) ($row['position_name'] ?? ''); $line[4] = $this->codeValue($row['manager_1_code'] ?? ''); $line[5] = (string) ($row['manager_1_name'] ?? ''); $line[6] = $this->codeValue($row['manager_2_code'] ?? ''); $line[7] = (string) ($row['manager_2_name'] ?? ''); $cells = is_array($row['days'] ?? null) ? $row['days'] : []; foreach ($this->days as $index => $day) { $column = $identityCount + $index * 2; $cell = is_array($cells[$day] ?? null) ? $cells[$day] : ['status' => 'blank']; $status = $cell['status'] ?? 'blank'; if ($status === 'link' && !empty($cell['link'])) { $line[$column] = 'Link'; $line[$column + 1] = (string) ($cell['time'] ?? ''); } elseif ($status === 'no') { $line[$column] = 'No'; } } $line[$lastColumn - 1] = (int) ($row['total'] ?? 0); $grid[] = $line; } if (!empty($this->rows)) { $totalLine = array_fill(0, $lastColumn, ''); $totalLine[0] = 'Total'; $sum = 0; foreach ($this->rows as $row) { $sum += (int) ($row['total'] ?? 0); } $totalLine[$lastColumn - 1] = $sum; $grid[] = $totalLine; } return $grid; } public function registerEvents(): array { return [ AfterSheet::class => function (AfterSheet $event) { $sheet = $event->sheet->getDelegate(); $identityCount = count(self::IDENTITY); $dayCount = count($this->days); $lastColumn = $identityCount + $dayCount * 2 + 1; $lastLetter = Coordinate::stringFromColumnIndex($lastColumn); $rowCount = count($this->rows); $lastRow = $rowCount ? $rowCount + 4 : 3; $sheet->getStyle('A1')->applyFromArray([ 'font' => ['bold' => true], 'alignment' => [ 'horizontal' => Alignment::HORIZONTAL_LEFT, 'vertical' => Alignment::VERTICAL_CENTER, ], ]); for ($column = 1; $column <= $identityCount; $column++) { $letter = Coordinate::stringFromColumnIndex($column); $sheet->mergeCells($letter . '2:' . $letter . '3'); } foreach ($this->days as $index => $day) { $start = Coordinate::stringFromColumnIndex($identityCount + $index * 2 + 1); $end = Coordinate::stringFromColumnIndex($identityCount + $index * 2 + 2); $sheet->mergeCells($start . '2:' . $end . '2'); } $sheet->mergeCells($lastLetter . '2:' . $lastLetter . '3'); $sheet->getStyle('A2:' . $lastLetter . '3')->applyFromArray([ 'font' => [ 'bold' => true, 'color' => ['rgb' => '000000'], ], 'fill' => [ 'fillType' => Fill::FILL_SOLID, 'startColor' => ['rgb' => '95B3D7'], ], 'alignment' => [ 'horizontal' => Alignment::HORIZONTAL_CENTER, 'vertical' => Alignment::VERTICAL_CENTER, 'wrapText' => true, ], ]); if ($rowCount > 0) { $dataEnd = $rowCount + 3; $sheet->getStyle('A4:' . $lastLetter . $dataEnd)->applyFromArray([ 'font' => [ 'color' => ['rgb' => '000000'], ], 'fill' => [ 'fillType' => Fill::FILL_SOLID, 'startColor' => ['rgb' => 'FFFFFF'], ], 'alignment' => [ 'horizontal' => Alignment::HORIZONTAL_CENTER, 'vertical' => Alignment::VERTICAL_CENTER, 'wrapText' => true, ], ]); foreach ([1, 3, 4, 6, 8] as $column) { $letter = Coordinate::stringFromColumnIndex($column); $sheet->getStyle($letter . '4:' . $letter . $dataEnd) ->getAlignment() ->setHorizontal(Alignment::HORIZONTAL_LEFT); } foreach ([2, 5, 7] as $column) { $letter = Coordinate::stringFromColumnIndex($column); for ($excelRow = 4; $excelRow <= $dataEnd; $excelRow++) { $value = $sheet->getCell($letter . $excelRow)->getValue(); if (is_int($value) || (is_string($value) && preg_match('/^-?\d+$/', $value))) { $sheet->getStyle($letter . $excelRow)->getNumberFormat()->setFormatCode('0'); } } } foreach ($this->rows as $rowIndex => $row) { $excelRow = $rowIndex + 4; $cells = is_array($row['days'] ?? null) ? $row['days'] : []; foreach ($this->days as $index => $day) { $column = $identityCount + $index * 2 + 1; $cell = is_array($cells[$day] ?? null) ? $cells[$day] : ['status' => 'blank']; $status = $cell['status'] ?? 'blank'; $start = Coordinate::stringFromColumnIndex($column); $end = Coordinate::stringFromColumnIndex($column + 1); if ($status === 'link' && !empty($cell['link'])) { $linkCell = $sheet->getCell($start . $excelRow); $linkCell->getHyperlink()->setUrl((string) $cell['link']); $sheet->getStyle($start . $excelRow)->applyFromArray([ 'font' => [ 'color' => ['rgb' => '0563C1'], 'underline' => Font::UNDERLINE_SINGLE, ], 'alignment' => [ 'horizontal' => Alignment::HORIZONTAL_CENTER, 'vertical' => Alignment::VERTICAL_CENTER, ], ]); } else { $sheet->mergeCells($start . $excelRow . ':' . $end . $excelRow); } } } $totalRow = $rowCount + 4; $sheet->getStyle('A' . $totalRow . ':' . $lastLetter . $totalRow)->applyFromArray([ 'font' => [ 'bold' => true, 'color' => ['rgb' => '000000'], ], 'fill' => [ 'fillType' => Fill::FILL_SOLID, 'startColor' => ['rgb' => '95B3D7'], ], 'alignment' => [ 'horizontal' => Alignment::HORIZONTAL_CENTER, 'vertical' => Alignment::VERTICAL_CENTER, ], ]); $sheet->getStyle('A' . $totalRow)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT); $sheet->getStyle($lastLetter . $totalRow)->getNumberFormat()->setFormatCode('0'); } $sheet->getStyle('A2:' . $lastLetter . $lastRow)->applyFromArray([ 'borders' => [ 'allBorders' => [ 'borderStyle' => Border::BORDER_THIN, 'color' => ['rgb' => '000000'], ], ], ]); for ($column = 1; $column <= $lastColumn; $column++) { $letter = Coordinate::stringFromColumnIndex($column); $sheet->getColumnDimension($letter)->setWidth($column <= $identityCount ? 22 : 14); } }, ]; } private function dayLabel(string $iso): string { $bits = explode('-', $iso); if (count($bits) !== 3) { return $iso; } return $bits[2] . '-' . $bits[1] . '-' . $bits[0]; } private function codeValue($value) { $text = trim((string) $value); if ($text !== '' && preg_match('/^-?\d+$/', $text)) { return (int) $text; } return $text; } }