QR_code_generator/app/Imports/CompaniesImport.php

44 lines
1.4 KiB
PHP

<?php
namespace App\Imports;
use App\Models\Companies\Company;
use App\Models\Companies\CompanyType;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class CompaniesImport extends Import implements ToCollection, WithHeadingRow {
public array $mapper = [
'name' => ['prop' => 'nazvanie_organizacii', 'required' => true],
'inn' => ['prop' => 'inn_organizacii'],
'email' => ['prop' => 'email'],
'phone' => ['prop' => 'telefon'],
'types' => ['prop' => 'vid_organizacii']
];
public function collection(Collection $collection) {
$collection->each(function($row) {
if ($row = $this->mapData($row)) $this->importRow($row);
});
}
public function importRow($row) {
$row['inn'] = $row['inn'] ?? $row['name'];
$row['types'] = $this->translateTypes($row['types'] ?? '');
Company::getByData($row);
}
public function translateTypes(string $types): array {
$aTypes = [
'изготовитель' => CompanyType::PRODUCER,
'проектировщик' => CompanyType::DESIGNER
];
return collect(explode(';', $types))->map(function($type) use($aTypes) {
return $aTypes[mb_strtolower(trim($type))] ?? null;
})->filter(function($type) {return $type;})->all();
}
}