$items */ class SimulasiJabatanTotalExport implements FromCollection, WithHeadings, WithStyles, ShouldAutoSize, WithColumnFormatting, WithTitle { private const RUPIAH_FORMAT = '_([$Rp-421]* #,##0.00_);_([$Rp-421]* (#,##0.00);_([$Rp-421]* "-"??_);_(@_)'; public function __construct(private readonly Collection $items, private readonly bool $splitUnit = false) {} public function collection(): Collection { $rows = $this->items->map(fn ($r) => array_values($r)); $total = collect(['p2_2022', 'p2_2023', 'p2_excel']) ->mapWithKeys(fn ($key) => [$key => $this->items->sum(fn ($r) => (float) ($r[$key] ?? 0))]); // Kolom teks sebelum angka: Nama, NIP, Jenis Pegawai, Unit Kerja (+ Unit Pembayaran). $lead = array_fill(0, $this->splitUnit ? 5 : 4, ''); $lead[0] = 'TOTAL'; return $rows->push(array_merge($lead, [$total['p2_2022'], $total['p2_2023'], $total['p2_excel'], ''])); } public function headings(): array { $head = ['Nama Pegawai', 'NIP', 'Jenis Pegawai', 'Unit Kerja']; if ($this->splitUnit) { $head[] = 'Unit Pembayaran'; } return array_merge($head, ['Total P2 (2022)', 'Total P2 (2023)', 'Total P2 (Excel)', 'Keterangan']); } public function columnFormats(): array { $cols = $this->splitUnit ? ['F', 'G', 'H'] : ['E', 'F', 'G']; return array_fill_keys($cols, self::RUPIAH_FORMAT); } public function title(): string { return 'Total P2'; } public function styles(Worksheet $sheet): array { return [ 1 => ['font' => ['bold' => true, 'color' => ['rgb' => 'FFFFFF']], 'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => ['rgb' => '20C997']]], $sheet->getHighestRow() => ['font' => ['bold' => true]], ]; } }