79 lines
3.0 KiB
PHP

<?php
// Connessione al database
include('../../Connections/repnew.php');
// Inizia la sessione per gestire le variabili di sessione
$conn = new mysqli($servername, $username, $password, $database);
// Verifica la connessione
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Controlla se il JSON è stato ricevuto tramite POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Riceve il JSON dal laboratorio
$json_data = file_get_contents('php://input');
// Decodifica il JSON per la validazione (facoltativa)
$decoded_data = json_decode($json_data, true);
// Se il JSON è valido
if (json_last_error() === JSON_ERROR_NONE) {
// Genera un UUID per identificare univocamente il record
$uuid = uniqid(); // Alternativamente puoi usare UUID() in MySQL
// ID del laboratorio dal quale proviene il JSON (puoi aggiungere autenticazione)
$lab_id = isset($_POST['lab_id']) ? $_POST['lab_id'] : 'Unknown Lab'; // Modifica a seconda della tua logica
// Estrai alcune informazioni dal JSON
$product_refnumber = $decoded_data['product']['products_refnumber']; // Numero prodotto
$report_number = $decoded_data['product']['reports'][0]['reportsNumberLab']; // Numero report
$rating = $decoded_data['product']['reports'][0]['reportsRating']; // Rating del report (es. Pass/Fail)
$saved_at = date("Y-m-d H:i:s"); // Data del salvataggio
// Query per inserire i dati nella tabella temp_json_queue
$stmt = $conn->prepare("INSERT INTO temp_json_queue (uuid, lab_id, json_data) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $uuid, $lab_id, $json_data);
if ($stmt->execute()) {
// Imposta una variabile di sessione per notificare l'importazione del report
$_SESSION['new_report'] = [
'report_number' => $report_number,
'rating' => $rating,
'timestamp' => time() // Puoi usare un timestamp per gestire la scadenza della notifica
];
echo json_encode([
"status" => "success",
"message" => "Data successfully saved.",
"uuid" => $uuid,
"product_refnumber" => $product_refnumber, // Numero del prodotto
"report_number" => $report_number, // Numero del report
"rating" => $rating, // Rating del report
"saved_at" => $saved_at // Data del salvataggio
]);
} else {
echo json_encode([
"status" => "error",
"message" => "Failed to save data."
]);
}
$stmt->close();
} else {
// Se il JSON è invalido
echo json_encode([
"status" => "error",
"message" => "Invalid JSON format."
]);
}
} else {
echo json_encode([
"status" => "error",
"message" => "Invalid request method."
]);
}
// Chiude la connessione al database
$conn->close();