Merge branch 'develop' into trunk
This commit is contained in:
commit
f495fbf8ca
3 changed files with 59 additions and 74 deletions
|
|
@ -33,12 +33,18 @@ class ImportRequest extends FormRequest
|
|||
'mimetypes:text/csv,text/plain,application/csv,text/comma-separated-values,text/anytext,application/octet-stream,application/txt',
|
||||
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
||||
function ($attribute, $value, $fail) {
|
||||
/** @var \Illuminate\Http\UploadedFile */
|
||||
$uploadedFile = $this->file('import');
|
||||
$content = $uploadedFile->get();
|
||||
if (
|
||||
$this->import->getClientMimeType() !== 'text/csv'
|
||||
&& $this->import->getClientMimeType() !== 'application/vnd.ms-excel'
|
||||
$uploadedFile->getClientMimeType() !== 'text/csv'
|
||||
&& $uploadedFile->getClientMimeType() !== 'application/vnd.ms-excel'
|
||||
) {
|
||||
$fail(__('Le fichier doit être d\'un mime type text/csv ou application/vnd.ms-excel'));
|
||||
}
|
||||
if (!$content or mb_detect_encoding($content, ['ISO-8859-1'], false) !== 'ISO-8859-1') {
|
||||
$fail(__('Le fichier doit etre encodé en ISO-8859-1'));
|
||||
}
|
||||
}
|
||||
]
|
||||
];
|
||||
|
|
|
|||
|
|
@ -4,10 +4,12 @@ namespace App\Imports;
|
|||
|
||||
use App\Models\Salarie;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Arr;
|
||||
use Maatwebsite\Excel\Concerns\ToModel;
|
||||
use Maatwebsite\Excel\Concerns\WithCustomCsvSettings;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
||||
|
||||
class SalariesImport implements ToModel, WithCustomCsvSettings
|
||||
class SalariesImport implements ToModel, WithCustomCsvSettings, WithHeadingRow
|
||||
{
|
||||
/**
|
||||
* @var array $salariesImport
|
||||
|
|
@ -20,61 +22,38 @@ class SalariesImport implements ToModel, WithCustomCsvSettings
|
|||
public function getCsvSettings(): array
|
||||
{
|
||||
return [
|
||||
'delimiter' => ";"
|
||||
'delimiter' => ";",
|
||||
'input_encoding' => 'ISO-8859-1'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $line
|
||||
* @param array $row
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Model|null
|
||||
*/
|
||||
public function model(array $line)
|
||||
public function model(array $row)
|
||||
{
|
||||
if (
|
||||
!isset($line[0]) ||
|
||||
!isset($line[1]) ||
|
||||
!isset($line[2]) ||
|
||||
!isset($line[6]) ||
|
||||
!isset($line[7]) ||
|
||||
!isset($line[7])
|
||||
) {
|
||||
return null;
|
||||
/** @var string|null */
|
||||
$end_date = Arr::get($row, 'date_fin', Arr::get($row, 'date_fin_prevue'));
|
||||
|
||||
if (is_string($end_date)) {
|
||||
$end_date = Carbon::createFromFormat('d/m/Y', $end_date);
|
||||
}
|
||||
return $this->processFile($line);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $line
|
||||
*
|
||||
* @return \App\Models\Salarie
|
||||
*/
|
||||
public function processFile(array $line)
|
||||
{
|
||||
/** Check if the first line of the file matches to the data expected or is just a column commentary */
|
||||
if (preg_match('/[0-9]{7}/', $line[0])) {
|
||||
/** @var \App\Models\Salarie */
|
||||
$salarie = Salarie::firstOrNew(['matricule' => $line[0]]);
|
||||
$salarie->customer_id = $salarie->customer_id ?? null;
|
||||
$salarie->nom = ucfirst(strtolower($line[1]));
|
||||
$salarie->prenom = ucfirst(strtolower($line[2]));
|
||||
if (isset($line[3])) {
|
||||
$salarie->start_date = Carbon::createFromFormat('d/m/Y', $line[3])->format('Y-m-d');
|
||||
}
|
||||
if (isset($line[4])) {
|
||||
$salarie->end_date = Carbon::createFromFormat('d/m/Y', $line[4])->format('Y-m-d');
|
||||
} elseif (isset($line[5])) {
|
||||
$salarie->end_date = Carbon::createFromFormat('d/m/Y', $line[5])->format('Y-m-d');
|
||||
}
|
||||
$salarie->etablissement = $line[6];
|
||||
$salarie->site = $line[7];
|
||||
$salarie->service = $line[8];
|
||||
$salarie->code = $salarie->code ?? Salarie::generateRandomString(8, Salarie::query());
|
||||
Salarie::setSalarieMagasin($salarie);
|
||||
$salarie->saveOrFail();
|
||||
$this->salariesImport = array_merge($this->salariesImport, [$line[0]]);
|
||||
|
||||
return $salarie;
|
||||
} //end if
|
||||
$salarie = Salarie::firstOrNew(['matricule' => Arr::get($row, 'matricule')]);
|
||||
$salarie->matricule = Arr::get($row, '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'));
|
||||
$salarie->end_date = $end_date;
|
||||
$salarie->etablissement = Arr::get($row, 'etablissement');
|
||||
$salarie->site = Arr::get($row, 'site');
|
||||
$salarie->service = Arr::get($row, 'service');
|
||||
$salarie->code = $salarie->code ?? Salarie::generateRandomString(8, Salarie::query());
|
||||
Salarie::setSalarieMagasin($salarie);
|
||||
$salarie->saveOrFail();
|
||||
$this->salariesImport = array_merge($this->salariesImport, [Arr::get($row, 'matricule')]);
|
||||
return $salarie;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,29 +1,29 @@
|
|||
@extends('layouts.bo-layout')
|
||||
|
||||
@section('content')
|
||||
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
|
||||
<h1 class="h2">{{ __('Paramètres Site Public') }}</h1>
|
||||
@if (session('success'))
|
||||
<span class="float-center text-success">{{ session('success') }}</span>
|
||||
@endif
|
||||
</div>
|
||||
<form
|
||||
action="{{ route('bo.salaries.file') }}"
|
||||
method="POST"
|
||||
enctype="multipart/form-data">
|
||||
<fieldset>
|
||||
<legend>{{ __('Importer des salariés') }}</legend>
|
||||
<div class="input-group my-2">
|
||||
<input class="form-control" type="file" name="import" accept="text/csv,.csv"
|
||||
title="{{ __('Sélectionner un fichier .csv') }}"
|
||||
data-bs-toggle="tooltip" required>
|
||||
@csrf
|
||||
<button type="submit" class="btn btn-primary"
|
||||
title="{{ __('Importer le fichier .csv') }}"
|
||||
data-bs-toggle="tooltip">
|
||||
{{ __('Importer') }}
|
||||
</button>
|
||||
</div>
|
||||
</fieldset>
|
||||
</form>
|
||||
<div class="d-flex justify-content-between flex-md-nowrap align-items-center border-bottom mb-3 flex-wrap pb-2 pt-3">
|
||||
<h1 class="h2">{{ __('Paramètres Site Public') }}</h1>
|
||||
@if (session('success'))
|
||||
<span class="float-center text-success">{{ session('success') }}</span>
|
||||
@endif
|
||||
</div>
|
||||
<form action="{{ route('bo.salaries.file') }}" method="POST" enctype="multipart/form-data">
|
||||
<fieldset>
|
||||
<legend>{{ __('Importer des salariés') }}</legend>
|
||||
<div class="input-group my-2">
|
||||
<input class="form-control" name="import" data-bs-toggle="tooltip" type="file" title="{{ __('Sélectionner un fichier .csv') }}"
|
||||
accept="text/csv,.csv" required>
|
||||
@csrf
|
||||
<button class="btn btn-primary" data-bs-toggle="tooltip" type="submit" title="{{ __('Importer le fichier .csv') }}">
|
||||
{{ __('Importer') }}
|
||||
</button>
|
||||
</div>
|
||||
@if (request()->session()->has('success'))
|
||||
<span class="fw-bold text-success">{{ request()->session()->get('success') }}</span>
|
||||
@endif
|
||||
@if ($errors->has('import'))
|
||||
<span class="fw-bold text-danger">{{ collect($errors->get('import'))->implode(', ') }}</span>
|
||||
@endif
|
||||
</fieldset>
|
||||
</form>
|
||||
@endsection
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue