From cf45a5bc31c6295382e51f9919e65f96a98fbf5a Mon Sep 17 00:00:00 2001 From: Claudio Date: Mon, 22 Sep 2025 16:36:23 +0200 Subject: [PATCH] fixed export to lims --- .../class/VisualLimsApiClient.class.php | 89 ++++++++++++++- public/userarea/export_to_lims.php | 107 +++++++++++++----- 2 files changed, 164 insertions(+), 32 deletions(-) diff --git a/public/userarea/class/VisualLimsApiClient.class.php b/public/userarea/class/VisualLimsApiClient.class.php index e9b4a80..fc65d66 100644 --- a/public/userarea/class/VisualLimsApiClient.class.php +++ b/public/userarea/class/VisualLimsApiClient.class.php @@ -1,5 +1,5 @@ load(); $this->baseUrl = $_ENV['API_BASE_URL']; @@ -87,6 +87,7 @@ class VisualLimsApiClient $token = $this->getToken(); $url = "{$this->baseUrl}/api/odata/{$endpoint}"; + $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ @@ -120,4 +121,88 @@ class VisualLimsApiClient return $data; } + + public function post($endpoint, $payload) + { + $token = $this->getToken(); + $url = "{$this->baseUrl}/api/odata/{$endpoint}"; // Corretto per /api/odata/CommessaWeb + file_put_contents(__DIR__ . '/url_debug.log', date('Y-m-d H:i:s') . " - POST URL: {$url}\n", FILE_APPEND); + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload)); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + "Authorization: Bearer {$token}", + "Content-Type: application/json", + "Accept: application/json" + ]); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + curl_setopt($ch, CURLOPT_VERBOSE, true); + $log = fopen(__DIR__ . '/curl_request_debug.log', 'w') ?: fopen('php://stderr', 'w'); + curl_setopt($ch, CURLOPT_STDERR, $log); + + $response = curl_exec($ch); + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $curl_error = curl_error($ch); + fclose($log); + curl_close($ch); + + if ($response === false) { + throw new Exception("Errore nella richiesta POST: {$curl_error}"); + } + + if ($http_code >= 400) { + throw new Exception("Errore nella richiesta POST: HTTP {$http_code}, Risposta: " . substr($response, 0, 1000)); + } + + $data = json_decode($response, true); + if (json_last_error() !== JSON_ERROR_NONE) { + throw new Exception("Risposta non JSON valida: " . substr($response, 0, 1000)); + } + + return $data; + } + + public function patch($endpoint, $payload) + { + $token = $this->getToken(); + $url = "{$this->baseUrl}/api/odata/{$endpoint}"; // Corretto per /api/odata/CommessaWeb({key}) + + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload)); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + "Authorization: Bearer {$token}", + "Content-Type: application/json", + "Accept: application/json" + ]); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + curl_setopt($ch, CURLOPT_VERBOSE, true); + $log = fopen(__DIR__ . '/curl_request_debug.log', 'w') ?: fopen('php://stderr', 'w'); + curl_setopt($ch, CURLOPT_STDERR, $log); + + $response = curl_exec($ch); + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $curl_error = curl_error($ch); + fclose($log); + curl_close($ch); + + if ($response === false) { + throw new Exception("Errore nella richiesta PATCH: {$curl_error}"); + } + + if ($http_code >= 400) { + throw new Exception("Errore nella richiesta PATCH: HTTP {$http_code}, Risposta: " . substr($response, 0, 1000)); + } + + $data = json_decode($response, true); + if (json_last_error() !== JSON_ERROR_NONE) { + throw new Exception("Risposta non JSON valida: " . substr($response, 0, 1000)); + } + + return $data; + } } diff --git a/public/userarea/export_to_lims.php b/public/userarea/export_to_lims.php index 02ebcd0..8c6cfc7 100644 --- a/public/userarea/export_to_lims.php +++ b/public/userarea/export_to_lims.php @@ -1,6 +1,6 @@ load(); // Leggi la variabile SIMULATE_EXPORT_LIMS @@ -67,8 +67,8 @@ try { $payloadCommessa = [ 'Cliente' => (int)$recordCommessa['Cliente'], 'SchemaCustomField' => (int)$recordCommessa['SchemaCustomField'], - 'Richiedente' => null, - 'Descrizione' => 'example' + 'Richiedente' => 'Richiedente test', + 'Descrizione' => 'esempio Claudio' ]; // Step 2: Creazione payload per campi custom (CommesseCustomFields) @@ -85,7 +85,6 @@ try { $stmtCustomFields->execute(['iddatadb' => $iddatadb]); $customFields = $stmtCustomFields->fetchAll(PDO::FETCH_ASSOC); - // Costruisci l'array CommesseCustomFields $commesseCustomFields = []; foreach ($customFields as $field) { $commesseCustomFields[] = [ @@ -94,7 +93,6 @@ try { ]; } - // Payload per aggiornamento campi custom $payloadCustomFields = [ 'CommesseCustomFields' => $commesseCustomFields ]; @@ -180,38 +178,87 @@ try { $apiClient = VisualLimsApiClient::getInstance(); // Step 1: Crea CommessaWeb - $response = $apiClient->post('/api/odata/CommessaWeb', $payloadCommessa); - if (!isset($response['success']) || !isset($response['CommessaId'])) { - throw new Exception('Errore nella creazione della CommessaWeb: ' . json_encode($response)); + try { + $response = $apiClient->post('CommessaWeb', $payloadCommessa); + if (!isset($response['IdCommessa']) || !isset($response['CodiceCommessa'])) { + throw new Exception("Risposta API non valida: IdCommessa o CodiceCommessa mancanti: " . json_encode($response)); + } + $idcommessaweb = (int)$response['IdCommessa']; + $commessaweb = $response['CodiceCommessa']; + error_log(date('Y-m-d H:i:s') . " - CommessaWeb creata: idcommessaweb {$idcommessaweb}, codice {$commessaweb} per iddatadb {$iddatadb}\n", 3, $logDir . '/export_lims_success.log'); + // Salva payload CommessaWeb + $outputFileCommessa = $logDir . "/commessaweb_create_{$iddatadb}_{$idcommessaweb}.json"; + file_put_contents($outputFileCommessa, json_encode($payloadCommessa, JSON_PRETTY_PRINT)); + error_log(date('Y-m-d H:i:s') . " - Payload CommessaWeb salvato in {$outputFileCommessa}\n", 3, $logDir . '/export_lims_success.log'); + } catch (Exception $e) { + error_log(date('Y-m-d H:i:s') . " - Errore nella creazione della CommessaWeb: " . $e->getMessage() . "\n", 3, $logDir . '/export_lims_error.log'); + throw new Exception("Errore nella creazione della CommessaWeb: " . $e->getMessage()); } - $idcommessaweb = (int)$response['CommessaId']; - // Salva idcommessaweb in datadb - $updateStmt = $pdo->prepare("UPDATE datadb SET idcommessaweb = :idcommessaweb WHERE iddatadb = :iddatadb"); - $updateStmt->execute(['idcommessaweb' => $idcommessaweb, 'iddatadb' => $iddatadb]); + // Salva idcommessaweb e commessaweb in datadb + $updateStmt = $pdo->prepare("UPDATE datadb SET idcommessaweb = :idcommessaweb, commessaweb = :commessaweb WHERE iddatadb = :iddatadb"); + $updateStmt->execute([ + 'idcommessaweb' => $idcommessaweb, + 'commessaweb' => $commessaweb, + 'iddatadb' => $iddatadb + ]); - // Logga il successo della creazione CommessaWeb - file_put_contents($logDir . '/export_lims_success.log', date('Y-m-d H:i:s') . " - CommessaWeb creata: idcommessaweb {$idcommessaweb} per iddatadb {$iddatadb}\n", FILE_APPEND); + // Step 2: Crea Campioni + try { + foreach ($payloadsCampioni as $index => $payloadCampione) { + $payloadCampione['Commessa'] = $idcommessaweb; + // Salva payload Campione + $outputFileCampione = $logDir . "/campione_{$iddatadb}_{$idcommessaweb}_{$campioni[$index]['part_number']}.json"; + file_put_contents($outputFileCampione, json_encode($payloadCampione, JSON_PRETTY_PRINT)); + error_log(date('Y-m-d H:i:s') . " - Payload Campione salvato in {$outputFileCampione}\n", 3, $logDir . '/export_lims_success.log'); + $apiClient->post('Campione', $payloadCampione); + $payloadsCampioni[$index] = $payloadCampione; // Aggiorna il payload con Commessa + error_log(date('Y-m-d H:i:s') . " - Campione creato: part_number {$campioni[$index]['part_number']} per idcommessaweb {$idcommessaweb}\n", 3, $logDir . '/export_lims_success.log'); + } + } catch (Exception $e) { + error_log(date('Y-m-d H:i:s') . " - Errore nella creazione dei Campioni: " . $e->getMessage() . "\n", 3, $logDir . '/export_lims_error.log'); + throw new Exception("Errore nella creazione dei Campioni: " . $e->getMessage()); + } - // Step 2: Aggiorna CommesseCustomFields - $apiClient->patch("/api/odata/CommessaWeb({$idcommessaweb})", $payloadCustomFields); - - // Step 3: Crea Campioni - foreach ($payloadsCampioni as $index => $payloadCampione) { - $payloadCampione['Commessa'] = $idcommessaweb; - $apiClient->post('/api/odata/Campione', $payloadCampione); - $payloadsCampioni[$index] = $payloadCampione; // Aggiorna il payload con Commessa + // Step 3: Aggiorna CommesseCustomFields + try { + // Salva payload CustomFields + $outputFileCustomFields = $logDir . "/commessaweb_customfields_{$iddatadb}_{$idcommessaweb}.json"; + file_put_contents($outputFileCustomFields, json_encode($payloadCustomFields, JSON_PRETTY_PRINT)); + error_log(date('Y-m-d H:i:s') . " - PayloadCustomFields per idcommessaweb {$idcommessaweb}: " . json_encode($payloadCustomFields, JSON_PRETTY_PRINT) . "\n", 3, $logDir . '/export_lims_success.log'); + error_log(date('Y-m-d H:i:s') . " - Payload CustomFields salvato in {$outputFileCustomFields}\n", 3, $logDir . '/export_lims_success.log'); + $apiClient->patch("CommessaWeb({$idcommessaweb})", $payloadCustomFields); + error_log(date('Y-m-d H:i:s') . " - CommesseCustomFields aggiornati per idcommessaweb {$idcommessaweb}\n", 3, $logDir . '/export_lims_success.log'); + } catch (Exception $e) { + error_log(date('Y-m-d H:i:s') . " - Errore nell'aggiornamento CommesseCustomFields: " . $e->getMessage() . "\n", 3, $logDir . '/export_lims_error.log'); + throw new Exception("Errore nell'aggiornamento CommesseCustomFields: " . $e->getMessage()); } // Step 4: Invia Commessa - $apiClient->post("/api/odata/CommessaWeb({$idcommessaweb})/InviaCommessa", $payloadInviaCommessa); + try { + // Salva payload InviaCommessa + $outputFileInviaCommessa = $logDir . "/commessaweb_invia_{$iddatadb}_{$idcommessaweb}.json"; + file_put_contents($outputFileInviaCommessa, json_encode($payloadInviaCommessa, JSON_PRETTY_PRINT)); + error_log(date('Y-m-d H:i:s') . " - Payload InviaCommessa salvato in {$outputFileInviaCommessa}\n", 3, $logDir . '/export_lims_success.log'); + $apiClient->post("CommessaWeb({$idcommessaweb})/InviaCommessa", $payloadInviaCommessa); + error_log(date('Y-m-d H:i:s') . " - Commessa inviata: idcommessaweb {$idcommessaweb}\n", 3, $logDir . '/export_lims_success.log'); + } catch (Exception $e) { + error_log(date('Y-m-d H:i:s') . " - Errore nell'invio della Commessa: " . $e->getMessage() . "\n", 3, $logDir . '/export_lims_error.log'); + throw new Exception("Errore nell'invio della Commessa: " . $e->getMessage()); + } - // Step 5: Recupera il numero commessaweb (opzionale) - $commessaData = $apiClient->get("/api/odata/CommessaWeb({$idcommessaweb})"); - $commessaweb = $commessaData['Numero'] ?? ''; - if ($commessaweb) { - $updateStmt = $pdo->prepare("UPDATE datadb SET commessaweb = :commessaweb WHERE iddatadb = :iddatadb"); - $updateStmt->execute(['commessaweb' => $commessaweb, 'iddatadb' => $iddatadb]); + // Step 5: Recupera il numero commessaweb + try { + $commessaData = $apiClient->get("CommessaWeb({$idcommessaweb})"); + $commessaweb = $commessaData['CodiceCommessaWeb'] ?? $commessaweb; // Usa CodiceCommessaWeb o fallback + if ($commessaweb) { + $updateStmt = $pdo->prepare("UPDATE datadb SET commessaweb = :commessaweb WHERE iddatadb = :iddatadb"); + $updateStmt->execute(['commessaweb' => $commessaweb, 'iddatadb' => $iddatadb]); + } + error_log(date('Y-m-d H:i:s') . " - CommessaWeb recuperata: codice {$commessaweb} per idcommessaweb {$idcommessaweb}\n", 3, $logDir . '/export_lims_success.log'); + } catch (Exception $e) { + error_log(date('Y-m-d H:i:s') . " - Errore nel recupero della CommessaWeb: " . $e->getMessage() . "\n", 3, $logDir . '/export_lims_error.log'); + throw new Exception("Errore nel recupero della CommessaWeb: " . $e->getMessage()); } // Aggiorna lo status a 'l' (To LIMS)