<?php
namespace App\Imports;
use App\Models\Companies;
use App\Models\Datalog;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Maatwebsite\Excel\Concerns\SkipsOnError;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Concerns\WithUpserts;
use Maatwebsite\Excel\Concerns\WithValidation;
use Throwable;
use Maatwebsite\Excel\Concerns\WithCustomCsvSettings;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Illuminate\Support\Facades\Validator;
use App\Events\NotificationEvent;
use App\Http\Traits\EventFormatter;
/**
* @property array $errors
*/
class CompaniesImport implements ToModel, WithChunkReading, ShouldQueue, WithStartRow, SkipsEmptyRows, WithUpserts, SkipsOnError
{
use Dispatchable, Queueable, Importable, EventFormatter;
private $headers = [];
/**
* @var Collection
*/
private $references = [];
/**
* @var mixed|null
*/
private $batchData;
private $errors = [];
public function __construct($headers, $batchInfo = null)
{
$this->headers = $headers;
$this->batchData = $batchInfo;
$this->references = Companies::all("id", "public_id")->pluck("public_id", "id");
}
public function chunkSize(): int
{
return 10000;
}
public function batchSize(): int
{
return 20;
}
public function setHeaders($headers)
{
$this->headers = $headers;
}
public function startRow(): int
{
return 2;
}
/**
* @param array $row
*
* @return Companies
*/
public function rules(): array
{
return [
'business_name' => 'required|string|max:255',
// 'public_id' => 'nullable|string',
// 'data_urn' => 'nullable|string',
'reg' => 'required|integer',
// 'type' => 'nullable|integer',
'website' => 'required|string|max:255',
'headcount' => 'required|integer',
'est_turnover' => 'required|integer',
'sic' => 'required|integer',
'sic_description' => 'required|string',
];
}
public function model(array $row)
{
$data = [
'id' => null,
"business_name"=>"",
"public_id" => "",
"data_urn" => "",
"reg" => null,
"website" => null,
"headcount" => 0,
"type" => 0,
"est_turnover" => 0,
"sic" => null,
"sic_description" => null,
"created_at" => now(),
"updated_at" => now()
];
if ($this->headers == "") {
return null;
}
foreach ($this->headers as $key => $header) {
$data[$header] = $row[$key];
}
$validator = Validator::make($data, $this->rules());
if ($validator->fails()) {
// Log the validation error and skip the iteration
\Log::error('companies Validation error: ' . $validator->errors()->first() . "(" . json_encode($row) . ")");
$this->notification([
"title" => 'Companies Validation Error',
"subtitle" => $validator->errors()->first() . "(" . json_encode($row) . ")",
"type" => 'light-success'
]);
return null;
}
// foreach ($this->headers as $key => $header) {
// $data[$header] = $row[$header];
// }
try {
$uniqid = env("COMPANY_PREFIX") . "/" . rand(1000000, 9999999);
while (in_array($uniqid, (array)$this->references, true)) {
$uniqid =env("COMPANY_PREFIX") . "/" . rand(1000000, 9999999);
$data['public_id'] = $uniqid;
}
$company = Companies::updateOrCreate([
"business_name" => $data['business_name']
],
$data
);
if ($company->wasRecentlyCreated) {
$company->public_id = $uniqid;
}
if ($this->batchData != null) {
$batch = $this->batchData;
// dd($batch);
$batch->company_id = $company->id;
// $batch->status = 1;
$databatch = Datalog::firstOrCreate(["company_id" => $company->id]);
$batch = $databatch->update((array)$batch);
$batch = null;
}
return $company;
} catch (Exception $e) {
\Log::error('companies errror: ' .$e->getMessage());
die($e->getMessage());
}
}
public function getErrors()
{
return $this->errors ?? [];
}
public function uniqueBy()
{
return ['business_name'];
}
public function onError(Throwable $e)
{
// TODO: Implement onError() method.
$this->errors[] = $e->getMessage();
}
}
No comments:
Post a Comment