diff --git a/app/Console/Commands/ImportSalaries.php b/app/Console/Commands/ImportSalaries.php new file mode 100644 index 0000000..eed40e3 --- /dev/null +++ b/app/Console/Commands/ImportSalaries.php @@ -0,0 +1,50 @@ +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/Exceptions/AbortCsvImportException.php b/app/Exceptions/AbortCsvImportException.php new file mode 100644 index 0000000..065bd9c --- /dev/null +++ b/app/Exceptions/AbortCsvImportException.php @@ -0,0 +1,10 @@ +ignoreFiles) or !$this->checkExtension($filename, 'csv'); + } +} diff --git a/app/Jobs/CsvSalariesReader.php b/app/Jobs/CsvSalariesReader.php new file mode 100644 index 0000000..572ba67 --- /dev/null +++ b/app/Jobs/CsvSalariesReader.php @@ -0,0 +1,111 @@ +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 */ + $filepath = $disk->path($filename); + + try { + $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['prenom']]; + $nom = $line[$header['nom']]; + $salarie = Salarie::firstOrNew(['matricule' => $matricule ]); + $salarie->matricule = $matricule; + $salarie->nom = $nom; + $salarie->prenom = $prenom; + $salarie->save(); + } + } 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() + ); + 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(); + } + +} diff --git a/app/Models/Salarie.php b/app/Models/Salarie.php new file mode 100644 index 0000000..cf0c1f7 --- /dev/null +++ b/app/Models/Salarie.php @@ -0,0 +1,23 @@ + 'local', 'root' => storage_path('inco'), ], + + 'salaries' => [ + 'driver' => 'local', + 'root' => storage_path('salaries'), + ], ], /* diff --git a/database/migrations/2021_07_20_141844_create_salaries_table.php b/database/migrations/2021_07_20_141844_create_salaries_table.php new file mode 100644 index 0000000..82a2516 --- /dev/null +++ b/database/migrations/2021_07_20_141844_create_salaries_table.php @@ -0,0 +1,38 @@ +id(); + //liaison à la table customers + $table->foreignId('customers_id')->nullable()->constrained('customers'); + $table->string('matricule'); + $table->string('nom')->nullable(); + $table->string('prenom')->nullable(); + $table->string('code')->nullable(); + + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('salaries'); + } +} diff --git a/storage/.gitignore b/storage/.gitignore index e3a4197..771e253 100644 --- a/storage/.gitignore +++ b/storage/.gitignore @@ -1 +1,2 @@ inco/* +salaries/* diff --git a/storage/salaries/.gitkeep b/storage/salaries/.gitkeep new file mode 100644 index 0000000..e69de29