From c332696024c4e037c5a46adefb656b73fb192a2c Mon Sep 17 00:00:00 2001 From: Vincent BRIDIER Date: Thu, 24 Oct 2024 16:25:12 +0200 Subject: [PATCH] feat: Make the fu**ing Salarie import works again --- app/Console/Commands/ImportSalaries.php | 37 ------ app/Enums/SalarieSite.php | 6 +- app/Imports/SalariesImport.php | 12 +- app/Jobs/CsvOrderWrite.php | 6 +- app/Jobs/CsvSalariesReader.php | 140 -------------------- app/Models/Salarie.php | 11 ++ resources/views/bo/salaries/index.blade.php | 1 + 7 files changed, 27 insertions(+), 186 deletions(-) delete mode 100644 app/Console/Commands/ImportSalaries.php delete mode 100644 app/Jobs/CsvSalariesReader.php diff --git a/app/Console/Commands/ImportSalaries.php b/app/Console/Commands/ImportSalaries.php deleted file mode 100644 index 514638d..0000000 --- a/app/Console/Commands/ImportSalaries.php +++ /dev/null @@ -1,37 +0,0 @@ -info('Running the Job, this could take some time.'); - $job = new JobCsvSalarieReader(); - $job->console = $this; - $job->handle(); - $this->info('Job completed'); - } -} diff --git a/app/Enums/SalarieSite.php b/app/Enums/SalarieSite.php index e9c757f..6fbc826 100644 --- a/app/Enums/SalarieSite.php +++ b/app/Enums/SalarieSite.php @@ -18,7 +18,11 @@ use Kwaadpepper\Enum\BaseEnumRoutable; * @method static self cepacpe() * @method static self dde() * @method static self dpe() - * + * @method static self sl() + * @method static self gdfroi() + * @method static self ldcplb() + * @method static self slela() + * @method static self tpwiss() */ class SalarieSite extends BaseEnumRoutable { diff --git a/app/Imports/SalariesImport.php b/app/Imports/SalariesImport.php index db1cb7d..da93569 100644 --- a/app/Imports/SalariesImport.php +++ b/app/Imports/SalariesImport.php @@ -42,8 +42,12 @@ class SalariesImport implements ToModel, WithCustomCsvSettings, WithHeadingRow, $end_date = Carbon::createFromFormat('d/m/Y', $end_date); } - $salarie = Salarie::firstOrNew(['matricule' => Arr::get($row, 'matricule')]); - $salarie->matricule = Arr::get($row, 'matricule'); + $matricule = Salarie::getFormattedMatricule(Arr::get($row, 'matricule')); + + $salarie = Salarie::firstOrNew( + ['matricule' => $matricule] + ); + $salarie->matricule = $matricule; $salarie->nom = Arr::get($row, 'nom'); $salarie->prenom = Arr::get($row, 'prenom'); $salarie->start_date = Carbon::createFromFormat('d/m/Y', Arr::get($row, 'date_debut')); @@ -54,7 +58,7 @@ class SalariesImport implements ToModel, WithCustomCsvSettings, WithHeadingRow, $salarie->code = $salarie->code ?? Salarie::generateRandomString(8, Salarie::query()); Salarie::setSalarieMagasin($salarie); $salarie->saveOrFail(); - $this->salariesImport = array_merge($this->salariesImport, [Arr::get($row, 'matricule')]); + $this->salariesImport = array_merge($this->salariesImport, [$matricule]); return $salarie; } @@ -66,7 +70,7 @@ class SalariesImport implements ToModel, WithCustomCsvSettings, WithHeadingRow, public function rules(): array { return [ - 'matricule' => 'required|numeric|digits:6', + 'matricule' => 'required|numeric', 'nom' => 'required|string|max:255', 'prenom' => 'required|string|max:255', 'etablissement' => 'required|string|max:255', diff --git a/app/Jobs/CsvOrderWrite.php b/app/Jobs/CsvOrderWrite.php index 132e1ea..3a942f9 100644 --- a/app/Jobs/CsvOrderWrite.php +++ b/app/Jobs/CsvOrderWrite.php @@ -87,13 +87,11 @@ class CsvOrderWrite extends CsvJobs $product = $orderline->product; // COLIS, KGM, PCE (faut il introduire un nouveau champs type unité de commande). $unité_commande = $product->order_unit ?? 'PCE'; - // Matricule RH avec 6 caractères numériques, préfixé de deux zéros. - $code_client = sprintf("00%s", $customer->salarie->matricule); $oneline = [ $product->foodProduct->no_art, // 21/01/2022 : suppression du pcb ici - $code_client, + $customer->salarie->matricule, $product->reference, "commande ferme", $unité_commande, @@ -113,7 +111,7 @@ class CsvOrderWrite extends CsvJobs $outputfilename = "BTQ_" . $this->order->reference . "_" . - $code_client . "_" . + $customer->salarie->matricule . "_" . $customer->salarie->nom . "_" . $customer->salarie->prenom . "_001.csv"; diff --git a/app/Jobs/CsvSalariesReader.php b/app/Jobs/CsvSalariesReader.php deleted file mode 100644 index 702edc4..0000000 --- a/app/Jobs/CsvSalariesReader.php +++ /dev/null @@ -1,140 +0,0 @@ -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) { - $end_date = $line[$header['Date fin']] ? - $line[$header['Date fin']] : $line[$header['Date fin prévue']]; - - $end_date = !empty($end_date) ? Carbon::createFromFormat('d/m/Y', $end_date) : null; - - // Matricule RH avec 6 caractères numériques. - $matricule = substr($line[$header['Matricule']], -6); - - $salarie = Salarie::firstOrNew(['matricule' => $matricule]); - $salarie->matricule = $matricule; - $salarie->nom = $line[$header['Nom']]; - $salarie->prenom = $line[$header['Prénom']]; - $salarie->start_date = Carbon::createFromFormat('d/m/Y', $line[$header['Date début']]); - $salarie->end_date = $end_date; - $salarie->etablissement = $line[$header['Etablissement']]; - $salarie->site = $line[$header['Site']]; - $salarie->service = $line[$header['Service']]; - $salarie->code = $salarie->code ?? $this->generateCode(); - Salarie::setSalarieMagasin($salarie); - $salarie->saveOrFail(); - } //end foreach - } - - /** - * Génère un code d'inscription - * - * @return string - */ - protected function generateCode() - { - return Salarie::generateRandomString(8, Salarie::all()); - } -} diff --git a/app/Models/Salarie.php b/app/Models/Salarie.php index 02ae621..5c84b84 100644 --- a/app/Models/Salarie.php +++ b/app/Models/Salarie.php @@ -111,6 +111,17 @@ class Salarie extends Model return $randomString; } + /** + * getFormattedMatricule method + * + * @param string $matricule + * @return string Digits only without leading zeros. + */ + public static function getFormattedMatricule(string $matricule): string + { + return ltrim(preg_replace('/[^\d]/', '', $matricule), '0'); + } + // * RELATIONSHIPS /** diff --git a/resources/views/bo/salaries/index.blade.php b/resources/views/bo/salaries/index.blade.php index 6e0e9c1..5d90686 100644 --- a/resources/views/bo/salaries/index.blade.php +++ b/resources/views/bo/salaries/index.blade.php @@ -19,6 +19,7 @@ {{ __('Importer') }} +

{{ __("La première ligne du fichier CSV doit être :") }} matricule;nom;prenom;date_debut;date_fin;date_fin_prevue;etablissement;site;service

@if (request()->session()->has('success')) {{ request()->session()->get('success') }} @endif