table salaries, import depuis un csv
This commit is contained in:
parent
460d8e2cff
commit
b49c5b25cb
9 changed files with 273 additions and 0 deletions
50
app/Console/Commands/ImportSalaries.php
Normal file
50
app/Console/Commands/ImportSalaries.php
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use App\Jobs\CsvSalariesReader as JobCsvSalarieReader;
|
||||||
|
|
||||||
|
class ImportSalaries extends Command
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'import:salaries';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Import Salaries, pour provision des clients';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new command instance.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$this->info('Running the Job, this could take some time.');
|
||||||
|
$job = new JobCsvSalarieReader();
|
||||||
|
$job->console = $this;
|
||||||
|
$job->handle();
|
||||||
|
$this->info('Job completed');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
10
app/Exceptions/AbortCsvImportException.php
Normal file
10
app/Exceptions/AbortCsvImportException.php
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Exceptions;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
class AbortCsvImportException extends Exception
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
35
app/Jobs/CsvJobs.php
Normal file
35
app/Jobs/CsvJobs.php
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Jobs;
|
||||||
|
|
||||||
|
abstract class CsvJobs extends XArcadiaFlux
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* parse csv file
|
||||||
|
*/
|
||||||
|
protected function parseCsv($csvFile) {
|
||||||
|
$file_handle = fopen($csvFile, 'r');
|
||||||
|
while (!feof($file_handle)) {
|
||||||
|
$line_of_text[] = fgetcsv($file_handle, 0, ";");
|
||||||
|
if(!end($line_of_text)) {
|
||||||
|
//supprime les lignes vides du csv
|
||||||
|
array_pop($line_of_text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fclose($file_handle);
|
||||||
|
return $line_of_text;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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');
|
||||||
|
}
|
||||||
|
}
|
||||||
111
app/Jobs/CsvSalariesReader.php
Normal file
111
app/Jobs/CsvSalariesReader.php
Normal file
|
|
@ -0,0 +1,111 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Jobs;
|
||||||
|
|
||||||
|
use App\Enums\LogType;
|
||||||
|
use App\Models\Salarie;
|
||||||
|
use ErrorException;
|
||||||
|
use TypeError;
|
||||||
|
use Illuminate\Database\QueryException;
|
||||||
|
use App\Exceptions\AbortCsvImportException;
|
||||||
|
use Illuminate\Contracts\Filesystem\Filesystem;
|
||||||
|
|
||||||
|
class CsvSalariesReader extends CsvJobs
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is for Laravel Console usage
|
||||||
|
*/
|
||||||
|
public $console = null;
|
||||||
|
|
||||||
|
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 $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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
23
app/Models/Salarie.php
Normal file
23
app/Models/Salarie.php
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Salarie extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attributes that are mass assignable.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $fillable = [
|
||||||
|
'matricule',
|
||||||
|
'nom',
|
||||||
|
'prenom',
|
||||||
|
'code'
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
@ -69,6 +69,11 @@ return [
|
||||||
'driver' => 'local',
|
'driver' => 'local',
|
||||||
'root' => storage_path('inco'),
|
'root' => storage_path('inco'),
|
||||||
],
|
],
|
||||||
|
|
||||||
|
'salaries' => [
|
||||||
|
'driver' => 'local',
|
||||||
|
'root' => storage_path('salaries'),
|
||||||
|
],
|
||||||
],
|
],
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
class CreateSalariesTable extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::create('salaries', function (Blueprint $table) {
|
||||||
|
$table->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');
|
||||||
|
}
|
||||||
|
}
|
||||||
1
storage/.gitignore
vendored
1
storage/.gitignore
vendored
|
|
@ -1 +1,2 @@
|
||||||
inco/*
|
inco/*
|
||||||
|
salaries/*
|
||||||
|
|
|
||||||
0
storage/salaries/.gitkeep
Normal file
0
storage/salaries/.gitkeep
Normal file
Loading…
Add table
Reference in a new issue