201 lines
5.9 KiB
PHP
201 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Enums\LogType;
|
|
use App\Enums\SalarieSite;
|
|
use App\Models\Salarie;
|
|
use ErrorException;
|
|
use TypeError;
|
|
use Illuminate\Database\QueryException;
|
|
use App\Exceptions\AbortCsvImportException;
|
|
use App\Lib\Utils;
|
|
use App\Models\Magasin;
|
|
use Carbon\Carbon;
|
|
|
|
class CsvSalariesReader extends CsvJobs
|
|
{
|
|
/**
|
|
* This is for Laravel Console usage
|
|
* @var \Illuminate\Console\Command|null
|
|
*/
|
|
public $console = null;
|
|
|
|
/**
|
|
* The Laravel disk name
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $disk = 'salaries';
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
}
|
|
|
|
/**
|
|
* 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 $filepath */
|
|
$filepath = $disk->path($filename);
|
|
|
|
try {
|
|
$this->processFile($filepath);
|
|
} catch (ErrorException | TypeError | QueryException $e) {
|
|
$message = $e->getMessage();
|
|
if (get_class($e) === QueryException::class) {
|
|
$message = implode(' ', $e->errorInfo);
|
|
}
|
|
$this->log(
|
|
sprintf(
|
|
'File %s could not be parsed => %s on line %d',
|
|
$filename,
|
|
$message,
|
|
$e->getLine()
|
|
),
|
|
LogType::critical()
|
|
);
|
|
// Ignore the file.
|
|
continue;
|
|
} catch (AbortCsvImportException $e) {
|
|
$this->log(
|
|
sprintf(
|
|
'File %s could not be parsed => %s on line %d',
|
|
$filename,
|
|
$e->getMessage(),
|
|
$e->getLine()
|
|
),
|
|
LogType::error()
|
|
);
|
|
// Ignore the file.
|
|
continue;
|
|
}//end try
|
|
$this->moveToArchives($filename);
|
|
}//end foreach
|
|
|
|
// Clear old archives.
|
|
$this->clearOldArchives();
|
|
}
|
|
|
|
/**
|
|
* Read one file and save salarie for each line
|
|
*
|
|
* @param string $filepath
|
|
* @return void
|
|
*/
|
|
private function processFile(string $filepath)
|
|
{
|
|
$data = $this->parseCsv($filepath);
|
|
// First line is header (mandatory).
|
|
$header = array_flip(array_shift($data));
|
|
foreach ($data as $line) {
|
|
$matricule = $line[$header['Matricule']];
|
|
$prenom = $line[$header['Prénom']];
|
|
$nom = $line[$header['Nom']];
|
|
$start_date = Carbon::createFromFormat('d/m/Y', $line[$header['Date début']]);
|
|
$end_date = $line[$header['Date fin']] ?
|
|
$line[$header['Date fin']] : $line[$header['Date fin prévue']];
|
|
$etablissement = $line[$header['Etablissement']];
|
|
$site = $line[$header['Site']];
|
|
$service = $line[$header['Service']];
|
|
|
|
if ($end_date != '') {
|
|
$end_date = Carbon::createFromFormat('d/m/Y', $end_date);
|
|
} else {
|
|
$end_date = null;
|
|
}
|
|
|
|
|
|
$salarie = Salarie::firstOrNew(['matricule' => $matricule]);
|
|
$salarie->matricule = $matricule;
|
|
$salarie->nom = $nom;
|
|
$salarie->prenom = $prenom;
|
|
$salarie->start_date = $start_date;
|
|
$salarie->end_date = $end_date;
|
|
$salarie->etablissement = $etablissement;
|
|
$salarie->site = $site;
|
|
$salarie->service = $service;
|
|
$salarie->code = $salarie->code ?? $this->generateCode();
|
|
$salarie->save();
|
|
$this->setMagasin($salarie);
|
|
} //end foreach
|
|
}
|
|
|
|
/**
|
|
* Génère un code d'inscription
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function generateCode()
|
|
{
|
|
return Utils::generateRandomString(8);
|
|
}
|
|
|
|
/**
|
|
* Sets the fixed collection point for a salarie.
|
|
*
|
|
* @param \App\Models\Salarie $salarie
|
|
* @return void
|
|
*/
|
|
protected function setMagasin(Salarie $salarie)
|
|
{
|
|
if ($salarie->site == SalarieSite::ldccav()) {
|
|
// CAVOL.
|
|
$magasin = Magasin::find(3);
|
|
} elseif ($salarie->site == SalarieSite::dde()) {
|
|
// Dinde.
|
|
$magasin = Magasin::find(8);
|
|
} elseif ($salarie->site == SalarieSite::dpe()) {
|
|
// DPE.
|
|
$magasin = Magasin::find(9);
|
|
} elseif ($salarie->site == SalarieSite::cepacpe()) {
|
|
if (
|
|
$salarie->service == "CEPACP060"
|
|
|| $salarie->service == "CEPACP070"
|
|
|| $salarie->service == "CEPACP072"
|
|
) {
|
|
// CPE.
|
|
$magasin = Magasin::find(6);
|
|
} elseif ($salarie->service == "CEPACP065" || $salarie->service == "CEPACP066") {
|
|
// PLB.
|
|
$magasin = Magasin::find(10);
|
|
} else {
|
|
// CEPA.
|
|
$magasin = Magasin::find(10);
|
|
}
|
|
} else {
|
|
// Saint Laurent.
|
|
$magasin = Magasin::find(1);
|
|
} //end if
|
|
$salarie->magasin()->associate($magasin);
|
|
$salarie->save();
|
|
}
|
|
}
|