61 lines
2.8 KiB
PHP
61 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\User;
|
|
use App\Services\WarehousePilotReadiness;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Inertia\Inertia;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class WarehousePilotController extends Controller
|
|
{
|
|
public function export(Request $request, WarehousePilotReadiness $service): Response
|
|
{
|
|
$actor = $request->user('web');
|
|
abort_unless($actor instanceof User && $actor->hasFunctions(['admin']), 403);
|
|
$report = DB::transaction(function () use ($actor, $service) {
|
|
if (DB::getDriverName() === 'pgsql' && DB::transactionLevel() === 1) {
|
|
DB::statement('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY');
|
|
}
|
|
|
|
return $service->inspect($actor);
|
|
});
|
|
$rows = [['Conferido em', 'Âmbito', 'Conferência', 'Resultado', 'Observação', 'Onde configurar']];
|
|
foreach ($report['checks'] as $check) {
|
|
$rows[] = [$report['checked_at'], 'SGC autónomo · diagnóstico de configuração', $check['label'], $check['ok'] ? 'Conferido' : 'A preparar', $check['detail'], $check['path'] ?? ''];
|
|
}
|
|
|
|
return response()->streamDownload(static function () use ($rows) {
|
|
$stream = fopen('php://output', 'wb');
|
|
if ($stream === false) {
|
|
throw new \RuntimeException('Não foi possível preparar a exportação.');
|
|
}
|
|
fwrite($stream, "\xEF\xBB\xBF");
|
|
foreach ($rows as $row) {
|
|
$safe = array_map(static fn (string $value) => preg_match('/^[\s\x{FEFF}]*[=+@-]/u', $value) ? "'".$value : $value, $row);
|
|
fputcsv($stream, $safe, ';', '"', '');
|
|
}
|
|
fclose($stream);
|
|
}, 'preparacao-piloto-sgc-'.now()->format('Ymd-His').'.csv', ['Content-Type' => 'text/csv; charset=UTF-8', 'Cache-Control' => 'private, no-store, max-age=0']);
|
|
}
|
|
|
|
public function index(Request $request, WarehousePilotReadiness $service): Response
|
|
{
|
|
$actor = $request->user('web');
|
|
abort_unless($actor instanceof User && $actor->hasFunctions(['admin']), 403);
|
|
|
|
return DB::transaction(function () use ($actor, $request, $service) {
|
|
if (DB::getDriverName() === 'pgsql' && DB::transactionLevel() === 1) {
|
|
DB::statement('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY');
|
|
}
|
|
Inertia::encryptHistory();
|
|
$response = Inertia::render('stock/pilot', ['report' => $service->inspect($actor), 'syntheticEvaluation' => app()->environment(['testing', 'payable-testing'])])->toResponse($request);
|
|
$response->headers->set('Cache-Control', 'private, no-store, max-age=0');
|
|
|
|
return $response;
|
|
});
|
|
}
|
|
}
|