fixed mescole delete deactivate

This commit is contained in:
2026-07-24 14:24:33 +02:00
parent d564561a4e
commit 135519ae5e
2 changed files with 161 additions and 22 deletions
+50
View File
@@ -0,0 +1,50 @@
<?php
include('include/headscript.php');
header('Content-Type: application/json; charset=utf-8');
$id = isset($_POST['id']) ? (int)$_POST['id'] : 0;
if ($id <= 0) {
echo json_encode(['success' => 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()]);
}
+111 -22
View File
@@ -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 @@
<button type='button' class='btn btn-sm {$toggleClass} toggle-active'
data-id='{$row['id']}'
data-active='" . ($isActive ? '1' : '0') . "'
data-nomeuscita='" . htmlspecialchars($row['nomeuscita'] ?? '', ENT_QUOTES) . "'
data-bs-toggle='tooltip' data-bs-placement='top' title='{$toggleText}'>
🔁
</button>
<button type='button' class='btn btn-sm btn-outline-secondary edit-mescola'
data-id='{$row['id']}'
data-nomeuscita='" . htmlspecialchars($row['nomeuscita'], ENT_QUOTES) . "'
data-nomeuscita='" . htmlspecialchars($row['nomeuscita'] ?? '', ENT_QUOTES) . "'
data-bs-toggle='tooltip' data-bs-placement='top' title='Modifica'>
✏️
</button>
<button type='button' class='btn btn-sm btn-outline-danger delete-mescola'
data-id='{$row['id']}'
data-nomeuscita='" . htmlspecialchars($row['nomeuscita'] ?? '', ENT_QUOTES) . "'
data-bs-toggle='tooltip' data-bs-placement='top' title='Elimina'>
🗑️
</button>
</td>
</tr>";
}
@@ -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 ? '<b>' + nome + '</b><br>' : '') + 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 ? '<b>' + nome + '</b><br>' : '') +
"L'operazione non può essere annullata.<br>" +
"<span class='text-muted small'>Se la mescola ha lotti o file, disattivala invece di eliminarla.</span>",
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");