diff --git a/public/userarea/errors.php b/public/userarea/errors.php index b7314827..32e8b549 100644 --- a/public/userarea/errors.php +++ b/public/userarea/errors.php @@ -146,6 +146,10 @@ $rowsData = $stmt->fetchAll(PDO::FETCH_ASSOC); background: #fd7e14; } + .btn-delete { + background: #dc3545; + } + .pager-bar { display: flex; align-items: center; @@ -232,7 +236,7 @@ $rowsData = $stmt->fetchAll(PDO::FETCH_ASSOC); - + @@ -250,6 +254,7 @@ $rowsData = $stmt->fetchAll(PDO::FETCH_ASSOC); +
AzioniAzioni Status Client Errore Error @@ -331,6 +336,39 @@ $rowsData = $stmt->fetchAll(PDO::FETCH_ASSOC); }); }); + // Elimina record + $(document).on('click', '.delete-btn', function() { + const $btn = $(this); + const iddatadb = $btn.data('iddatadb'); + if (!confirm('Eliminare definitivamente questo record? L\'operazione non è reversibile.')) return; + + const original = $btn.html(); + $btn.prop('disabled', true).html(''); + + $.ajax({ + url: 'revert_status.php', + method: 'POST', + dataType: 'json', + data: { + iddatadb: iddatadb, + action: 'delete' + }, + success: function(res) { + if (res && res.success) { + const $tr = $('tr[data-id="' + iddatadb + '"]'); + $tr.fadeOut(300, () => $tr.remove()); + } else { + alert('Errore: ' + ((res && res.message) ? res.message : 'cancellazione non riuscita')); + $btn.prop('disabled', false).html(original); + } + }, + error: function() { + alert('Errore di rete.'); + $btn.prop('disabled', false).html(original); + } + }); + }); + // Revert a Imported (i) $(document).on('click', '.revert-btn', function() { const $btn = $(this); diff --git a/public/userarea/revert_status.php b/public/userarea/revert_status.php index de613f46..65563d6c 100644 --- a/public/userarea/revert_status.php +++ b/public/userarea/revert_status.php @@ -8,12 +8,41 @@ 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']); @@ -28,7 +57,7 @@ try { // 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 = 'l'"); + $stmt = $pdo->prepare("UPDATE datadb SET status = ?, idcommessaweb = NULL WHERE iddatadb = ? AND status IN ('l', 'x')"); $stmt->execute([$newStatus, $iddatadb]); if ($stmt->rowCount() > 0) {