58 lines
1.7 KiB
PHP
58 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Imports;
|
|
|
|
use App\Models\Kriteria;
|
|
use App\Models\PenilaianWarga;
|
|
use App\Models\Warga;
|
|
use Illuminate\Support\Collection;
|
|
use Maatwebsite\Excel\Concerns\ToCollection;
|
|
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
|
|
|
class PenilaianWargaImport implements ToCollection, WithHeadingRow
|
|
{
|
|
protected $periode_id;
|
|
protected $kriteria;
|
|
|
|
public function __construct($periode_id)
|
|
{
|
|
$this->periode_id = $periode_id;
|
|
$this->kriteria = Kriteria::orderBy('kode')->get();
|
|
}
|
|
|
|
public function headingRow(): int
|
|
{
|
|
return 9;
|
|
}
|
|
|
|
public function collection(Collection $rows)
|
|
{
|
|
foreach ($rows as $row) {
|
|
if (!isset($row['nik'])) continue;
|
|
|
|
$warga = Warga::where('nik', $row['nik'])->first();
|
|
if (!$warga) continue; // Warga harus ada di database
|
|
|
|
foreach ($this->kriteria as $k) {
|
|
$kode = strtolower($k->kode); // Excel heading mapping is usually lowercase
|
|
if (isset($row[$kode])) {
|
|
$nilai = intval($row[$kode]); // "1 - Petani" menjadi 1, "3" menjadi 3
|
|
|
|
if ($nilai > 0 && $nilai <= 5) {
|
|
PenilaianWarga::updateOrCreate(
|
|
[
|
|
'warga_id' => $warga->id,
|
|
'periode_id' => $this->periode_id,
|
|
'kriteria_id' => $k->id,
|
|
],
|
|
[
|
|
'nilai' => $nilai,
|
|
]
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|