MEP Flux reader

This commit is contained in:
Jérémy Munsch 2021-04-12 14:30:53 +02:00
parent aab47891fc
commit 5a61040804
6 changed files with 273 additions and 1 deletions

View file

@ -0,0 +1,38 @@
<?php
namespace App\Console\Commands;
use App\Jobs\XSCVMepReader as JobsXSCVMepReader;
use Illuminate\Console\Command;
class XSCVMepReader extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'csv:mep';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Run the XSCVMepReader manually';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$this->info('Running the Job, this could take some time.');
$job = new JobsXSCVMepReader();
$job->console = $this;
$job->handle();
$this->info('Job completed');
}
}

View file

@ -10,6 +10,7 @@ use App\Console\Commands\UserCreate;
use App\Console\Commands\XmlEleveursReader;
use App\Console\Commands\XmlIncoReader;
use App\Console\Commands\XmlTracaReader;
use App\Console\Commands\XSCVMepReader;
use App\Jobs\DeleteOldMessages;
use App\Jobs\DeleteOldNotifications;
use App\Jobs\DeleteOrders;
@ -18,6 +19,7 @@ use App\Jobs\UpdateReductions as JobsUpdateReductions;
use App\Jobs\XmlEleveursReader as JobsXmlEleveursReader;
use App\Jobs\XmlIncoReader as JobsXmlIncoReader;
use App\Jobs\XmlTracaReader as JobsXmlTracaReader;
use App\Jobs\XSCVMepReader as JobsXSCVMepReader;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
@ -37,7 +39,8 @@ class Kernel extends ConsoleKernel
XmlTracaReader::class,
UserCreate::class,
UserChangePassword::class,
RefreshSiteMap::class
RefreshSiteMap::class,
XSCVMepReader::class
];
/**
@ -56,5 +59,6 @@ class Kernel extends ConsoleKernel
$schedule->job(JobsXmlTracaReader::class)->dailyAt('15:15');
$schedule->job(JobsXmlEleveursReader::class)->dailyAt('15:30');
$schedule->job(JobsRefreshSiteMap::class)->dailyAt('15:45');
$schedule->job(JobsXSCVMepReader::class)->dailyAt('16:00');
}
}

View file

@ -0,0 +1,9 @@
<?php
namespace App\Exceptions;
use Exception;
class AbortCsvImportException extends Exception
{
}

160
app/Jobs/XSCVMepReader.php Normal file
View file

@ -0,0 +1,160 @@
<?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 ?? '',
'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');
}
}

View file

@ -0,0 +1,20 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class OCBWaitingList extends Model
{
protected $table = 'ocb_breeder_inauv_waiting_list';
protected $fillable = [
'inuav_code',
'first_name',
'last_name',
'cp',
'city',
'date_mep'
];
}

View file

@ -0,0 +1,41 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateOcbBreederInauvWaitingListTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('ocb_breeder_inauv_waiting_list', function (Blueprint $table) {
$table->id();
$table->string('inuav_code', 10)->unique();
$table->string('first_name', 50);
$table->string('last_name', 50);
$table->string('cp', 10);
$table->string('city', 40);
$table->date('date_mep');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('ocb_breeder_inauv_waiting_list');
}
}