From d088364a0da8a0f8d587594a22c2a5c353419fc1 Mon Sep 17 00:00:00 2001 From: solocla Date: Wed, 10 Jun 2026 11:02:43 +0200 Subject: [PATCH] report search --- .../class/VisualLimsApiClient.class.php | 49 +- public/userarea/download_rapporto_pdf.php | 46 ++ public/userarea/get_rapporti_cliente.php | 143 ++++ public/userarea/get_rapporto_prova.php | 53 +- public/userarea/include/navbar.php | 10 +- public/userarea/rapporti_cliente_lookup.php | 700 ++++++++++++++++++ public/userarea/rapporto_prova_lookup.php | 580 +++++++++++++++ 7 files changed, 1558 insertions(+), 23 deletions(-) create mode 100644 public/userarea/download_rapporto_pdf.php create mode 100644 public/userarea/get_rapporti_cliente.php create mode 100644 public/userarea/rapporti_cliente_lookup.php create mode 100644 public/userarea/rapporto_prova_lookup.php diff --git a/public/userarea/class/VisualLimsApiClient.class.php b/public/userarea/class/VisualLimsApiClient.class.php index 369d585..c9e72c1 100644 --- a/public/userarea/class/VisualLimsApiClient.class.php +++ b/public/userarea/class/VisualLimsApiClient.class.php @@ -257,35 +257,56 @@ class VisualLimsApiClient } /** - * Recupera contenuto binario - Adattato per https://bvcpsitaly-elims.com/limsapi + * Get raw/binary content from VisualLims API. + * Used for PDF downloads from MediaFile/DownloadStream. */ public function getRaw($endpoint) { $token = $this->getToken(); - // IMPORTANTE: usa /odata/ e NON /api/odata/ - $url = "{$this->baseUrl}/odata/{$endpoint}"; + /* + * Normal JSON OData calls use: + * {$this->baseUrl}/api/odata/... + * + * Media file downloads use: + * {$this->baseUrl}/api/MediaFile/DownloadStream... + */ + $url = rtrim($this->baseUrl, '/') . '/api/' . ltrim($endpoint, '/'); $ch = curl_init($url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_HTTPHEADER, [ - "Authorization: Bearer {$token}", - "Accept: */*" + + curl_setopt_array($ch, [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_HTTPHEADER => [ + "Authorization: Bearer {$token}", + "Accept: application/pdf,*/*" + ], + CURLOPT_SSL_VERIFYPEER => false, + CURLOPT_SSL_VERIFYHOST => false, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_TIMEOUT => 60 ]); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $response = curl_exec($ch); - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); - $curl_error = curl_error($ch); + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); + $curlError = curl_error($ch); + curl_close($ch); if ($response === false) { - throw new Exception("Errore cURL: " . $curl_error); + throw new Exception("Errore cURL download raw: " . $curlError); } - if ($http_code !== 200) { - throw new Exception("HTTP {$http_code} su endpoint: " . $url); + if ($httpCode < 200 || $httpCode >= 300) { + throw new Exception( + "Errore HTTP {$httpCode} durante download raw. Content-Type: {$contentType}. Response: " . + substr($response, 0, 500) + ); + } + + if (empty($response)) { + throw new Exception("Risposta vuota dal download raw."); } return $response; diff --git a/public/userarea/download_rapporto_pdf.php b/public/userarea/download_rapporto_pdf.php new file mode 100644 index 0000000..d3570bf --- /dev/null +++ b/public/userarea/download_rapporto_pdf.php @@ -0,0 +1,46 @@ +getRaw($endpoint); + + if (empty($pdfContent)) { + throw new Exception("PDF vuoto o non ricevuto dal server."); + } + + $fileName = "rapporto_{$idRapportoFile}.pdf"; + + header('Content-Type: application/pdf'); + header('Content-Disposition: inline; filename="' . $fileName . '"'); + header('Content-Length: ' . strlen($pdfContent)); + header('Cache-Control: private, max-age=0, must-revalidate'); + header('Pragma: public'); + + echo $pdfContent; + exit; +} catch (Exception $e) { + http_response_code(500); + header('Content-Type: application/json; charset=utf-8'); + + echo json_encode([ + 'success' => false, + 'error' => $e->getMessage() + ], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); +} diff --git a/public/userarea/get_rapporti_cliente.php b/public/userarea/get_rapporti_cliente.php new file mode 100644 index 0000000..b68d676 --- /dev/null +++ b/public/userarea/get_rapporti_cliente.php @@ -0,0 +1,143 @@ + $filter, + '$select' => 'IdRapporto,CodiceRapporto,Data,Versione,Firmato,DataStampa', + '$expand' => 'RapportiFiles', + '$orderby' => 'Data desc', + '$top' => $limit + ]; + + $endpoint = "Rapporto?" . http_build_query($params); + + file_put_contents( + __DIR__ . '/last_rapporti_cliente_endpoint.txt', + '[' . date('Y-m-d H:i:s') . '] ' . $endpoint . PHP_EOL, + FILE_APPEND + ); + + $data = $api->get($endpoint); + + $items = $data['value'] ?? []; + + if (!is_array($items)) { + $items = []; + } + + $reports = []; + + foreach ($items as $item) { + $rapportiFiles = $item['RapportiFiles'] ?? []; + $pdfFiles = []; + + if (is_array($rapportiFiles)) { + foreach ($rapportiFiles as $file) { + $idRapportoFile = intval($file['IdRapportoFile'] ?? 0); + + if ($idRapportoFile > 0) { + $pdfFiles[] = [ + 'id_rapporto_file' => $idRapportoFile, + 'file_name' => $file['FileName'] ?? null, + 'categoria' => $file['Categoria'] ?? null, + 'tipo_rapporto' => $file['TipoRapporto'] ?? null, + 'download_url' => "download_rapporto_pdf.php?id_rapporto_file={$idRapportoFile}" + ]; + } + } + } + + $reports[] = [ + 'id_rapporto' => $item['IdRapporto'] ?? null, + 'codice_rapporto' => $item['CodiceRapporto'] ?? null, + 'data' => $item['Data'] ?? null, + 'data_stampa' => $item['DataStampa'] ?? null, + 'versione' => $item['Versione'] ?? null, + 'firmato' => $item['Firmato'] ?? null, + 'pdf_files' => $pdfFiles + ]; + } + + echo json_encode([ + 'success' => true, + 'id_cliente' => $idCliente, + 'limit' => $limit, + 'signed_status' => $signedStatus, + 'endpoint' => $endpoint, + 'count' => count($reports), + 'reports' => $reports + ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); +} catch (Exception $e) { + file_put_contents( + __DIR__ . '/error_log.txt', + date('Y-m-d H:i:s') . ' - get_rapporti_cliente.php - ' . $e->getMessage() . PHP_EOL, + FILE_APPEND + ); + + http_response_code(500); + + echo json_encode([ + 'success' => false, + 'error' => $e->getMessage() + ], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); +} diff --git a/public/userarea/get_rapporto_prova.php b/public/userarea/get_rapporto_prova.php index 0ae0cd9..232444b 100644 --- a/public/userarea/get_rapporto_prova.php +++ b/public/userarea/get_rapporto_prova.php @@ -17,6 +17,7 @@ try { // rapporto_by_codice_expand_step.php?codice=2541111&step=files_campioni $codiceRapporto = trim($_GET['codice'] ?? ''); + // Safe step mode: default is base, but allows controlled read-only steps $step = trim($_GET['step'] ?? 'base'); if ($codiceRapporto === '') { @@ -25,10 +26,9 @@ try { $allowedSteps = [ 'base' => '', - 'files' => 'RapportiFiles', - 'allegati' => 'RapportiAllegati', 'campioni' => 'CampioniDatiRapporto', - 'files_campioni' => 'RapportiFiles,CampioniDatiRapporto' + 'files' => 'RapportiFiles,Cliente', + 'cliente' => 'Cliente' ]; if (!array_key_exists($step, $allowedSteps)) { @@ -37,7 +37,8 @@ try { // Escape OData per eventuali apostrofi $codiceRapportoSafe = str_replace("'", "''", $codiceRapporto); - + // Safe version of codice rapporto for filenames + $codiceRapportoFileSafe = preg_replace('/[^a-zA-Z0-9_-]/', '_', $codiceRapporto); /* * STEP 1 - Trova IdRapporto partendo da CodiceRapporto. * Query leggera, con $select e $top=1. @@ -107,15 +108,43 @@ try { $detailData = $api->get($detailEndpoint); + $pdfFiles = []; + + if ($step === 'files') { + $rapportiFiles = $detailData['RapportiFiles'] ?? []; + + if (is_array($rapportiFiles)) { + foreach ($rapportiFiles as $file) { + $idRapportoFile = intval($file['IdRapportoFile'] ?? 0); + + if ($idRapportoFile > 0) { + $pdfFiles[] = [ + 'id_rapporto_file' => $idRapportoFile, + 'file_name' => $file['FileName'] ?? null, + 'categoria' => $file['Categoria'] ?? null, + 'tipo_rapporto' => $file['TipoRapporto'] ?? null, + 'download_endpoint' => "MediaFile/DownloadStream?objectType=RapportoFile&propertyName=FileContent&objectKey={$idRapportoFile}" + ]; + } + } + } + } + + $clienteData = null; + + if ($step === 'cliente' || $step === 'files') { + $clienteData = $detailData['Cliente'] ?? null; + } + file_put_contents( - __DIR__ . "/rapporto_codice_{$codiceRapportoSafe}_{$step}.json", + __DIR__ . "/rapporto_codice_{$codiceRapportoFileSafe}_{$step}.json", json_encode([ 'search' => $searchData, 'detail' => $detailData ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) ); - echo json_encode([ + $response = [ 'success' => true, 'codice_rapporto' => $codiceRapporto, 'id_rapporto' => $rapportoId, @@ -124,7 +153,17 @@ try { 'detail_endpoint' => $detailEndpoint, 'rapporto_base' => $rapportoBase, 'data' => $detailData - ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); + ]; + + if ($step === 'files') { + $response['pdf_files'] = $pdfFiles; + } + + if ($step === 'cliente' || $step === 'files') { + $response['cliente'] = $clienteData; + } + + echo json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); } catch (Exception $e) { file_put_contents( __DIR__ . '/error_log.txt', diff --git a/public/userarea/include/navbar.php b/public/userarea/include/navbar.php index 128e77b..c33ab3e 100644 --- a/public/userarea/include/navbar.php +++ b/public/userarea/include/navbar.php @@ -57,8 +57,14 @@ - - + +
  • + +
    +
    + +
    +
  • diff --git a/public/userarea/rapporti_cliente_lookup.php b/public/userarea/rapporti_cliente_lookup.php new file mode 100644 index 0000000..27d80e9 --- /dev/null +++ b/public/userarea/rapporti_cliente_lookup.php @@ -0,0 +1,700 @@ + + + + + + + + + + + + + + + + TRF-Project - Customer Reports + + + + + +
    + + + + + +
    +
    + + + +
    + +
    +
    +
    +
    +
    Customer Test Reports
    +
    + Select a VisualLims customer and retrieve the latest reports with PDF links. +
    +
    +
    +
    + +
    +
    + +
    + + +
    + +
    + + +
    + +
    + + +
    + +
    + +
    + +
    +
    +
    + +
    +
    +
    +
    Customer Code
    +
    -
    +
    + +
    +
    Customer Name
    +
    -
    +
    +
    +
    + + + +
    + +
    No reports loaded
    +
    Select a customer, choose the limit and click Search Reports.
    +
    + +
    + +
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + \ No newline at end of file diff --git a/public/userarea/rapporto_prova_lookup.php b/public/userarea/rapporto_prova_lookup.php new file mode 100644 index 0000000..8720ec6 --- /dev/null +++ b/public/userarea/rapporto_prova_lookup.php @@ -0,0 +1,580 @@ + + + + + + + + + + + + + + + TRF-Project - Test Report Lookup + + + + + + +
    + + + + + + + + + + +
    +
    + + + +
    + +
    +
    +
    +
    +
    Test Report Lookup
    +
    + Search a test report from VisualLims by report number and download the PDF if available. +
    +
    +
    +
    + +
    + +
    +
    + + +
    + +
    + +
    +
    + +
    +
    + + + +
    + +
    No report loaded
    +
    Enter a report number and click Proceed.
    +
    + +
    + +
    +
    + + + +
    + + + + + + + + + + +
    + + + + + + + + + \ No newline at end of file