Compare commits
1 Commits
main
...
feature/au
| Author | SHA1 | Date | |
|---|---|---|---|
| cf45a5bc31 |
@ -1,5 +1,5 @@
|
||||
<?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;
|
||||
|
||||
@ -13,7 +13,7 @@ class VisualLimsApiClient
|
||||
|
||||
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();
|
||||
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
// File: export_to_lims.php
|
||||
ini_set('display_errors', '0');
|
||||
ini_set('display_errors', '1');
|
||||
error_reporting(E_ALL);
|
||||
ini_set('log_errors', 1);
|
||||
ini_set('error_log', __DIR__ . '/logsapi/export_lims_error.log');
|
||||
@ -13,7 +13,7 @@ require_once __DIR__ . '/class/VisualLimsApiClient.class.php';
|
||||
use Dotenv\Dotenv;
|
||||
|
||||
// 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();
|
||||
|
||||
// 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,39 +178,88 @@ 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: Aggiorna CommesseCustomFields
|
||||
$apiClient->patch("/api/odata/CommessaWeb({$idcommessaweb})", $payloadCustomFields);
|
||||
|
||||
// Step 3: Crea Campioni
|
||||
// Step 2: Crea Campioni
|
||||
try {
|
||||
foreach ($payloadsCampioni as $index => $payloadCampione) {
|
||||
$payloadCampione['Commessa'] = $idcommessaweb;
|
||||
$apiClient->post('/api/odata/Campione', $payloadCampione);
|
||||
// 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 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'] ?? '';
|
||||
// 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)
|
||||
$updateStmt = $pdo->prepare("UPDATE datadb SET status = 'l' WHERE iddatadb = :iddatadb");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user