Files
verifikator/app/Exports/KeseluruhanExport.php
T

153 lines
5.7 KiB
PHP

<?php
namespace App\Exports;
use App\Exports\Concerns\ForcesTextColumns;
use App\Http\Controllers\Perhitungan\Remunerasi\KeseluruhanController;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Fill;
class KeseluruhanExport implements FromArray, ShouldAutoSize, WithColumnFormatting, WithCustomValueBinder, WithEvents, WithMapping
{
use ForcesTextColumns;
/**
* Jumlah kolom tetap di depan (No., Nama, NIP, Jenis Pegawai, Unit Kerja) sebelum kolom
* bulanan P1 dimulai.
*/
private const FIXED_COLUMNS = ['No.', 'Nama', 'NIP', 'Jenis Pegawai', 'Unit Kerja'];
/**
* Kolom setelah blok bulanan P1 (tidak ikut di-expand per bulan).
*/
private const TRAILING_COLUMNS = ['Jumlah P2', 'Jumlah THR', 'Jumlah R13', 'Jumlah Publikasi', 'Total'];
private const BULAN_NAMES = [
1 => 'Januari', 2 => 'Februari', 3 => 'Maret', 4 => 'April', 5 => 'Mei', 6 => 'Juni',
7 => 'Juli', 8 => 'Agustus', 9 => 'September', 10 => 'Oktober', 11 => 'November', 12 => 'Desember',
];
private array $rows;
private int $bulanTerakhir;
protected function nipColumns(): array
{
return ['C'];
}
public function __construct(int $tahun, ?string $jenisPegawai, mixed $unit, bool $includePppk2025 = false, bool $includeP1Zero = false)
{
$controller = app(KeseluruhanController::class);
$this->rows = $controller->getRekapRows($tahun, $jenisPegawai, $unit, $includePppk2025, $includeP1Zero);
$this->bulanTerakhir = $controller->getBulanTerakhir($tahun);
}
public function array(): array
{
return $this->rows;
}
public function map($row): array
{
static $no = 0;
$no++;
$bulananValues = [];
for ($bulan = 1; $bulan <= $this->bulanTerakhir; $bulan++) {
$bulananValues[] = $row['jumlah_p1_bulanan'][$bulan] ?? 0;
}
return array_merge([
$no,
$row['nama'],
$row['nip'],
$row['jenis_pegawai'] === 'dosen' ? 'Dosen' : ($row['jenis_pegawai'] === 'tendik' ? 'Tendik' : '-'),
$row['nama_unit'],
], $bulananValues, [
$row['jumlah_p2'],
$row['jumlah_thr'],
$row['jumlah_r13'],
$row['jumlah_publikasi'],
$row['total'],
]);
}
public function columnFormats(): array
{
$accountingFormat = '_("Rp"\ * #,##0.00_);_("Rp"\ * \-#,##0.00_);_("Rp"\ * "-"??_);_(@_)';
$fixedCount = count(self::FIXED_COLUMNS);
$lastIndex = $fixedCount + $this->bulanTerakhir + count(self::TRAILING_COLUMNS);
$formats = ['C' => '@'];
for ($i = $fixedCount + 1; $i <= $lastIndex; $i++) {
$formats[Coordinate::stringFromColumnIndex($i)] = $accountingFormat;
}
return $formats;
}
/**
* Data (via array()+map()) ditulis mulai baris 1 karena export ini sengaja tidak memakai
* WithHeadings biasa. Di sini baris 1-2 disisipkan di atasnya untuk header 2 tingkat: baris 1
* berisi label grup ("Jumlah P1 (Januari - Sekarang)" yang di-merge selebar kolom bulanannya,
* dan label lain yang di-merge vertikal 2 baris), baris 2 berisi nama tiap bulan.
*/
public function registerEvents(): array
{
return [
AfterSheet::class => function (AfterSheet $event) {
$sheet = $event->sheet->getDelegate();
$sheet->insertNewRowBefore(1, 2);
$fixedCount = count(self::FIXED_COLUMNS);
foreach (self::FIXED_COLUMNS as $i => $label) {
$col = Coordinate::stringFromColumnIndex($i + 1);
$sheet->setCellValue($col.'1', $label);
$sheet->mergeCells($col.'1:'.$col.'2');
}
$p1StartCol = Coordinate::stringFromColumnIndex($fixedCount + 1);
$p1EndCol = Coordinate::stringFromColumnIndex($fixedCount + $this->bulanTerakhir);
$sheet->setCellValue($p1StartCol.'1', 'Jumlah P1 (Januari - Sekarang)');
$sheet->mergeCells($p1StartCol.'1:'.$p1EndCol.'1');
for ($bulan = 1; $bulan <= $this->bulanTerakhir; $bulan++) {
$col = Coordinate::stringFromColumnIndex($fixedCount + $bulan);
$sheet->setCellValue($col.'2', self::BULAN_NAMES[$bulan]);
}
foreach (self::TRAILING_COLUMNS as $i => $label) {
$col = Coordinate::stringFromColumnIndex($fixedCount + $this->bulanTerakhir + $i + 1);
$sheet->setCellValue($col.'1', $label);
$sheet->mergeCells($col.'1:'.$col.'2');
}
$lastCol = Coordinate::stringFromColumnIndex($fixedCount + $this->bulanTerakhir + count(self::TRAILING_COLUMNS));
$sheet->getStyle('A1:'.$lastCol.'2')->applyFromArray([
'font' => ['bold' => true, 'color' => ['rgb' => 'FFFFFF']],
'fill' => [
'fillType' => Fill::FILL_SOLID,
'startColor' => ['rgb' => '4472C4'],
],
'alignment' => [
'horizontal' => Alignment::HORIZONTAL_CENTER,
'vertical' => Alignment::VERTICAL_CENTER,
'wrapText' => true,
],
]);
},
];
}
}