Files
verifikator/app/Exports/RasioP2PoinExport.php
T
2026-08-07 10:47:09 +07:00

362 lines
15 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Exports;
use App\Models\Verifikator\View\Pegawai;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
class RasioP2PoinExport
{
protected array $rows;
/**
* @param array<string, mixed> $sec Data satu bulan (rows, kegiatan_split, total_poin_p2, p2_total_bruto, dst) dari RasioP2Controller::prepareViewData()
*/
public function __construct(
protected Pegawai $pegawai,
protected int $periode,
protected string $bulanNama,
protected array $sec,
) {
$this->rows = $sec['rows'];
}
public function export(): Spreadsheet
{
$spreadsheet = new Spreadsheet;
$spreadsheet->getProperties()
->setCreator('Verifikator System')
->setTitle('Data Poin P2 - '.$this->pegawai->nama)
->setSubject('Export Poin P2')
->setDescription('Detail poin kegiatan per kategori dan sub kategori beserta total poin dan nominal');
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle($this->sanitizeSheetName($this->bulanNama));
$this->fillSheetData($sheet);
return $spreadsheet;
}
protected function fillSheetData($sheet): void
{
$sheet->setCellValue('A1', 'Detail Poin P2 - '.$this->pegawai->nama);
$sheet->mergeCells('A1:G1');
$sheet->getStyle('A1')->getFont()->setBold(true)->setSize(13);
$sheet->setCellValueExplicit('A2', 'NIP: '.$this->pegawai->nip.' | Periode: '.$this->periode.' | Bulan: '.$this->bulanNama, DataType::TYPE_STRING);
$sheet->mergeCells('A2:G2');
$sheet->getStyle('A2')->getFont()->setItalic(true);
$headerRow = 4;
$headers = [
'A' => 'No.',
'B' => 'Kegiatan',
'C' => 'Poin Total',
'D' => 'Poin Remun',
'E' => 'Poin P2',
'F' => 'Nominal (Rp)',
'G' => 'Rumus Perhitungan Nominal',
];
foreach ($headers as $col => $label) {
$sheet->setCellValue($col.$headerRow, $label);
}
$sheet->getStyle('A'.$headerRow.':G'.$headerRow)->applyFromArray([
'font' => ['bold' => true, 'color' => ['rgb' => 'FFFFFF']],
'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => ['rgb' => '4472C4']],
'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER, 'vertical' => Alignment::VERTICAL_CENTER],
'borders' => ['allBorders' => ['borderStyle' => Border::BORDER_THIN, 'color' => ['rgb' => '000000']]],
]);
$row = $headerRow + 1;
$previousType = null;
$spacerRows = [];
foreach ($this->rows as $item) {
// Beri jarak satu baris kosong sebelum kategori baru, dan sebelum sub kategori
// baru yang bukan langsung setelah header kategorinya sendiri.
$needsSpacer = $previousType !== null && (
$item['type'] === 'cat'
|| ($item['type'] === 'sub' && $previousType !== 'cat')
);
if ($needsSpacer) {
$spacerRows[] = $row;
$row++;
}
match ($item['type']) {
'cat' => $this->writeCategoryRow($sheet, $row, $item),
'sub' => $this->writeSubcategoryRow($sheet, $row, $item),
'item' => $this->writeItemRow($sheet, $row, $item),
'stot' => $this->writeSummaryRow($sheet, $row, $item, 'FFFDE7', 'F9A825'),
'tot' => $this->writeSummaryRow($sheet, $row, $item, 'E8F5E9', '43A047', true),
default => null,
};
$previousType = $item['type'];
$row++;
}
$lastRow = $row - 1;
if ($lastRow >= $headerRow + 1) {
$sheet->getStyle('A'.($headerRow + 1).':G'.$lastRow)->applyFromArray([
'borders' => ['allBorders' => ['borderStyle' => Border::BORDER_THIN, 'color' => ['rgb' => 'CCCCCC']]],
]);
$sheet->getStyle('C'.($headerRow + 1).':F'.$lastRow)->getNumberFormat()->setFormatCode('#,##0.00');
}
foreach ($spacerRows as $spacerRow) {
$sheet->getStyle('A'.$spacerRow.':G'.$spacerRow)->applyFromArray([
'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => ['rgb' => 'E9ECEF']],
'borders' => ['allBorders' => ['borderStyle' => Border::BORDER_NONE]],
]);
$sheet->getRowDimension($spacerRow)->setRowHeight(18);
}
$this->writeSummarySection($sheet, $lastRow + 3);
foreach (range('A', 'G') as $column) {
$sheet->getColumnDimension($column)->setAutoSize(true);
}
}
/**
* Ringkasan total poin P2 & nominal per kategori di bagian bawah sheet, dengan kegiatan
* Penunjang dipecah menjadi "Pengali 660" (Tim Adhoc/Kepanitiaan dosen, tarif tetap
* Rp 660.000÷6) dan "Pengali Biasa" (proporsional terhadap nilai per poin bulan tersebut).
*/
protected function writeSummarySection($sheet, int $startRow): void
{
$row = $startRow;
$sheet->setCellValue('A'.$row, 'RINGKASAN TOTAL POIN & NOMINAL');
$sheet->mergeCells('A'.$row.':C'.$row);
$sheet->getStyle('A'.$row)->getFont()->setBold(true)->setSize(12);
$row += 2;
$headerRow = $row;
$sheet->setCellValue('A'.$headerRow, 'Kategori');
$sheet->mergeCells('A'.$headerRow.':B'.$headerRow);
$sheet->setCellValue('C'.$headerRow, 'Poin P2');
$sheet->setCellValue('D'.$headerRow, 'Nominal (Rp)');
$sheet->getStyle('A'.$headerRow.':D'.$headerRow)->applyFromArray([
'font' => ['bold' => true, 'color' => ['rgb' => 'FFFFFF']],
'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => ['rgb' => '37474F']],
'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER, 'vertical' => Alignment::VERTICAL_CENTER],
]);
$row++;
$writeSummaryLine = function (int $row, string $label, float $poinP2, float $nominal, bool $indent = false) use ($sheet): void {
$sheet->setCellValue('A'.$row, ($indent ? ' - ' : '').$label);
$sheet->mergeCells('A'.$row.':B'.$row);
$sheet->setCellValue('C'.$row, $poinP2);
$sheet->setCellValue('D'.$row, $nominal);
$sheet->getStyle('C'.$row.':D'.$row)->getNumberFormat()->setFormatCode('#,##0.00');
if ($indent) {
$sheet->getStyle('A'.$row)->getFont()->setItalic(true);
}
};
foreach ($this->rows as $item) {
if ($item['type'] !== 'tot' || str_starts_with($item['label'], 'Total Penunjang')) {
continue;
}
$label = preg_replace('/^Total\s+/', '', $item['label']);
$writeSummaryLine($row, $label, $item['pp'], $item['br']);
$row++;
}
$pengali660 = $this->sec['kegiatan_split']['pengali660'] ?? ['pp' => 0.0, 'br' => 0.0];
$pengaliBiasa = $this->sec['kegiatan_split']['pengaliBiasa'] ?? ['pp' => 0.0, 'br' => 0.0];
if ($pengali660['pp'] > 0 || $pengali660['br'] > 0 || $pengaliBiasa['pp'] > 0 || $pengaliBiasa['br'] > 0) {
$sheet->setCellValue('A'.$row, 'Kegiatan');
$sheet->mergeCells('A'.$row.':B'.$row);
$sheet->getStyle('A'.$row)->getFont()->setBold(true);
$row++;
$writeSummaryLine($row, 'Pengali 660', $pengali660['pp'], $pengali660['br'], indent: true);
$row++;
$writeSummaryLine($row, 'Pengali Biasa', $pengaliBiasa['pp'], $pengaliBiasa['br'], indent: true);
$row++;
}
$lastSummaryRow = $row - 1;
if ($lastSummaryRow >= $headerRow + 1) {
$sheet->getStyle('A'.($headerRow + 1).':D'.$lastSummaryRow)->applyFromArray([
'borders' => ['allBorders' => ['borderStyle' => Border::BORDER_THIN, 'color' => ['rgb' => 'CCCCCC']]],
]);
}
$row++;
$sheet->setCellValue('A'.$row, 'Total');
$sheet->mergeCells('A'.$row.':B'.$row);
$sheet->setCellValue('C'.$row, (float) $this->sec['total_poin_p2']);
$sheet->setCellValue('D'.$row, (float) $this->sec['bruto']);
$sheet->getStyle('C'.$row.':D'.$row)->getNumberFormat()->setFormatCode('#,##0.00');
$sheet->getStyle('A'.$row.':D'.$row)->applyFromArray([
'font' => ['bold' => true],
'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => ['rgb' => 'E8F5E9']],
'borders' => ['top' => ['borderStyle' => Border::BORDER_MEDIUM, 'color' => ['rgb' => '43A047']]],
]);
}
protected function writeCategoryRow($sheet, int $row, array $item): void
{
$color = ltrim($item['color'] ?? '#1565c0', '#');
$sheet->setCellValue('A'.$row, $item['label']);
$sheet->mergeCells('A'.$row.':G'.$row);
$sheet->getStyle('A'.$row)->applyFromArray([
'font' => ['bold' => true, 'color' => ['rgb' => 'FFFFFF']],
'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => ['rgb' => $color]],
]);
}
protected function writeSubcategoryRow($sheet, int $row, array $item): void
{
$label = $item['label'];
if (! is_null($item['limit'] ?? null)) {
$label .= ' (Maks: '.(int) $item['limit'].')';
}
$sheet->setCellValue('A'.$row, $label);
$sheet->mergeCells('A'.$row.':G'.$row);
$sheet->getStyle('A'.$row)->applyFromArray([
'font' => ['italic' => true, 'bold' => true, 'color' => ['rgb' => '1565C0']],
'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => ['rgb' => 'E3F2FD']],
]);
}
protected function writeItemRow($sheet, int $row, array $item): void
{
$sheet->setCellValue('A'.$row, $item['no']);
$sheet->setCellValue('B'.$row, $item['label']);
$sheet->setCellValue('C'.$row, $item['pt']);
$sheet->setCellValue('D'.$row, $item['pr']);
$sheet->setCellValue('E'.$row, $item['pp']);
$sheet->setCellValue('F'.$row, $item['br']);
$formula = $this->formulaText($item);
$sheet->setCellValue('G'.$row, $formula);
$sheet->getStyle('A'.$row)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
$sheet->getStyle('G'.$row)->getFont()->setItalic(true)->setSize(10);
$sheet->getStyle('G'.$row)->getAlignment()->setWrapText(true)->setVertical(Alignment::VERTICAL_CENTER);
$sheet->getStyle('A'.$row.':F'.$row)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
// Tinggi baris menyesuaikan jumlah baris teks keterangan, supaya tidak terpotong
$lineCount = substr_count($formula, "\n") + 1;
if ($lineCount > 1) {
$sheet->getRowDimension($row)->setRowHeight($lineCount * 15 + 4);
}
}
/**
* Jelaskan asal nominal per kegiatan: Poin P2 × nilai per poin efektif, lalu jelaskan lagi
* asal nilai per poin itu sendiri. Nilai per poin diturunkan dari nominal ÷ poin P2 itu
* sendiri (bukan konstanta), sehingga tetap benar untuk kegiatan Penunjang yang dihargai
* dengan tarif tetap Rp 660.000÷6 (bukan nilai per poin bulan berjalan seperti kegiatan lain).
*/
protected function formulaText(array $item): string
{
$pp = $item['pp'];
$br = $item['br'];
if ($pp > 0.0001) {
$rate = $br / $pp;
$baris = 'Result : '.number_format($pp, 2, ',', '.').' Poin × Rp '.number_format($rate, 2, ',', '.').' = Rp '.number_format($br, 2, ',', '.');
$nilaiPerPoin = $this->nilaiPerPoin();
if ($nilaiPerPoin > 0 && abs($rate - $nilaiPerPoin) < 0.5) {
$totalPoinFull = (float) ($this->sec['total_poin_p2'] ?? 0);
$totalPoinNonAdhoc = (float) ($this->sec['total_poin_p2_non_adhoc'] ?? $totalPoinFull);
$adhocPoin = max(0, $totalPoinFull - $totalPoinNonAdhoc);
$p2TotalBrutoNonAdhoc = (float) $this->sec['p2_total_bruto'];
$p2BrutoFull = (float) ($this->sec['bruto'] ?? $p2TotalBrutoNonAdhoc);
$p2Adhoc = max(0, $p2BrutoFull - $p2TotalBrutoNonAdhoc);
$baris .= "\nNilai Per Poin :";
if ($adhocPoin > 0.001) {
$baris .= "\n- (P2 Bruto - P2 Adhoc) ÷ (Total Poin P2 - Poin Adhoc)";
$baris .= "\n- (Rp. ".number_format($p2BrutoFull, 2, ',', '.').' - Rp. '.number_format($p2Adhoc, 2, ',', '.').') ÷ ('.number_format($totalPoinFull, 2, ',', '.').' Poin - '.number_format($adhocPoin, 2, ',', '.').' Poin)';
$baris .= "\n- Rp ".number_format($p2TotalBrutoNonAdhoc, 2, ',', '.').' ÷ '.number_format($totalPoinNonAdhoc, 2, ',', '.').' Poin';
} else {
$baris .= "\n- Total P2 Bruto ÷ Total Poin P2";
$baris .= "\n- Rp ".number_format($p2TotalBrutoNonAdhoc, 2, ',', '.').' ÷ '.number_format($totalPoinNonAdhoc, 2, ',', '.').' Poin';
}
$baris .= "\n- Rp ".number_format($nilaiPerPoin, 2, ',', '.');
} else {
$baris .= "\n(Tarif tetap Tim Adhoc/Kepanitiaan Adhoc dosen: Rp 660.000 ÷ 6, disesuaikan status kepegawaian)";
}
return $baris;
}
if (! empty($item['cut_reason'])) {
return $item['cut_reason'];
}
return '-';
}
protected function nilaiPerPoin(): float
{
// Poin Adhoc dikeluarkan dari pembagi — Adhoc dihargai tarif tetap sendiri (Rp 660.000÷6),
// bukan proporsional dari pool P2 Bruto, sehingga tidak boleh ikut mengencerkan nilai per poin.
$totalPoinP2 = (float) ($this->sec['total_poin_p2_non_adhoc'] ?? $this->sec['total_poin_p2'] ?? 0);
return $totalPoinP2 > 0 ? ((float) $this->sec['p2_total_bruto']) / $totalPoinP2 : 0.0;
}
protected function writeSummaryRow($sheet, int $row, array $item, string $bgColor, string $borderColor, bool $bold = false): void
{
$sheet->setCellValue('A'.$row, $item['label']);
$sheet->mergeCells('A'.$row.':B'.$row);
$sheet->setCellValue('C'.$row, $item['pt']);
$sheet->setCellValue('D'.$row, $item['pr']);
$sheet->setCellValue('E'.$row, $item['pp']);
$sheet->setCellValue('F'.$row, $item['br']);
$sheet->getStyle('A'.$row.':G'.$row)->applyFromArray([
'font' => ['bold' => true],
'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => ['rgb' => $bgColor]],
'borders' => ['top' => ['borderStyle' => $bold ? Border::BORDER_MEDIUM : Border::BORDER_THIN, 'color' => ['rgb' => $borderColor]]],
]);
}
protected function sanitizeSheetName(string $name): string
{
$name = str_replace(['\\', '/', '?', '*', '[', ']'], '', $name);
return mb_substr($name, 0, 31);
}
public function download(?string $filename = null): void
{
set_time_limit(300);
ini_set('memory_limit', '512M');
$spreadsheet = $this->export();
if (! $filename) {
$filename = 'poin-p2-'.date('Y-m-d-H-i-s').'.xlsx';
}
$writer = new Xlsx($spreadsheet);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$writer->save('php://output');
exit;
}
}