ldc-portsalaries/app/Jobs/XSCVMepReader.php
2021-06-07 18:28:25 +02:00

160 lines
4.8 KiB
PHP

<?php
namespace App\Jobs;
use App\Enums\LogType;
use App\Exceptions\AbortCsvImportException;
use App\Exceptions\AbortRowImportException;
use App\Mail\OcbMissingBreederReminder;
use App\Models\OCBBreeder;
use App\Models\OCBWaitingList;
use App\Models\XmlBreeder;
use ErrorException;
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();
$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 = substr($noElev, strlen($noElev) - 7, strlen($noElev));
// Check if inuav exists
if (!OCBBreeder::where('inuavs', 'LIKE', sprintf('%%s%', $inuav))->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
}
$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');
}
}