Compare commits
1 Commits
main
...
feature/au
| Author | SHA1 | Date | |
|---|---|---|---|
| cf45a5bc31 |
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
require_once dirname(__DIR__, 3) . '/vendor/autoload.php'; // Torna al livello di public
|
require_once dirname(__DIR__, 3) . '/vendor/autoload.php';
|
||||||
|
|
||||||
use Dotenv\Dotenv;
|
use Dotenv\Dotenv;
|
||||||
|
|
||||||
@ -13,7 +13,7 @@ class VisualLimsApiClient
|
|||||||
|
|
||||||
private function __construct()
|
private function __construct()
|
||||||
{
|
{
|
||||||
$dotenv = Dotenv::createImmutable(dirname(__DIR__, 3)); // Torna al livello di public
|
$dotenv = Dotenv::createImmutable(dirname(__DIR__, 3)); // Corretto per C:\xampp8-2-12\htdocs\trf_certest\.env
|
||||||
$dotenv->load();
|
$dotenv->load();
|
||||||
|
|
||||||
$this->baseUrl = $_ENV['API_BASE_URL'];
|
$this->baseUrl = $_ENV['API_BASE_URL'];
|
||||||
@ -87,6 +87,7 @@ class VisualLimsApiClient
|
|||||||
$token = $this->getToken();
|
$token = $this->getToken();
|
||||||
$url = "{$this->baseUrl}/api/odata/{$endpoint}";
|
$url = "{$this->baseUrl}/api/odata/{$endpoint}";
|
||||||
|
|
||||||
|
|
||||||
$ch = curl_init($url);
|
$ch = curl_init($url);
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||||
@ -120,4 +121,88 @@ class VisualLimsApiClient
|
|||||||
|
|
||||||
return $data;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
// File: export_to_lims.php
|
// File: export_to_lims.php
|
||||||
ini_set('display_errors', '0');
|
ini_set('display_errors', '1');
|
||||||
error_reporting(E_ALL);
|
error_reporting(E_ALL);
|
||||||
ini_set('log_errors', 1);
|
ini_set('log_errors', 1);
|
||||||
ini_set('error_log', __DIR__ . '/logsapi/export_lims_error.log');
|
ini_set('error_log', __DIR__ . '/logsapi/export_lims_error.log');
|
||||||
@ -13,7 +13,7 @@ require_once __DIR__ . '/class/VisualLimsApiClient.class.php';
|
|||||||
use Dotenv\Dotenv;
|
use Dotenv\Dotenv;
|
||||||
|
|
||||||
// Carica il file .env
|
// Carica il file .env
|
||||||
$dotenv = Dotenv::createImmutable(dirname(__DIR__, 3)); // Torna al livello di public
|
$dotenv = Dotenv::createImmutable(dirname(__DIR__, 2)); // Torna al livello di public
|
||||||
$dotenv->load();
|
$dotenv->load();
|
||||||
|
|
||||||
// Leggi la variabile SIMULATE_EXPORT_LIMS
|
// Leggi la variabile SIMULATE_EXPORT_LIMS
|
||||||
@ -67,8 +67,8 @@ try {
|
|||||||
$payloadCommessa = [
|
$payloadCommessa = [
|
||||||
'Cliente' => (int)$recordCommessa['Cliente'],
|
'Cliente' => (int)$recordCommessa['Cliente'],
|
||||||
'SchemaCustomField' => (int)$recordCommessa['SchemaCustomField'],
|
'SchemaCustomField' => (int)$recordCommessa['SchemaCustomField'],
|
||||||
'Richiedente' => null,
|
'Richiedente' => 'Richiedente test',
|
||||||
'Descrizione' => 'example'
|
'Descrizione' => 'esempio Claudio'
|
||||||
];
|
];
|
||||||
|
|
||||||
// Step 2: Creazione payload per campi custom (CommesseCustomFields)
|
// Step 2: Creazione payload per campi custom (CommesseCustomFields)
|
||||||
@ -85,7 +85,6 @@ try {
|
|||||||
$stmtCustomFields->execute(['iddatadb' => $iddatadb]);
|
$stmtCustomFields->execute(['iddatadb' => $iddatadb]);
|
||||||
$customFields = $stmtCustomFields->fetchAll(PDO::FETCH_ASSOC);
|
$customFields = $stmtCustomFields->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
// Costruisci l'array CommesseCustomFields
|
|
||||||
$commesseCustomFields = [];
|
$commesseCustomFields = [];
|
||||||
foreach ($customFields as $field) {
|
foreach ($customFields as $field) {
|
||||||
$commesseCustomFields[] = [
|
$commesseCustomFields[] = [
|
||||||
@ -94,7 +93,6 @@ try {
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Payload per aggiornamento campi custom
|
|
||||||
$payloadCustomFields = [
|
$payloadCustomFields = [
|
||||||
'CommesseCustomFields' => $commesseCustomFields
|
'CommesseCustomFields' => $commesseCustomFields
|
||||||
];
|
];
|
||||||
@ -180,38 +178,87 @@ try {
|
|||||||
$apiClient = VisualLimsApiClient::getInstance();
|
$apiClient = VisualLimsApiClient::getInstance();
|
||||||
|
|
||||||
// Step 1: Crea CommessaWeb
|
// Step 1: Crea CommessaWeb
|
||||||
$response = $apiClient->post('/api/odata/CommessaWeb', $payloadCommessa);
|
try {
|
||||||
if (!isset($response['success']) || !isset($response['CommessaId'])) {
|
$response = $apiClient->post('CommessaWeb', $payloadCommessa);
|
||||||
throw new Exception('Errore nella creazione della CommessaWeb: ' . json_encode($response));
|
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
|
// Salva idcommessaweb e commessaweb in datadb
|
||||||
$updateStmt = $pdo->prepare("UPDATE datadb SET idcommessaweb = :idcommessaweb WHERE iddatadb = :iddatadb");
|
$updateStmt = $pdo->prepare("UPDATE datadb SET idcommessaweb = :idcommessaweb, commessaweb = :commessaweb WHERE iddatadb = :iddatadb");
|
||||||
$updateStmt->execute(['idcommessaweb' => $idcommessaweb, 'iddatadb' => $iddatadb]);
|
$updateStmt->execute([
|
||||||
|
'idcommessaweb' => $idcommessaweb,
|
||||||
|
'commessaweb' => $commessaweb,
|
||||||
|
'iddatadb' => $iddatadb
|
||||||
|
]);
|
||||||
|
|
||||||
// Logga il successo della creazione CommessaWeb
|
// Step 2: Crea Campioni
|
||||||
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);
|
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
|
// Step 3: Aggiorna CommesseCustomFields
|
||||||
$apiClient->patch("/api/odata/CommessaWeb({$idcommessaweb})", $payloadCustomFields);
|
try {
|
||||||
|
// Salva payload CustomFields
|
||||||
// Step 3: Crea Campioni
|
$outputFileCustomFields = $logDir . "/commessaweb_customfields_{$iddatadb}_{$idcommessaweb}.json";
|
||||||
foreach ($payloadsCampioni as $index => $payloadCampione) {
|
file_put_contents($outputFileCustomFields, json_encode($payloadCustomFields, JSON_PRETTY_PRINT));
|
||||||
$payloadCampione['Commessa'] = $idcommessaweb;
|
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');
|
||||||
$apiClient->post('/api/odata/Campione', $payloadCampione);
|
error_log(date('Y-m-d H:i:s') . " - Payload CustomFields salvato in {$outputFileCustomFields}\n", 3, $logDir . '/export_lims_success.log');
|
||||||
$payloadsCampioni[$index] = $payloadCampione; // Aggiorna il payload con Commessa
|
$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
|
// 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)
|
// Step 5: Recupera il numero commessaweb
|
||||||
$commessaData = $apiClient->get("/api/odata/CommessaWeb({$idcommessaweb})");
|
try {
|
||||||
$commessaweb = $commessaData['Numero'] ?? '';
|
$commessaData = $apiClient->get("CommessaWeb({$idcommessaweb})");
|
||||||
if ($commessaweb) {
|
$commessaweb = $commessaData['CodiceCommessaWeb'] ?? $commessaweb; // Usa CodiceCommessaWeb o fallback
|
||||||
$updateStmt = $pdo->prepare("UPDATE datadb SET commessaweb = :commessaweb WHERE iddatadb = :iddatadb");
|
if ($commessaweb) {
|
||||||
$updateStmt->execute(['commessaweb' => $commessaweb, 'iddatadb' => $iddatadb]);
|
$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)
|
// Aggiorna lo status a 'l' (To LIMS)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user