71 lines
2.4 KiB
PHP
71 lines
2.4 KiB
PHP
<?php
|
|
// Evita che eventuale output di headscript rompa il JSON
|
|
ob_start();
|
|
include('include/headscript.php');
|
|
ob_end_clean();
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
$iddatadb = (int)($_POST['iddatadb'] ?? 0);
|
|
$newStatus = $_POST['status'] ?? 'i';
|
|
$action = $_POST['action'] ?? 'revert';
|
|
|
|
if ($iddatadb <= 0) {
|
|
echo json_encode(['success' => false, 'message' => 'ID record mancante']);
|
|
exit;
|
|
}
|
|
|
|
// Cancellazione record (consentita solo da stato 'i' o 'x')
|
|
if ($action === 'delete') {
|
|
try {
|
|
$db = DBHandlerSelect::getInstance();
|
|
$pdo = $db->getConnection();
|
|
|
|
$stmt = $pdo->prepare("DELETE FROM datadb WHERE iddatadb = ? AND status IN ('i', 'x')");
|
|
$stmt->execute([$iddatadb]);
|
|
|
|
if ($stmt->rowCount() > 0) {
|
|
echo json_encode(['success' => true]);
|
|
} else {
|
|
$chk = $pdo->prepare("SELECT status FROM datadb WHERE iddatadb = ?");
|
|
$chk->execute([$iddatadb]);
|
|
$current = $chk->fetchColumn();
|
|
|
|
if ($current === false) {
|
|
echo json_encode(['success' => false, 'message' => 'Record non trovato']);
|
|
} else {
|
|
echo json_encode(['success' => false, 'message' => "Cancellazione non consentita: stato attuale '$current'"]);
|
|
}
|
|
}
|
|
} catch (Throwable $e) {
|
|
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
|
|
}
|
|
exit;
|
|
}
|
|
|
|
// Da questa azione consentiamo solo il ritorno a 'i'
|
|
if ($newStatus !== 'i') {
|
|
echo json_encode(['success' => false, 'message' => 'Stato non consentito']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$db = DBHandlerSelect::getInstance();
|
|
$pdo = $db->getConnection();
|
|
|
|
// Aggiorna solo se attualmente è in stato 'l' (To LIMS).
|
|
// Azzera idcommessaweb: il revert è una scelta esplicita di riesportare, altrimenti il
|
|
// CHECK PRELIMINARE di export_to_lims.php riallineerebbe la riga alla vecchia commessa.
|
|
// commessaweb resta come traccia del codice precedente.
|
|
$stmt = $pdo->prepare("UPDATE datadb SET status = ?, idcommessaweb = NULL WHERE iddatadb = ? AND status IN ('l', 'x')");
|
|
$stmt->execute([$newStatus, $iddatadb]);
|
|
|
|
if ($stmt->rowCount() > 0) {
|
|
echo json_encode(['success' => true]);
|
|
} else {
|
|
echo json_encode(['success' => false, 'message' => 'Record non trovato o già modificato']);
|
|
}
|
|
} catch (Throwable $e) {
|
|
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
|
|
}
|