sigd/app/Imports/FormKehutananImport.php

47 lines
1.4 KiB
PHP

<?php
namespace App\Imports;
use App\Services\Activity\FormService;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class FormKehutananImport implements ToCollection, WithHeadingRow
{
protected $formService;
protected $sector, $code, $inventoryYear;
protected $lands;
protected $instansi;
public function __construct($formService, $sector, $code, $inventoryYear, $lands, $instansi = null)
{
$this->formService = $formService;
$this->sector = $sector;
$this->code = $code;
$this->inventoryYear = $inventoryYear ?? date('Y');
$this->lands = $lands;
$this->instansi = $instansi;
}
public function collection(Collection $rows)
{
$data = [];
for ($i = 0; $i < count($rows); $i++) {
$land1 = strtolower($this->lands[$i]);
$items = $rows[$i]->toArray();
$items = array_values($items);
for ($j = 0; $j < count($items); $j++) {
if ($j !== 0) {
$land2 = strtolower($this->lands[$j - 1]);
$data["{$land1}_{$land2}"] = getFormattedValue($items[$j]);
}
}
}
$this->formService->save($this->sector, $this->code, $this->inventoryYear, $data, $this->instansi);
}
}