Files
verifikator/app/Exports/KeseluruhanCompareExport.php

209 lines
8.2 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;
/**
* Export Keseluruhan mode perbandingan: P1 per bulan, Total, Total Pajak, dan Total Netto ditampilkan
* dua kolom (Database & Dawai). Pada baris yang ada selisih, kolom Jumlah P1, Total Pajak, dan Total Netto
* diberi latar kuning, dan bulan P1 yang berselisih diberi latar orange.
*/
class KeseluruhanCompareExport implements FromArray, ShouldAutoSize, WithColumnFormatting, WithCustomValueBinder, WithEvents, WithMapping
{
use ForcesTextColumns;
private const FIXED_COLUMNS = ['No.', 'Nama', 'NIP', 'Jenis Pegawai', 'Unit Kerja'];
private const SINGLE_COLUMNS = ['Jumlah P2', 'Jumlah THR', 'Jumlah R13', 'Jumlah Publikasi'];
private const PAIR_COLUMNS = ['Total', 'Total Pajak', 'Total Netto'];
private const HEADER_ROWS = 3;
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;
private int $no = 0;
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->getCompareRows($tahun, $jenisPegawai, $unit, $includePppk2025, $includeP1Zero);
$this->bulanTerakhir = $controller->getBulanTerakhir($tahun);
}
public function array(): array
{
return $this->rows;
}
public function map($row): array
{
$this->no++;
$bulanan = [];
for ($bulan = 1; $bulan <= $this->bulanTerakhir; $bulan++) {
$bulanan[] = $row['p1_bulanan_db'][$bulan] ?? 0;
$bulanan[] = $row['p1_bulanan_dawai'][$bulan] ?? 0;
}
return array_merge([
$this->no,
$row['nama'],
$row['nip'],
$row['jenis_pegawai'] === 'dosen' ? 'Dosen' : ($row['jenis_pegawai'] === 'tendik' ? 'Tendik' : '-'),
$row['nama_unit'],
], $bulanan, [
$row['jumlah_p2'],
$row['jumlah_thr'],
$row['jumlah_r13'],
$row['jumlah_publikasi'],
$row['total_db'],
$row['total_dawai'],
$row['total_pajak_db'],
$row['total_pajak_dawai'],
$row['total_netto_db'],
$row['total_netto_dawai'],
]);
}
private function lastColumnIndex(): int
{
return count(self::FIXED_COLUMNS) + 2 * $this->bulanTerakhir + count(self::SINGLE_COLUMNS) + 2 * count(self::PAIR_COLUMNS);
}
public function columnFormats(): array
{
$accountingFormat = '_("Rp"\ * #,##0.00_);_("Rp"\ * \-#,##0.00_);_("Rp"\ * "-"??_);_(@_)';
$formats = ['C' => '@'];
for ($i = count(self::FIXED_COLUMNS) + 1; $i <= $this->lastColumnIndex(); $i++) {
$formats[Coordinate::stringFromColumnIndex($i)] = $accountingFormat;
}
return $formats;
}
/**
* Header 3 tingkat disisipkan di atas data: baris 1 = grup ("Jumlah P1 (Januari - Sekarang)", Total, dst.),
* baris 2 = nama bulan, baris 3 = "Database" / "Dawai". Setelah itu baris dengan selisih diberi warna kuning.
*/
public function registerEvents(): array
{
return [
AfterSheet::class => function (AfterSheet $event) {
$sheet = $event->sheet->getDelegate();
$sheet->insertNewRowBefore(1, self::HEADER_ROWS);
$col = fn (int $i) => Coordinate::stringFromColumnIndex($i);
$fixedCount = count(self::FIXED_COLUMNS);
foreach (self::FIXED_COLUMNS as $i => $label) {
$sheet->setCellValue($col($i + 1).'1', $label);
$sheet->mergeCells($col($i + 1).'1:'.$col($i + 1).'3');
}
$p1Start = $fixedCount + 1;
$sheet->setCellValue($col($p1Start).'1', 'Jumlah P1 (Januari - Sekarang)');
$sheet->mergeCells($col($p1Start).'1:'.$col($fixedCount + 2 * $this->bulanTerakhir).'1');
for ($bulan = 1; $bulan <= $this->bulanTerakhir; $bulan++) {
$start = $fixedCount + 2 * ($bulan - 1) + 1;
$sheet->setCellValue($col($start).'2', self::BULAN_NAMES[$bulan]);
$sheet->mergeCells($col($start).'2:'.$col($start + 1).'2');
$sheet->setCellValue($col($start).'3', 'Database');
$sheet->setCellValue($col($start + 1).'3', 'Dawai');
}
$idx = $fixedCount + 2 * $this->bulanTerakhir + 1;
foreach (self::SINGLE_COLUMNS as $label) {
$sheet->setCellValue($col($idx).'1', $label);
$sheet->mergeCells($col($idx).'1:'.$col($idx).'3');
$idx++;
}
foreach (self::PAIR_COLUMNS as $label) {
$sheet->setCellValue($col($idx).'1', $label);
$sheet->mergeCells($col($idx).'1:'.$col($idx + 1).'2');
$sheet->setCellValue($col($idx).'3', 'Database');
$sheet->setCellValue($col($idx + 1).'3', 'Dawai');
$idx += 2;
}
$lastCol = $col($this->lastColumnIndex());
$sheet->getStyle('A1:'.$lastCol.self::HEADER_ROWS)->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,
],
]);
// Stabilo kuning hanya pada blok Jumlah P1, Total Pajak, dan Total Netto; stabilo orange
// pada pasangan kolom bulan P1 yang nilainya berbeda antara Database dan Dawai.
$p1End = $fixedCount + 2 * $this->bulanTerakhir;
$pairStart = $p1End + count(self::SINGLE_COLUMNS) + 1;
$blokKuning = [
[$p1Start, $p1End],
[$pairStart + 2, $pairStart + 3],
[$pairStart + 4, $pairStart + 5],
];
$warnai = function (int $baris, int $dari, int $sampai, string $rgb) use ($sheet, $col) {
$sheet->getStyle($col($dari).$baris.':'.$col($sampai).$baris)->getFill()
->setFillType(Fill::FILL_SOLID)
->getStartColor()->setRGB($rgb);
};
foreach ($this->rows as $i => $row) {
if (! $row['ada_selisih']) {
continue;
}
$baris = self::HEADER_ROWS + $i + 1;
foreach ($blokKuning as [$dari, $sampai]) {
$warnai($baris, $dari, $sampai, 'FFFF00');
}
for ($bulan = 1; $bulan <= $this->bulanTerakhir; $bulan++) {
if (abs(($row['p1_bulanan_db'][$bulan] ?? 0) - ($row['p1_bulanan_dawai'][$bulan] ?? 0)) >= 0.01) {
$start = $fixedCount + 2 * ($bulan - 1) + 1;
$warnai($baris, $start, $start + 1, 'FFC000');
}
}
}
},
];
}
}