feat: Make the fu**ing Salarie import works again
This commit is contained in:
parent
f156dd62ef
commit
7f53f2abf8
7 changed files with 27 additions and 186 deletions
|
|
@ -1,37 +0,0 @@
|
|||
<?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';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
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');
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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',
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
|
|
|||
|
|
@ -1,140 +0,0 @@
|
|||
<?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 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';
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
$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());
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@
|
|||
{{ __('Importer') }}
|
||||
</button>
|
||||
</div>
|
||||
<p>{{ __("La première ligne du fichier CSV doit être :") }} matricule;nom;prenom;date_debut;date_fin;date_fin_prevue;etablissement;site;service</p>
|
||||
@if (request()->session()->has('success'))
|
||||
<span class="fw-bold text-success">{{ request()->session()->get('success') }}</span>
|
||||
@endif
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue