From 135519ae5ea854746564ca2aaa42141348522085 Mon Sep 17 00:00:00 2001 From: solocla Date: Fri, 24 Jul 2026 14:24:33 +0200 Subject: [PATCH] fixed mescole delete deactivate --- public/userarea/delete_mescola.php | 50 +++++++++++ public/userarea/mescole.php | 133 ++++++++++++++++++++++++----- 2 files changed, 161 insertions(+), 22 deletions(-) create mode 100644 public/userarea/delete_mescola.php diff --git a/public/userarea/delete_mescola.php b/public/userarea/delete_mescola.php new file mode 100644 index 0000000..3aff0f5 --- /dev/null +++ b/public/userarea/delete_mescola.php @@ -0,0 +1,50 @@ + false, 'message' => 'ID mescola non valido']); + exit; +} + +try { + $pdo = DBHandlerSelect::getInstance()->getConnection(); + + // Blocca la cancellazione se ci sono dati collegati: meglio disattivare + $chk = $pdo->prepare("SELECT COUNT(*) FROM mescole_supplier_lots WHERE idmescola = ?"); + $chk->execute([$id]); + $nLotti = (int)$chk->fetchColumn(); + + $chk = $pdo->prepare("SELECT COUNT(*) FROM mescole_files WHERE idmescola = ?"); + $chk->execute([$id]); + $nFile = (int)$chk->fetchColumn(); + + if ($nLotti > 0 || $nFile > 0) { + $parti = []; + if ($nLotti > 0) $parti[] = $nLotti . ' lotto/i fornitore'; + if ($nFile > 0) $parti[] = $nFile . ' file'; + echo json_encode([ + 'success' => false, + 'message' => 'Impossibile eliminare: la mescola ha ' . implode(' e ', $parti) + . '. Rimuovili prima, oppure disattiva la mescola.' + ]); + exit; + } + + $pdo->beginTransaction(); + + // Le associazioni con le linee non sono dati storici: si possono rimuovere + $pdo->prepare("DELETE FROM mescole_lines WHERE idmescola = ?")->execute([$id]); + $pdo->prepare("DELETE FROM mescole WHERE id = ?")->execute([$id]); + + $pdo->commit(); + + echo json_encode(['success' => true]); +} catch (Throwable $e) { + if (isset($pdo) && $pdo->inTransaction()) { + $pdo->rollBack(); + } + echo json_encode(['success' => false, 'message' => 'Errore database: ' . $e->getMessage()]); +} diff --git a/public/userarea/mescole.php b/public/userarea/mescole.php index 8589440..7f4cc02 100644 --- a/public/userarea/mescole.php +++ b/public/userarea/mescole.php @@ -158,8 +158,8 @@ /* Azioni */ #tabellaMescole th:nth-child(7), #tabellaMescole td:nth-child(7) { - width: 190px; - max-width: 190px; + width: 230px; + max-width: 230px; white-space: normal; } @@ -195,13 +195,13 @@ .azioni-cell { display: flex; justify-content: center; - gap: 6px; + gap: 4px; flex-wrap: nowrap; } .azioni-cell .btn { - width: 34px; - height: 34px; + width: 32px; + height: 32px; padding: 0; display: inline-flex; align-items: center; @@ -559,16 +559,25 @@ + + "; } @@ -837,30 +846,56 @@ new bootstrap.Tooltip(el); }); } - /* -------- TOGGLE ACTIVE -------- */ $(document).on('click', '.toggle-active', function() { const id = $(this).data('id'); + const isActive = String($(this).data('active')) === '1'; + const nome = $(this).data('nomeuscita') || ''; - fetch('toggle_mescola_active.php', { - method: 'POST', - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - }, - body: `id=${encodeURIComponent(id)}` - }) - .then(r => r.json()) - .then(data => { - if (data.success) { - location.reload(); - } else { + const azione = isActive ? 'Disattivare' : 'Attivare'; + const testo = isActive ? + 'La mescola non comparirà più nella lista delle attive.' : + 'La mescola tornerà disponibile fra le attive.'; + + Swal.fire({ + title: azione + ' la mescola?', + html: (nome ? '' + nome + '
' : '') + testo, + icon: 'question', + showCancelButton: true, + confirmButtonText: 'Sì, ' + (isActive ? 'disattiva' : 'attiva'), + cancelButtonText: 'Annulla', + confirmButtonColor: isActive ? '#e8930c' : '#16a34a', + cancelButtonColor: '#6c757d' + }).then((res) => { + if (!res.isConfirmed) return; + + fetch('toggle_mescola_active.php', { + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + }, + body: `id=${encodeURIComponent(id)}` + }) + .then(r => r.json()) + .then(data => { + if (data.success) { + location.reload(); + } else { + Swal.fire({ + icon: "error", + title: "Errore", + text: data.message || "Operazione non riuscita" + }); + } + }) + .catch(function() { Swal.fire({ icon: "error", title: "Errore", - text: data.message || "Operazione non riuscita" + text: "Errore di comunicazione." }); - } - }); + }); + }); }); /* -------- AGGIUNTA MESCOLA -------- */ @@ -938,6 +973,60 @@ }); }); + /* -------- CANCELLAZIONE MESCOLA -------- */ + $(document).on("click", ".delete-mescola", function() { + const id = $(this).data("id"); + const nome = $(this).data("nomeuscita") || ''; + + Swal.fire({ + title: "Eliminare definitivamente?", + html: (nome ? '' + nome + '
' : '') + + "L'operazione non può essere annullata.
" + + "Se la mescola ha lotti o file, disattivala invece di eliminarla.", + icon: "warning", + showCancelButton: true, + confirmButtonText: "Sì, elimina", + cancelButtonText: "Annulla", + confirmButtonColor: "#d33", + cancelButtonColor: "#6c757d" + }).then((res) => { + if (!res.isConfirmed) return; + + fetch("delete_mescola.php", { + method: "POST", + headers: { + "Content-Type": "application/x-www-form-urlencoded" + }, + body: `id=${encodeURIComponent(id)}` + }) + .then(r => r.json()) + .then(data => { + if (data.success) { + Swal.fire({ + icon: "success", + title: "Eliminata!", + timer: 1400, + showConfirmButton: false + }) + .then(() => location.reload()); + } else { + Swal.fire({ + icon: "error", + title: "Impossibile eliminare", + text: data.message || "Operazione non riuscita" + }); + } + }) + .catch(function() { + Swal.fire({ + icon: "error", + title: "Errore", + text: "Errore di comunicazione." + }); + }); + }); + }); + /* -------- MODALE ASSOCIA LINEE -------- */ $(document).on("click", ".associa-linee", function() { const idMescola = $(this).data("id");