106 lines
2.6 KiB
PHP
106 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Exports;
|
|
|
|
use App\Models\HasilSeleksi;
|
|
use App\Models\Periode;
|
|
use Maatwebsite\Excel\Concerns\FromCollection;
|
|
use Maatwebsite\Excel\Concerns\WithHeadings;
|
|
use Maatwebsite\Excel\Concerns\WithMapping;
|
|
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
|
|
use Maatwebsite\Excel\Concerns\WithStyles;
|
|
// tambahan
|
|
use Maatwebsite\Excel\Concerns\WithColumnFormatting; // <-- TAMBAHKAN INI
|
|
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
|
|
|
class HasilSeleksiExport implements FromCollection, WithHeadings, WithMapping, ShouldAutoSize, WithStyles, WithColumnFormatting
|
|
{
|
|
/**
|
|
* @return \Illuminate\Support\Collection
|
|
*/
|
|
|
|
protected $periode_id;
|
|
protected $status;
|
|
protected $limit;
|
|
|
|
public function __construct($periode_id, $status = null, $limit = null)
|
|
{
|
|
$this->periode_id = $periode_id;
|
|
$this->status = $status;
|
|
$this->limit = $limit;
|
|
}
|
|
|
|
/**
|
|
* Ambil koleksi data berdasarkan filter
|
|
*/
|
|
public function collection()
|
|
{
|
|
$query = HasilSeleksi::with('warga')
|
|
->where('periode_id', $this->periode_id)
|
|
->orderBy('ranking', 'asc');
|
|
|
|
// Filter berdasarkan status keputusan
|
|
if ($this->status) {
|
|
$query->where('status_keputusan', $this->status);
|
|
}
|
|
|
|
// Filter berdasarkan kuota periode
|
|
if ($this->limit == 'kuota') {
|
|
$periode = Periode::find($this->periode_id);
|
|
if ($periode) {
|
|
$query->take($periode->kuota);
|
|
}
|
|
}
|
|
|
|
return $query->get();
|
|
}
|
|
|
|
/**
|
|
* Header Excel
|
|
*/
|
|
public function headings(): array
|
|
{
|
|
return [
|
|
'Ranking',
|
|
'Nama Warga',
|
|
'NIK',
|
|
'Skor Akhir (V)',
|
|
'Status Keputusan'
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Mapping Data
|
|
*/
|
|
public function map($hasil): array
|
|
{
|
|
return [
|
|
$hasil->ranking,
|
|
$hasil->warga->nama,
|
|
$hasil->warga->nik . ' ', // Tambah spasi agar NIK tidak jadi format scientific (E+) di Excel
|
|
(float) $hasil->skor_akhir,
|
|
// number_format($hasil->skor_akhir, 8),
|
|
$hasil->status_keputusan,
|
|
];
|
|
}
|
|
|
|
//menampilkan 4 digit
|
|
public function columnFormats(): array
|
|
{
|
|
return [
|
|
'D' => '0.0000', // <-- Mengunci Kolom D (Skor Akhir) agar selalu menampilkan 4 angka di belakang koma
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Styling sederhana (Opsional: Membuat header Bold)
|
|
*/
|
|
public function styles(Worksheet $sheet)
|
|
{
|
|
return [
|
|
1 => ['font' => ['bold' => true]],
|
|
];
|
|
}
|
|
}
|