ldc-legaulois/app/Jobs/XSCVMepReader.php

180 lines
5.5 KiB
PHP
Raw Normal View History

2021-04-12 14:30:53 +02:00
<?php
namespace App\Jobs;
use App\Enums\LogType;
use App\Exceptions\AbortCsvImportException;
use App\Exceptions\AbortRowImportException;
use App\Models\OCBBreeder;
use App\Models\OCBWaitingList;
use App\Models\XmlBreeder;
use ErrorException;
2021-05-10 09:49:39 +02:00
use Illuminate\Database\QueryException;
2021-04-12 14:30:53 +02:00
use Illuminate\Support\Carbon;
use League\Csv\Reader;
use TypeError;
class XSCVMepReader extends XArcadiaFlux
{
/**
* The Laravel disk name
*
* @var string
*/
protected $disk = 'mise_en_place';
protected $expectedArray = [
"No Eleveur",
"Code mise en place LDC",
"Opa",
"Vivant",
"Numero semaine de sortie",
"Quantite Initiale",
"Date mise en place",
"Couvoir",
"Code Lot OPA",
"Nombre de volaille mises en place",
"Quantite Nouvelle",
"Libelle Opa",
"Libelle Vivant",
"Nom Prenom Eleveur",
"Code Société",
"Nom Société"
];
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
/** @var Filesystem */
$disk = $this->getDisk();
$this->log('Démarrage du traitement');
$files = $disk->files();
$ocbBreeders = OCBBreeder::all();
2021-04-12 14:30:53 +02:00
$progress = 1;
$total = count(array_filter($files, function ($filename) {
if ($this->fileHasToBeIgnored($filename)) {
return false;
}
return true;
}));
foreach ($files as $filename) {
if ($this->fileHasToBeIgnored($filename)) {
continue;
}
$this->log(sprintf(' traitement %d sur %s ', $progress++, $total));
/** @var string $data */
$data = $disk->get($filename);
//load the CSV document from a file path
$csv = Reader::createFromString($data);
$csv->setDelimiter(';');
$csv->setHeaderOffset(0);
$header = $csv->getHeader();
$records = $csv->getRecords();
try {
$this->assertHeaderIsAsExpected($header);
foreach ($records as $record) {
$noElev = $record['No Eleveur'];
$inuav = trim(substr($noElev, strlen($noElev) - 7, strlen($noElev)));
2021-04-12 14:30:53 +02:00
// Check if inuav exists
// attemps to fix weird issue on ocb waiting list
if (
!$ocbBreeders->filter(function (OCBBreeder $breeder) use ($inuav) {
return in_array($inuav, $breeder->inuavs);
})->count()
) {
2021-04-12 14:30:53 +02:00
$iWL = OCBWaitingList::where('inuav_code', $inuav)->firstOrNew();
$iWL->inuav_code = $inuav;
$NP = explode(' ', $record['Nom Prenom Eleveur'], 2);
// try to find more info with xml_breeders
$xmlBreeder = XmlBreeder::where('inuav_code', $inuav)->first();
$iWL->fill([
'date_mep' => Carbon::createFromFormat('d/m/Y', $record['Date mise en place'])
->toDateTimeString(),
'last_name' => $NP[0],
'first_name' => $NP[1],
2021-04-12 15:39:59 +02:00
'cp' => $xmlBreeder->postal_code ?? '',
2021-04-12 14:30:53 +02:00
'city' => $xmlBreeder->city ?? '',
'inuav' => $inuav
]);
$iWL->save();
}
}
} catch (ErrorException | TypeError $e) {
$this->log(
sprintf(
'File %s could not be parsed => %s on line %d',
$filename,
$e->getMessage(),
$e->getLine()
),
LogType::critical()
);
continue; // ignore the file
} catch (AbortCsvImportException $e) {
$this->log(
sprintf(
'File %s could not be parsed => %s on line %d',
$filename,
$e->getMessage(),
$e->getLine()
),
LogType::error()
);
continue; // ignore the file
2021-05-10 09:49:39 +02:00
} catch (QueryException $e) {
// this happens because of concurent jobs
$this->log(
sprintf(
'xmlBreeder %s could not be saved (already exists)',
$filename,
$e->getMessage(),
$e->getLine()
),
LogType::debug()
);
continue; // ignore the file
2021-04-12 14:30:53 +02:00
}
$this->moveToArchives($filename);
}
// Clear old archives
$this->clearOldArchives();
}
protected function assertHeaderIsAsExpected(array $header)
{
if ($this->expectedArray !== $header) {
throw new AbortRowImportException(trans('CSV Header does not match expected'));
}
}
/**
* Is the file to be ignored
*
* @param string $filename
* @return boolean
*/
protected function fileHasToBeIgnored(string $filename): bool
{
return in_array($filename, $this->ignoreFiles) or !$this->checkExtension($filename, 'csv');
}
}