ldc-legaulois/app/Jobs/XSCVMepReader.php
Jérémy Munsch c5e3a834fa phpcbf fix
2021-09-15 14:27:41 +02:00

179 lines
5.5 KiB
PHP

<?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;
use Illuminate\Database\QueryException;
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();
$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)));
// 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()
) {
$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],
'cp' => $xmlBreeder->postal_code ?? '',
'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
} 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
}
$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');
}
}