penjadwalan-ga/app/Exports/GaGenerationSheet.php

112 lines
3.3 KiB
PHP

<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Illuminate\Support\Collection;
class GaGenerationSheet implements FromCollection, WithTitle, WithHeadings, WithStyles, ShouldAutoSize
{
protected array $gaResult;
protected mixed $targetGen;
public function __construct(array $gaResult, mixed $targetGen = 0)
{
$this->gaResult = $gaResult;
$this->targetGen = $targetGen;
}
public function title(): string
{
return $this->targetGen === 'final' ? '3. Matriks Generasi Final' : '2. Matriks Gen 0 (Awal)';
}
public function headings(): array
{
$genTitle = $this->targetGen === 'final' ? 'GENERASI FINAL (HASIL CONVERGENCE)' : 'GENERASI KE-0 (INISIALISASI POPULASI ACAK)';
return [
["TABEL POPULASI & BREAKDOWN FITNESS - {$genTitle}"],
[],
[
'No Individu',
'Nilai Fitness',
'Formula Excel Fitness',
'Total Penalti',
'B1 (Guru)',
'B2 (Ruang)',
'B3 (Tersedia)',
'B4 (Kelas)',
'B5 (Level)',
'B6 (Sebaran)',
'B7 (Konsisten)',
'Kromosom Vector Gen [G, R, S, Hari_P]'
]
];
}
public function collection(): Collection
{
$genLogs = $this->gaResult['generationLogs'] ?? [];
$targetLog = null;
if ($this->targetGen === 'final') {
$targetLog = end($genLogs);
} else {
foreach ($genLogs as $log) {
if ($log['generation'] == $this->targetGen) {
$targetLog = $log;
break;
}
}
}
if (!$targetLog) {
return new Collection([['Data generasi tidak ditemukan']]);
}
$rows = [];
foreach ($targetLog['individus'] as $idx => $ind) {
$rowNum = $idx + 4; // Dimulai dari baris ke-4 Excel
$genesText = [];
if (!empty($ind['genesFormatted'])) {
foreach ($ind['genesFormatted'] as $gf) {
$genesText[] = $gf['text'];
}
}
$geneStr = implode(' ', $genesText);
$rows[] = [
'Individu[' . $ind['index'] . ']',
$ind['fitness'],
"=1/(1+D{$rowNum})", // Formula Excel Asli
$ind['penalty'],
'-', // Breakdown detail di baris Excel
'-',
'-',
'-',
'-',
'-',
'-',
$geneStr
];
}
return new Collection($rows);
}
public function styles(Worksheet $sheet)
{
$sheet->mergeCells('A1:L1');
$sheet->getStyle('A1')->getFont()->setBold(true)->setSize(12);
$sheet->getStyle('A3:L3')->getFont()->setBold(true);
$sheet->getStyle('A3:L3')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('E2E8F0');
return [];
}
}