Penambahan Detail P1 Export
This commit is contained in:
@@ -8,27 +8,48 @@ use Maatwebsite\Excel\Concerns\FromArray;
|
||||
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
|
||||
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
|
||||
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadings;
|
||||
use Maatwebsite\Excel\Concerns\WithEvents;
|
||||
use Maatwebsite\Excel\Concerns\WithMapping;
|
||||
use Maatwebsite\Excel\Concerns\WithStyles;
|
||||
use Maatwebsite\Excel\Events\AfterSheet;
|
||||
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
|
||||
use PhpOffice\PhpSpreadsheet\Style\Alignment;
|
||||
use PhpOffice\PhpSpreadsheet\Style\Fill;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
|
||||
class KeseluruhanExport implements FromArray, ShouldAutoSize, WithColumnFormatting, WithCustomValueBinder, WithHeadings, WithMapping, WithStyles
|
||||
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'];
|
||||
}
|
||||
|
||||
private array $rows;
|
||||
|
||||
public function __construct(int $tahun, ?string $jenisPegawai, mixed $unit)
|
||||
{
|
||||
$this->rows = app(KeseluruhanController::class)->getRekapRows($tahun, $jenisPegawai, $unit);
|
||||
$controller = app(KeseluruhanController::class);
|
||||
|
||||
$this->rows = $controller->getRekapRows($tahun, $jenisPegawai, $unit);
|
||||
$this->bulanTerakhir = $controller->getBulanTerakhir($tahun);
|
||||
}
|
||||
|
||||
public function array(): array
|
||||
@@ -36,72 +57,96 @@ class KeseluruhanExport implements FromArray, ShouldAutoSize, WithColumnFormatti
|
||||
return $this->rows;
|
||||
}
|
||||
|
||||
public function headings(): array
|
||||
{
|
||||
return [
|
||||
'No.',
|
||||
'Nama',
|
||||
'NIP',
|
||||
'Jenis Pegawai',
|
||||
'Unit Kerja',
|
||||
'Jumlah P1',
|
||||
'Jumlah P2',
|
||||
'Jumlah THR',
|
||||
'Jumlah R13',
|
||||
'Jumlah Publikasi',
|
||||
'Total',
|
||||
];
|
||||
}
|
||||
|
||||
public function map($row): array
|
||||
{
|
||||
static $no = 0;
|
||||
$no++;
|
||||
|
||||
return [
|
||||
$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'],
|
||||
$row['jumlah_p1'],
|
||||
], $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);
|
||||
|
||||
return [
|
||||
'C' => '@',
|
||||
'F' => $accountingFormat,
|
||||
'G' => $accountingFormat,
|
||||
'H' => $accountingFormat,
|
||||
'I' => $accountingFormat,
|
||||
'J' => $accountingFormat,
|
||||
'K' => $accountingFormat,
|
||||
];
|
||||
$formats = ['C' => '@'];
|
||||
for ($i = $fixedCount + 1; $i <= $lastIndex; $i++) {
|
||||
$formats[Coordinate::stringFromColumnIndex($i)] = $accountingFormat;
|
||||
}
|
||||
|
||||
return $formats;
|
||||
}
|
||||
|
||||
public function styles(Worksheet $sheet)
|
||||
/**
|
||||
* 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 [
|
||||
1 => [
|
||||
'font' => ['bold' => true, 'color' => ['rgb' => 'FFFFFF']],
|
||||
'fill' => [
|
||||
'fillType' => Fill::FILL_SOLID,
|
||||
'startColor' => ['rgb' => '4472C4'],
|
||||
],
|
||||
'alignment' => [
|
||||
'horizontal' => Alignment::HORIZONTAL_CENTER,
|
||||
'vertical' => Alignment::VERTICAL_CENTER,
|
||||
],
|
||||
],
|
||||
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,
|
||||
],
|
||||
]);
|
||||
},
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user