100 lines
2.9 KiB
PHP
100 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Imports;
|
|
|
|
use App\Models\Employee;
|
|
use App\Models\Site;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Maatwebsite\Excel\Concerns\ToCollection;
|
|
use Maatwebsite\Excel\Concerns\Importable;
|
|
use Maatwebsite\Excel\Concerns\SkipsOnFailure;
|
|
use Maatwebsite\Excel\Concerns\WithCustomCsvSettings;
|
|
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
|
use Maatwebsite\Excel\Concerns\WithValidation;
|
|
use Maatwebsite\Excel\Validators\Failure;
|
|
|
|
class EmployeeSortie implements ToCollection, WithCustomCsvSettings, WithValidation, WithHeadingRow, SkipsOnFailure
|
|
{
|
|
use Importable;
|
|
|
|
/** @var \App\Models\Site */
|
|
private $site;
|
|
|
|
/** @var array */
|
|
private $headers;
|
|
|
|
public $removedCount = 0;
|
|
|
|
public $importedFailed = [];
|
|
|
|
public $errorMessages = [];
|
|
|
|
public function __construct(Site $site)
|
|
{
|
|
$this->site = $site;
|
|
}
|
|
|
|
public function collection(Collection $rows)
|
|
{
|
|
/** @var \App\Models\User */
|
|
$user = auth()->user();
|
|
/** @var \App\Models\Site */
|
|
$site = Site::where('id', $user->selected_site_id)->firstOrFail();
|
|
|
|
foreach ($rows as $row) {
|
|
$employee = Employee::where('matricule', $row->get('matricule_sirh'))->first();
|
|
if (!$employee) {
|
|
$this->importedFailed[] = $row;
|
|
Log::debug(sprintf('Employee Import Remove, skipped (Employee not found) %s', $row->implode(' | ')));
|
|
continue;
|
|
}
|
|
if (!$row->get('date_fin_sirh')) {
|
|
$this->importedFailed[] = $row;
|
|
continue;
|
|
}
|
|
if (Carbon::createFromFormat('d/m/Y', $row->get('date_fin_sirh')) < Carbon::now()) {
|
|
$employee->delete();
|
|
$this->removedCount += 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'matricule_sirh' => 'required|digits_between:7,8',
|
|
'nom_sirh' => 'required|string',
|
|
'prenom_sirh' => 'required|string',
|
|
'date_fin_sirh' => 'required|string|date_format:d/m/Y',
|
|
'societe_groupe' => 'required|integer',
|
|
'libelle_societe_groupe' => 'required|string',
|
|
'site_groupe' => 'required|integer',
|
|
'libelle_site_groupe' => 'required|string'
|
|
];
|
|
}
|
|
|
|
public function getCsvSettings(): array
|
|
{
|
|
return [
|
|
'input_encoding' => 'UTF-8',
|
|
'delimiter' => ';'
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param Failure[] $failures
|
|
*/
|
|
public function onFailure(Failure ...$failures)
|
|
{
|
|
foreach ($failures as $failure) {
|
|
$this->errorMessages[] = sprintf(
|
|
"\nLigne %d :\n%s\nDonnée:\n%s\n",
|
|
$failure->row(),
|
|
\implode("\n", $failure->errors()),
|
|
\implode(", ", $failure->values())
|
|
);
|
|
}
|
|
}
|
|
}
|