Files
verifikator/app/Exports/SimulasiJabatanTotalExport.php
T

75 lines
2.8 KiB
PHP

<?php
namespace App\Exports;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\WithTitle;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
/**
* Rekap Excel "Total" untuk menu Simulasi Jabatan: satu baris per pegawai
* (atau per unit pembayaran bila mode pecah aktif) berisi Total P2 (2022),
* P2 (2023), dan P2 (Excel), ditutup satu baris TOTAL keseluruhan.
*
* Nilai sudah dihitung di controller (dari dataset yang sama dengan tabel &
* kartu ringkasan) dan diterima di sini sebagai angka mentah.
*
* @param Collection<int, array{nama_pegawai:string, nip:string, jenis_pegawai:string, unit_kerja:string, unit_pembayaran?:string, p2_2022:float, p2_2023:float, p2_excel:float, keterangan:string}> $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]],
];
}
}