Merge feature/lims-api into main, prefer lims-api version for conflicted files
This commit is contained in:
commit
e0e262fd32
@ -120,4 +120,76 @@ class VisualLimsApiClient
|
|||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function post($endpoint, $payload)
|
||||||
|
{
|
||||||
|
$token = $this->getToken();
|
||||||
|
$url = "{$this->baseUrl}/api/odata/{$endpoint}";
|
||||||
|
|
||||||
|
$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);
|
||||||
|
|
||||||
|
$response = curl_exec($ch);
|
||||||
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
|
$curl_error = curl_error($ch);
|
||||||
|
curl_close($ch);
|
||||||
|
|
||||||
|
if ($response === false) {
|
||||||
|
throw new Exception("Errore nella richiesta POST: {$curl_error}");
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($http_code < 200 || $http_code >= 300) {
|
||||||
|
throw new Exception("POST fallito: HTTP {$http_code}, Risposta: " . substr($response, 0, 1000));
|
||||||
|
}
|
||||||
|
|
||||||
|
return json_decode($response, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function patch($endpoint, $payload)
|
||||||
|
{
|
||||||
|
$token = $this->getToken();
|
||||||
|
$url = "{$this->baseUrl}/api/odata/{$endpoint}";
|
||||||
|
|
||||||
|
$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);
|
||||||
|
|
||||||
|
$response = curl_exec($ch);
|
||||||
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
|
$curl_error = curl_error($ch);
|
||||||
|
curl_close($ch);
|
||||||
|
|
||||||
|
if ($response === false) {
|
||||||
|
throw new Exception("Errore nella richiesta PATCH: {$curl_error}");
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($http_code < 200 || $http_code >= 300) {
|
||||||
|
throw new Exception("PATCH fallito: HTTP {$http_code}, Risposta: " . substr($response, 0, 1000));
|
||||||
|
}
|
||||||
|
|
||||||
|
return json_decode($response, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getBaseUrl()
|
||||||
|
{
|
||||||
|
return $this->baseUrl;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,243 +1,172 @@
|
|||||||
<?php
|
<?php
|
||||||
// File: export_to_lims.php
|
require_once "class/VisualLimsApiClient.class.php";
|
||||||
ini_set('display_errors', '0');
|
include('include/headscript.php');
|
||||||
error_reporting(E_ALL);
|
|
||||||
ini_set('log_errors', 1);
|
|
||||||
ini_set('error_log', __DIR__ . '/logsapi/export_lims_error.log');
|
|
||||||
|
|
||||||
// Includi il file con la connessione al database e Dotenv
|
$dbHandler = DBHandlerSelect::getInstance();
|
||||||
require_once __DIR__ . '/include/headscript.php';
|
$pdo = $dbHandler->getConnection();
|
||||||
require_once dirname(__DIR__, 2) . '/vendor/autoload.php';
|
|
||||||
require_once __DIR__ . '/class/VisualLimsApiClient.class.php';
|
|
||||||
|
|
||||||
use Dotenv\Dotenv;
|
header("Content-Type: application/json");
|
||||||
|
|
||||||
// Carica il file .env
|
|
||||||
$dotenv = Dotenv::createImmutable(dirname(__DIR__, 3)); // Torna al livello di public
|
|
||||||
$dotenv->load();
|
|
||||||
|
|
||||||
// Leggi la variabile SIMULATE_EXPORT_LIMS
|
|
||||||
$simulate = filter_var($_ENV['SIMULATE_EXPORT_LIMS'] ?? true, FILTER_VALIDATE_BOOLEAN);
|
|
||||||
|
|
||||||
header('Content-Type: application/json');
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Verifica che la richiesta sia POST e contenga iddatadb
|
$iddatadb = $_POST['iddatadb'] ?? null;
|
||||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST' || !isset($_POST['iddatadb'])) {
|
if (!$iddatadb) {
|
||||||
throw new Exception('Richiesta non valida: iddatadb mancante');
|
throw new Exception("Missing iddatadb");
|
||||||
}
|
}
|
||||||
|
|
||||||
$iddatadb = (int)$_POST['iddatadb'];
|
// 🔹 STEP 1+2: Fetch Cliente ID + Schema ID
|
||||||
|
$stmt = $pdo->prepare("
|
||||||
// Crea la cartella logsapi se non esiste
|
SELECT et.idclient AS clienteId, et.idschema AS schemaId
|
||||||
$logDir = __DIR__ . '/logsapi';
|
FROM datadb as d
|
||||||
if (!is_dir($logDir)) {
|
INNER JOIN excel_templates as et ON d.templateid = et.id
|
||||||
mkdir($logDir, 0755, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ottieni connessione al database
|
|
||||||
$db = DBHandlerSelect::getInstance();
|
|
||||||
$pdo = $db->getConnection();
|
|
||||||
|
|
||||||
// Step 1: Creazione payload per CommessaWeb
|
|
||||||
$queryCommessa = "
|
|
||||||
SELECT
|
|
||||||
d.iddatadb,
|
|
||||||
e.idclient AS Cliente,
|
|
||||||
e.idschema AS SchemaCustomField
|
|
||||||
FROM datadb d
|
|
||||||
LEFT JOIN excel_templates e ON d.templateid = e.id
|
|
||||||
WHERE d.iddatadb = :iddatadb
|
WHERE d.iddatadb = :iddatadb
|
||||||
";
|
LIMIT 1
|
||||||
|
");
|
||||||
|
$stmt->execute(['iddatadb' => $iddatadb]);
|
||||||
|
$result = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
$stmtCommessa = $pdo->prepare($queryCommessa);
|
if (!$result) {
|
||||||
$stmtCommessa->execute(['iddatadb' => $iddatadb]);
|
throw new Exception("No Cliente/Schema found for iddatadb {$iddatadb}");
|
||||||
$recordCommessa = $stmtCommessa->fetch(PDO::FETCH_ASSOC);
|
|
||||||
|
|
||||||
if (!$recordCommessa) {
|
|
||||||
throw new Exception("Nessun record trovato per iddatadb: {$iddatadb}");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validazione payload
|
$clienteId = (int) $result['clienteId'];
|
||||||
if (empty($recordCommessa['Cliente']) || empty($recordCommessa['SchemaCustomField'])) {
|
$schemaId = (int) $result['schemaId'];
|
||||||
throw new Exception("Dati mancanti per CommessaWeb: Cliente o SchemaCustomField non validi");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Payload per creazione CommessaWeb
|
// 🔹 STEP 3: Fetch Parts (including idmatrice)
|
||||||
$payloadCommessa = [
|
$stmt = $pdo->prepare("
|
||||||
'Cliente' => (int)$recordCommessa['Cliente'],
|
SELECT part_number, part_description, material, color, mix, idmatrice
|
||||||
'SchemaCustomField' => (int)$recordCommessa['SchemaCustomField'],
|
|
||||||
'Richiedente' => null,
|
|
||||||
'Descrizione' => 'example'
|
|
||||||
];
|
|
||||||
|
|
||||||
// Step 2: Creazione payload per campi custom (CommesseCustomFields)
|
|
||||||
$queryCustomFields = "
|
|
||||||
SELECT
|
|
||||||
tm.field_id AS IdCommesseCustomFields,
|
|
||||||
idd.field_value AS Valore
|
|
||||||
FROM import_data_details idd
|
|
||||||
JOIN template_mapping tm ON idd.mapping_id = tm.id
|
|
||||||
WHERE idd.id = :iddatadb
|
|
||||||
";
|
|
||||||
|
|
||||||
$stmtCustomFields = $pdo->prepare($queryCustomFields);
|
|
||||||
$stmtCustomFields->execute(['iddatadb' => $iddatadb]);
|
|
||||||
$customFields = $stmtCustomFields->fetchAll(PDO::FETCH_ASSOC);
|
|
||||||
|
|
||||||
// Costruisci l'array CommesseCustomFields
|
|
||||||
$commesseCustomFields = [];
|
|
||||||
foreach ($customFields as $field) {
|
|
||||||
$commesseCustomFields[] = [
|
|
||||||
'IdCommesseCustomFields' => (int)$field['IdCommesseCustomFields'],
|
|
||||||
'Valore' => $field['Valore'] ?? ''
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Payload per aggiornamento campi custom
|
|
||||||
$payloadCustomFields = [
|
|
||||||
'CommesseCustomFields' => $commesseCustomFields
|
|
||||||
];
|
|
||||||
|
|
||||||
// Step 3: Creazione payload per Campioni (da identification_parts)
|
|
||||||
$queryCampioni = "
|
|
||||||
SELECT
|
|
||||||
part_number,
|
|
||||||
idmatrice AS Matrice,
|
|
||||||
part_description AS NoteWeb
|
|
||||||
FROM identification_parts
|
FROM identification_parts
|
||||||
WHERE iddatadb = :iddatadb
|
WHERE iddatadb = :iddatadb
|
||||||
";
|
");
|
||||||
|
$stmt->execute(['iddatadb' => $iddatadb]);
|
||||||
|
$parts = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
$stmtCampioni = $pdo->prepare($queryCampioni);
|
// 🔹 STEP 4: Fetch Field Values with Labels
|
||||||
$stmtCampioni->execute(['iddatadb' => $iddatadb]);
|
$stmt = $pdo->prepare("
|
||||||
$campioni = $stmtCampioni->fetchAll(PDO::FETCH_ASSOC);
|
SELECT
|
||||||
|
idd.field_value,
|
||||||
|
m.field_label,
|
||||||
|
m.schema_id,
|
||||||
|
m.field_id
|
||||||
|
FROM
|
||||||
|
import_data_details as idd
|
||||||
|
JOIN template_mapping m ON idd.mapping_id = m.id
|
||||||
|
WHERE idd.id = :iddatadb
|
||||||
|
");
|
||||||
|
$stmt->execute(['iddatadb' => $iddatadb]);
|
||||||
|
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
$payloadsCampioni = [];
|
$fieldValues = [];
|
||||||
foreach ($campioni as $campione) {
|
foreach ($rows as $row) {
|
||||||
if (empty($campione['Matrice'])) {
|
$fieldValues[] = [
|
||||||
throw new Exception("Matrice non valida per campione: {$campione['part_number']}");
|
"IdCommesseCustomFields" => (int) $row['field_id'],
|
||||||
}
|
"Valore" => $row['field_value']
|
||||||
$payloadCampione = [
|
|
||||||
'Commessa' => null, // Sarà impostato dopo
|
|
||||||
'Matrice' => (int)$campione['Matrice'],
|
|
||||||
'SottoMatrice' => null,
|
|
||||||
'SchemaCustomField' => 1,
|
|
||||||
'NoteWeb' => $campione['NoteWeb'] ?? ''
|
|
||||||
];
|
];
|
||||||
$payloadsCampioni[] = $payloadCampione;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 4: Creazione payload per InviaCommessa
|
// 🔹 Initialize API client
|
||||||
$payloadInviaCommessa = [];
|
$api = VisualLimsApiClient::getInstance();
|
||||||
|
|
||||||
// Variabile per idcommessaweb
|
// 🔹 STEP 5: Create CommessaWeb (NOT WebOrder)
|
||||||
$idcommessaweb = null;
|
$commessaWebPayload = [
|
||||||
$commessaweb = '';
|
"Cliente" => $clienteId,
|
||||||
|
"SchemaCustomField" => $schemaId,
|
||||||
|
"Richiedente" => "Test Web Import",
|
||||||
|
"Descrizione" => "TEST CommessaWeb",
|
||||||
|
];
|
||||||
|
$commessaWeb = $api->post("CommessaWeb", $commessaWebPayload);
|
||||||
|
|
||||||
if ($simulate) {
|
$commessaId = $commessaWeb["IdCommessa"];
|
||||||
// Flusso simulato
|
// Estraiamo il numero della commessa usando CodiceCommessa
|
||||||
$idcommessaweb = 10176; // Fittizio per il test
|
$commessaWebCode = substr($commessaWeb["CodiceCommessa"] ?? "TEST CommessaWeb", 0, 30); // Limite a 30 caratteri
|
||||||
|
|
||||||
// Salva idcommessaweb in datadb
|
// 🔹 STEP 6: Create Campioni (Samples) for each part
|
||||||
$updateStmt = $pdo->prepare("UPDATE datadb SET idcommessaweb = :idcommessaweb WHERE iddatadb = :iddatadb");
|
$campioni = [];
|
||||||
$updateStmt->execute(['idcommessaweb' => $idcommessaweb, 'iddatadb' => $iddatadb]);
|
foreach ($parts as $index => $part) {
|
||||||
|
$matriceId = (int) ($part["idmatrice"] ?? 0);
|
||||||
|
|
||||||
// Salva i payload in file JSON
|
if ($matriceId <= 0) {
|
||||||
$outputFileCommessa = $logDir . "/commessaweb_create_{$iddatadb}.json";
|
throw new Exception("Invalid or missing idmatrice for part: " . ($part["part_number"] ?? "Unknown"));
|
||||||
file_put_contents($outputFileCommessa, json_encode($payloadCommessa, JSON_PRETTY_PRINT));
|
|
||||||
|
|
||||||
$outputFileCustomFields = $logDir . "/commessaweb_customfields_{$iddatadb}.json";
|
|
||||||
file_put_contents($outputFileCustomFields, json_encode($payloadCustomFields, JSON_PRETTY_PRINT));
|
|
||||||
|
|
||||||
foreach ($payloadsCampioni as $index => $payloadCampione) {
|
|
||||||
$payloadCampione['Commessa'] = $idcommessaweb;
|
|
||||||
$outputFileCampione = $logDir . "/campione_{$iddatadb}_{$campioni[$index]['part_number']}.json";
|
|
||||||
file_put_contents($outputFileCampione, json_encode($payloadCampione, JSON_PRETTY_PRINT));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$outputFileInviaCommessa = $logDir . "/commessaweb_invia_{$iddatadb}.json";
|
$campionePayload = [
|
||||||
file_put_contents($outputFileInviaCommessa, json_encode($payloadInviaCommessa, JSON_PRETTY_PRINT));
|
"Commessa" => $commessaId,
|
||||||
|
"Matrice" => $matriceId,
|
||||||
|
"SottoMatrice" => null,
|
||||||
|
"SchemaCustomField" => $schemaId,
|
||||||
|
"NoteWeb" => $part["part_description"] ?? ""
|
||||||
|
];
|
||||||
|
|
||||||
// Aggiorna lo status a 'l' (To LIMS)
|
$campione = $api->post("Campione", $campionePayload);
|
||||||
$updateStmt = $pdo->prepare("UPDATE datadb SET status = 'l' WHERE iddatadb = :iddatadb");
|
|
||||||
$updateStmt->execute(['iddatadb' => $iddatadb]);
|
|
||||||
|
|
||||||
// Risposta di successo (simulazione)
|
$campione["PartNumber"] = $part["part_number"] ?? "";
|
||||||
echo json_encode([
|
$campione["Material"] = $part["material"] ?? "";
|
||||||
'success' => true,
|
$campione["Color"] = $part["color"] ?? "";
|
||||||
'mode' => 'simulated',
|
$campione["Mix"] = $part["mix"] ?? "";
|
||||||
'message' => "Payload generati e salvati in {$outputFileCommessa}, {$outputFileCustomFields}, file campioni e {$outputFileInviaCommessa}",
|
|
||||||
'idcommessaweb' => $idcommessaweb,
|
|
||||||
'commessaweb' => $commessaweb,
|
|
||||||
'payload_commessa' => $payloadCommessa,
|
|
||||||
'payload_customfields' => $payloadCustomFields,
|
|
||||||
'payload_campioni' => $payloadsCampioni,
|
|
||||||
'payload_invia_commessa' => $payloadInviaCommessa
|
|
||||||
]);
|
|
||||||
} else {
|
|
||||||
// Flusso reale
|
|
||||||
$apiClient = VisualLimsApiClient::getInstance();
|
|
||||||
|
|
||||||
// Step 1: Crea CommessaWeb
|
$campioni[] = $campione;
|
||||||
$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));
|
|
||||||
}
|
|
||||||
$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]);
|
|
||||||
|
|
||||||
// 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
|
|
||||||
foreach ($payloadsCampioni as $index => $payloadCampione) {
|
|
||||||
$payloadCampione['Commessa'] = $idcommessaweb;
|
|
||||||
$apiClient->post('/api/odata/Campione', $payloadCampione);
|
|
||||||
$payloadsCampioni[$index] = $payloadCampione; // Aggiorna il payload con Commessa
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 4: Invia Commessa
|
|
||||||
$apiClient->post("/api/odata/CommessaWeb({$idcommessaweb})/InviaCommessa", $payloadInviaCommessa);
|
|
||||||
|
|
||||||
// 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]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Aggiorna lo status a 'l' (To LIMS)
|
|
||||||
$updateStmt = $pdo->prepare("UPDATE datadb SET status = 'l' WHERE iddatadb = :iddatadb");
|
|
||||||
$updateStmt->execute(['iddatadb' => $iddatadb]);
|
|
||||||
|
|
||||||
// Risposta di successo (flusso reale)
|
|
||||||
echo json_encode([
|
|
||||||
'success' => true,
|
|
||||||
'mode' => 'real',
|
|
||||||
'message' => "Dati inviati al LIMS con successo",
|
|
||||||
'idcommessaweb' => $idcommessaweb,
|
|
||||||
'commessaweb' => $commessaweb,
|
|
||||||
'payload_commessa' => $payloadCommessa,
|
|
||||||
'payload_customfields' => $payloadCustomFields,
|
|
||||||
'payload_campioni' => $payloadsCampioni,
|
|
||||||
'payload_invia_commessa' => $payloadInviaCommessa
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
} catch (Exception $e) {
|
|
||||||
// Log dell'errore
|
// 🔹 STEP 7: Update Custom Fields for CommessaWeb
|
||||||
file_put_contents($logDir . '/export_lims_error.log', date('Y-m-d H:i:s') . ' - Flusso ' . ($simulate ? 'simulato' : 'reale') . ' fallito: ' . $e->getMessage() . PHP_EOL, FILE_APPEND);
|
if (!empty($fieldValues)) {
|
||||||
http_response_code(500);
|
$commessaWithFields = $api->get("CommessaWeb(" . $commessaId . ")?\$expand=CommesseCustomFields");
|
||||||
|
$commessaCustomFields = [];
|
||||||
|
foreach ($commessaWithFields["CommesseCustomFields"] as $customField) {
|
||||||
|
foreach ($fieldValues as $fieldValue) {
|
||||||
|
if ($customField["IdCommesseCustomFields"] == $fieldValue["IdCommesseCustomFields"]) {
|
||||||
|
$commessaCustomFields[] = [
|
||||||
|
"IdCommesseCustomFields" => $customField["IdCommesseCustomFields"],
|
||||||
|
"Valore" => $fieldValue["Valore"]
|
||||||
|
];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($commessaCustomFields)) {
|
||||||
|
$updatePayload = ["CommesseCustomFields" => $commessaCustomFields];
|
||||||
|
$api->patch("CommessaWeb({$commessaId})", $updatePayload);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 🔹 STEP 8: Update datadb with idcommessaweb, commessaweb, and status
|
||||||
|
$stmt = $pdo->prepare("
|
||||||
|
UPDATE datadb
|
||||||
|
SET idcommessaweb = :idcommessaweb, commessaweb = :commessaweb, status = 'l'
|
||||||
|
WHERE iddatadb = :iddatadb
|
||||||
|
");
|
||||||
|
$stmt->execute([
|
||||||
|
'idcommessaweb' => $commessaId,
|
||||||
|
'commessaweb' => $commessaWebCode,
|
||||||
|
'iddatadb' => $iddatadb
|
||||||
|
]);
|
||||||
|
|
||||||
|
// 🔹 STEP 9: Send CommessaWeb to laboratory
|
||||||
|
$sendResult = $api->post("CommessaWeb({$commessaId})/InviaCommessa", []);
|
||||||
|
|
||||||
|
// 🔹 STEP 10: Prepare final response
|
||||||
|
$finalCommessa = [
|
||||||
|
"Cliente" => $clienteId,
|
||||||
|
"SchemaCustomField" => $schemaId,
|
||||||
|
"Richiedente" => $commessaWeb["Richiedente"] ?? "Web Import",
|
||||||
|
"Descrizione" => $commessaWeb["Descrizione"] ?? "",
|
||||||
|
"CommesseCustomFields" => $fieldValues,
|
||||||
|
"Campioni" => $campioni,
|
||||||
|
"Inviata" => 1
|
||||||
|
];
|
||||||
|
|
||||||
echo json_encode([
|
echo json_encode([
|
||||||
'success' => false,
|
"success" => true,
|
||||||
'mode' => $simulate ? 'simulated' : 'real',
|
"commessaWeb" => $finalCommessa,
|
||||||
'message' => 'Errore: ' . $e->getMessage()
|
"commessaWebApiResponse" => $commessaWeb, // Incluso per debug
|
||||||
|
"totalCampioni" => count($campioni),
|
||||||
|
"totalCustomFields" => count($fieldValues),
|
||||||
|
"message" => "Export successful"
|
||||||
|
]);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
error_log("LIMS Export Error: " . $e->getMessage() . "\nTrace: " . $e->getTraceAsString());
|
||||||
|
|
||||||
|
echo json_encode([
|
||||||
|
"success" => false,
|
||||||
|
"message" => "Export failed: " . $e->getMessage()
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -569,7 +569,11 @@ foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
|
|||||||
<?php foreach ($importedData as $index => $row): ?>
|
<?php foreach ($importedData as $index => $row): ?>
|
||||||
<div class="grid-row" data-id="<?= $row['iddatadb'] ?>">
|
<div class="grid-row" data-id="<?= $row['iddatadb'] ?>">
|
||||||
<div class="grid-cell button-cell" style="flex: 0 0 210px; position: relative;">
|
<div class="grid-cell button-cell" style="flex: 0 0 210px; position: relative;">
|
||||||
<button type="button" class="export-lims-btn action-btn" data-row="<?= $index ?>" data-iddatadb="<?= $row['iddatadb'] ?>" title="Export to LIMS" style="background: #eb0b0bff; color: white; border: none; border-radius: 5px; cursor: pointer;"><i class="fas fa-upload"></i></button>
|
<!-- commented only for admin roles -->
|
||||||
|
<?php if ((Auth::user()->hasRole('Admin'))) : ?>
|
||||||
|
<button type="button" class="export-lims-btn action-btn" data-row="<?= $index ?>" data-iddatadb="<?= $row['iddatadb'] ?>" title="Export to LIMS" style="background: #eb0b0bff; color: white; border: none; border-radius: 5px; cursor: pointer;"><i class="fas fa-upload"></i></button>
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
<button type="button" class="save-btn action-btn" data-row="<?= $index ?>" title="Save" style="background: #28a745; color: white; border: none; border-radius: 5px; cursor: pointer;"><i class="fas fa-save"></i></button>
|
<button type="button" class="save-btn action-btn" data-row="<?= $index ?>" title="Save" style="background: #28a745; color: white; border: none; border-radius: 5px; cursor: pointer;"><i class="fas fa-save"></i></button>
|
||||||
<button type="button" class="photos-btn action-btn" data-row="<?= $index ?>" data-iddatadb="<?= $row['iddatadb'] ?>" title="Photos" style="background: #007bff; color: white; border: none; border-radius: 5px; cursor: pointer;"><i class="fas fa-camera"></i></button>
|
<button type="button" class="photos-btn action-btn" data-row="<?= $index ?>" data-iddatadb="<?= $row['iddatadb'] ?>" title="Photos" style="background: #007bff; color: white; border: none; border-radius: 5px; cursor: pointer;"><i class="fas fa-camera"></i></button>
|
||||||
<button type="button" class="parts-btn action-btn" data-row="<?= $index ?>" data-iddatadb="<?= $row['iddatadb'] ?>" title="Parts" style="background: #ffc107; color: white; border: none; border-radius: 5px; cursor: pointer;"><i class="fas fa-puzzle-piece"></i></button>
|
<button type="button" class="parts-btn action-btn" data-row="<?= $index ?>" data-iddatadb="<?= $row['iddatadb'] ?>" title="Parts" style="background: #ffc107; color: white; border: none; border-radius: 5px; cursor: pointer;"><i class="fas fa-puzzle-piece"></i></button>
|
||||||
@ -720,6 +724,33 @@ foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
|
|||||||
<script src="tracking.js"></script>
|
<script src="tracking.js"></script>
|
||||||
<script src="export_to_lims.js"></script>
|
<script src="export_to_lims.js"></script>
|
||||||
<script>
|
<script>
|
||||||
|
$(document).on("click", ".export-lims-btn", function() {
|
||||||
|
let rowId = $(this).data("row");
|
||||||
|
let idDataDb = $(this).data("iddatadb");
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: "export_to_lims.php",
|
||||||
|
method: "POST",
|
||||||
|
data: {
|
||||||
|
iddatadb: idDataDb
|
||||||
|
},
|
||||||
|
dataType: "json",
|
||||||
|
beforeSend: function() {
|
||||||
|
alert("Export started in background for row " + rowId);
|
||||||
|
},
|
||||||
|
success: function(response) {
|
||||||
|
if (response.success) {
|
||||||
|
alert(response.message);
|
||||||
|
} else {
|
||||||
|
alert("❌ Error: " + response.message);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: function(xhr, status, error) {
|
||||||
|
alert("❌ AJAX error: " + error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
document.addEventListener("DOMContentLoaded", function() {
|
document.addEventListener("DOMContentLoaded", function() {
|
||||||
console.log("Page loaded, initializing event listeners");
|
console.log("Page loaded, initializing event listeners");
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user