ldc-portsalaries/app/Http/Controllers/PlanningController.php
2021-06-30 08:49:42 +02:00

148 lines
5.4 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Http\Requests\ImportPlanningXlsFile;
use App\Imports\PlanningImport;
use App\Models\Planning;
use App\Models\Site;
use Illuminate\Http\UploadedFile;
use Maatwebsite\Excel\Facades\Excel;
use Maatwebsite\Excel\Excel as LExcel;
use Maatwebsite\Excel\HeadingRowImport;
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
class PlanningController extends Controller
{
// BO
public function __construct()
{
// AUTH CONTROL TO ROOT POLICY
$this->authorizeResource(Planning::class);
}
public function import()
{
$this->authorize('import', Planning::class);
CrudController::shareCrudModelsClassesToView();
return view('back.plannings.import');
}
public function importSubmit(ImportPlanningXlsFile $request)
{
$this->authorize('import', Planning::class);
/** @var \App\Models\User */
$user = auth()->user();
/** @var \App\Models\Site */
$site = Site::where('id', $user->selected_site_id)->firstOrFail();
$planningImport = null;
try {
try {
$planningImport = new PlanningImport($site, (new HeadingRowImport())->toArray(
$request->file,
null,
LExcel::XLS
));
Excel::import(
$planningImport,
$request->file,
null,
LExcel::XLS
);
} catch (ReaderException $e) {
// Assert This is XML
$xmlString = $this->filterXml($request->file);
// create ne temp file
$stream = tmpfile();
fwrite($stream, $xmlString);
$file = new UploadedFile(
stream_get_meta_data($stream)['uri'],
'xml data',
'application/vnd.ms-excel'
);
$planningImport = new PlanningImport($site, (new HeadingRowImport())->toArray(
$file,
null,
LExcel::XML
));
Excel::import(
$planningImport,
$file,
null,
LExcel::XML
);
\fclose($stream);
}
} catch (ReaderException $e) {
// this time cannot read anything
return redirect()->back()->withErrors('Echec lors de l\'import le fichier doit être conforme à celui fourni lors du developement de l\'application');
} finally {
return redirect()->route('bo.plannings.import')
->with('success', sprintf(
"%d plannings ont été importés \n Les plannings suivant n'ont pu être importés \n %s",
$planningImport->importedCount,
collect($planningImport->importedFailed)->map(function ($d) {
return $d->implode(',');
})->implode("\n")
));
}
}
/**
* Le parser Xml ne supporte pas la lecture de cellule
* dont le contenu est encapsulé avec du style... (toujours plus)
* Donc on va essayer de virer ceux-ci
*
* NOTE: Conforme au fichier transmis par Ossan et gitté dans resources/documents à cette date
* eg :
* <Cell ss:MergeDown="2" ss:StyleID="m2129200830208"><ss:Data ss:Type="String"
* xmlns="http://www.w3.org/TR/REC-html40"><B><Font html:Color="#000000">CHUPE JEAN CHARLES </Font><I><Font
* html:Face="Parisine Plus Ptf Sombre" x:Family="Swiss" html:Size="11"
* html:Color="#FFFFFF">SSsT</Font></I></B></ss:Data></Cell>
*
* @param UploadedFile $file
* @return string
*/
private function filterXml(UploadedFile $file): string
{
$xmlString = $file->get();
$xmlString = str_replace(
[
'<B>',
'</B>',
'<I>',
'</I>',
'<Font>',
'<Font ss:FontName="Arial" x:Family="Swiss" ss:Color="#000000" ss:Size="8" ss:Bold="1"/>',
'<Font ss:FontName="Arial" x:Family="Swiss" ss:Size="8" ss:Color="#000000" ss:Bold="1"/>',
'<Font ss:FontName="Arial" x:Family="Swiss" ss:Size="8" ss:Color="#FF0000" ss:Bold="1"/>',
'<Font html:Face=\'Parisine Plus Ptf Sombre\' x:Family=\'Swiss\' html:Size=\'11\' html:Color=\'#FFFFFF\'>',
"<Font ss:FontName=\"Arial\" x:Family=\"Swiss\" ss:Size=\"8\" ss:Color=\"#000000\"\n ss:Bold=\"1\"/>",
"<Font ss:FontName=\"Arial\" x:Family=\"Swiss\" ss:Size=\"8\" ss:Color=\"#000000\"\n",
"<Font ss:FontName=\"Arial\" x:Family=\"Swiss\" ss:Size=\"8\" ss:Color=\"#fdfefe\"\n",
"<Font ss:FontName=\"Arial\" x:Family=\"Swiss\" ss:Size=\"8\" ss:Color=\"#FFFFFF\"\n",
" ss:Bold=\"1\"/>\n",
'<Font html:Color=\'#000000\'>',
'</Font>'
],
'',
$xmlString
);
// dd($xmlString);
return $xmlString;
}
// FO
public function showPlannings()
{
/** @var \App\Models\Employee */
$employee = auth()->user();
$plannings = $employee->getWeekAndNextPlannings();
return view('front.employees.plannings', compact('plannings'));
}
}