diff --git a/public/userarea/get_clienti_trial.php b/public/userarea/get_clienti_trial.php new file mode 100644 index 00000000..57b9abee --- /dev/null +++ b/public/userarea/get_clienti_trial.php @@ -0,0 +1,102 @@ +getConnection(); + $stmt = $pdo->query("SELECT DISTINCT idclient FROM datadb WHERE idclient IS NOT NULL AND idclient > 0 ORDER BY idclient ASC"); + $ids = $stmt->fetchAll(PDO::FETCH_COLUMN); + $fakeClients = array_map(fn($id) => [ + 'IdCliente' => (int) $id, + 'Nominativo' => "Cliente Simulato {$id}", + 'CodiceCliente' => "SIM_{$id}", + ], $ids); + echo json_encode(['value' => $fakeClients]); + exit; + } + + // Parametri OData + $params = [ + '$select' => 'IdCliente,Nominativo,CodiceCliente,Inattivo', + '$orderby' => 'Nominativo asc' + ]; + + // Costruisce query string con encoding corretto + $queryString = http_build_query($params); + + // Componi endpoint finale + $endpoint = "Cliente?$queryString"; + + // Funzione per eseguire la chiamata con retry + function makeApiRequest($api, $endpoint, $maxRetries = 3) + { + for ($retry = 0; $retry < $maxRetries; $retry++) { + try { + // Tenta la chiamata API + $data = $api->get($endpoint); + // Salva risposta per debug + file_put_contents(__DIR__ . '/clienti_response.json', json_encode($data, JSON_PRETTY_PRINT)); + return $data; + } catch (Exception $e) { + $errorMessage = $e->getMessage(); + // Controlla se l'errore è legato all'autenticazione (HTTP 400 con messaggio specifico) + if (strpos($errorMessage, 'HTTP 400') !== false && strpos($errorMessage, 'Cannot persist the object') !== false) { + // Forza il refresh del token + try { + // Assumi che VisualLimsApiClient abbia un metodo per il refresh del token + $api->refreshToken(); // Da implementare in VisualLimsApiClient se non esiste + error_log("Tentativo $retry: Refresh token eseguito per endpoint $endpoint"); + } catch (Exception $refreshEx) { + error_log("Errore durante il refresh del token: " . $refreshEx->getMessage()); + throw new Exception("Impossibile eseguire il refresh del token: " . $refreshEx->getMessage()); + } + // Ritarda leggermente prima del retry + usleep(500000); // 500ms + continue; + } + // Altri errori non gestiti dal retry + throw $e; + } + } + throw new Exception("Massimo numero di tentativi raggiunto per $endpoint"); + } + + // Cache file (1 hour TTL) + $cacheFile = __DIR__ . '/cache/clienti.json'; + if (file_exists($cacheFile) && (time() - filemtime($cacheFile) < 3600)) { + readfile($cacheFile); + exit; + } + + // Esegui la chiamata con retry + $data = makeApiRequest($api, $endpoint); + + $json = json_encode($data); + if (!is_dir(__DIR__ . '/cache')) mkdir(__DIR__ . '/cache', 0777, true); + file_put_contents($cacheFile, $json); + + echo $json; +} catch (Exception $e) { + http_response_code(500); + $errorResponse = [ + 'error' => $e->getMessage(), + 'file' => $e->getFile(), + 'line' => $e->getLine(), + 'trace' => $e->getTraceAsString() + ]; + error_log("Errore in get_clienti.php: " . json_encode($errorResponse)); + echo json_encode($errorResponse); +} diff --git a/public/userarea/gridRenderer.js b/public/userarea/gridRenderer.js index 4e4d73af..4b9ee607 100644 --- a/public/userarea/gridRenderer.js +++ b/public/userarea/gridRenderer.js @@ -66,8 +66,11 @@ // ── Client data (AJAX Select2, no bulk loading) ────────────────────── function formatClientLabel(client) { - const nome = client.Nominativo || client.text || "Nome non disponibile"; - const id = client.IdCliente || client.id || ""; + const nome = ( + client.Nominativo || + client.text || + "Nome non disponibile" + ).trim(); const codiceCliente = ( client.CodiceCliente ?? client.codiceCliente ?? @@ -75,11 +78,15 @@ ) .toString() .trim(); + + // Lettera estratta (parte dopo "_", altrimenti prima lettera del codice) const suffix = (codiceCliente.split("_")[1] || "").trim(); const shortCode = suffix || (codiceCliente ? codiceCliente.charAt(0) : "--"); - return `${nome.trim()} - ${shortCode} (ID: ${id})`; + // Nome - I - CODICE_COMPLETO + const codePart = codiceCliente ? ` - ${codiceCliente}` : ""; + return `${nome} - ${shortCode}${codePart}`; } // Cache of resolved client names: id → name diff --git a/public/userarea/search_clienti.php b/public/userarea/search_clienti.php index ff498cdd..75a4d6ce 100644 --- a/public/userarea/search_clienti.php +++ b/public/userarea/search_clienti.php @@ -30,7 +30,6 @@ function isInattivo(array $c): bool function formatClientLabel(array $client): string { $name = trim($client['Nominativo'] ?? ''); - $id = trim((string)($client['IdCliente'] ?? '')); $code = trim((string)($client['CodiceCliente'] ?? '')); $parts = explode('_', $code); @@ -44,7 +43,9 @@ function formatClientLabel(array $client): string $suffix = '--'; } - return $name . ' - ' . $suffix . ' (ID: ' . $id . ')'; + $codePart = $code !== '' ? ' - ' . $code : ''; + + return $name . ' - ' . $suffix . $codePart; } try {