inactive worksheet

This commit is contained in:
2026-10-23 17:41:00 +02:00
parent d5094bff01
commit 381befd1d8
+186 -3
View File
@@ -261,6 +261,81 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj
exit;
}
if ($action === 'deactivate') {
$id = (int)($_POST['id'] ?? 0);
if ($id <= 0) {
echo json_encode(['success' => false, 'message' => 'ID non valido']);
exit;
}
$stmt = $pdo->prepare("
UPDATE work_sheets
SET worksheet_status = 'inactive'
WHERE id = ?
AND COALESCE(NULLIF(worksheet_status, ''), 'active') = 'active'
");
$stmt->execute([$id]);
if ($stmt->rowCount() === 0) {
echo json_encode(['success' => false, 'message' => 'Foglio non trovato o già inattivo']);
exit;
}
echo json_encode(['success' => true, 'message' => 'Foglio disattivato']);
exit;
}
if ($action === 'reactivate') {
$id = (int)($_POST['id'] ?? 0);
if ($id <= 0) {
echo json_encode(['success' => false, 'message' => 'ID non valido']);
exit;
}
$stmt = $pdo->prepare("SELECT id, worksheet_number, worksheet_status FROM work_sheets WHERE id = ? LIMIT 1");
$stmt->execute([$id]);
$ws = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$ws) {
echo json_encode(['success' => false, 'message' => 'Foglio non trovato']);
exit;
}
if (($ws['worksheet_status'] ?: 'active') === 'active') {
echo json_encode(['success' => false, 'message' => 'Il foglio è già attivo']);
exit;
}
// Non riattivare se esiste già un'altra revisione attiva dello stesso FL
$wsNumber = (int)$ws['worksheet_number'];
if ($wsNumber > 0) {
$stmtChk = $pdo->prepare("
SELECT COALESCE(NULLIF(revision_code, ''), '0')
FROM work_sheets
WHERE worksheet_number = ?
AND id <> ?
AND COALESCE(NULLIF(worksheet_status, ''), 'active') = 'active'
LIMIT 1
");
$stmtChk->execute([$wsNumber, $id]);
$activeRev = $stmtChk->fetchColumn();
if ($activeRev !== false) {
echo json_encode([
'success' => false,
'message' => 'Esiste già una revisione attiva di FL' . $wsNumber . ' (rev. ' . $activeRev . '). Disattivala prima di riattivare questa.'
]);
exit;
}
}
$stmt = $pdo->prepare("UPDATE work_sheets SET worksheet_status = 'active' WHERE id = ?");
$stmt->execute([$id]);
echo json_encode(['success' => true, 'message' => 'Foglio riattivato']);
exit;
}
echo json_encode(['success' => false, 'message' => 'Azione sconosciuta']);
exit;
} catch (Exception $e) {
@@ -287,11 +362,26 @@ $worksheets = $pdo->query("
ws.updated_at,
m.nome AS matrice_nome,
m.cliente AS matrice_cliente,
(
(
SELECT COUNT(*)
FROM work_sheet_mescole wsm
WHERE wsm.worksheet_id = ws.id
) AS mixes_count
) AS mixes_count,
(
SELECT COUNT(*)
FROM work_sheets ws2
WHERE ws.worksheet_number > 0
AND ws2.worksheet_number = ws.worksheet_number
AND ws2.id > ws.id
) AS newer_count,
(
SELECT COUNT(*)
FROM work_sheets ws3
WHERE ws.worksheet_number > 0
AND ws3.worksheet_number = ws.worksheet_number
AND ws3.id <> ws.id
AND COALESCE(NULLIF(ws3.worksheet_status, ''), 'active') = 'active'
) AS active_siblings
FROM work_sheets ws
LEFT JOIN matrice m ON ws.idmatrice = m.id
ORDER BY ws.worksheet_number DESC, ws.id DESC
@@ -407,6 +497,19 @@ $worksheets = $pdo->query("
font-size: 0.85rem;
}
.badge-status-revised {
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 90px;
padding: 6px 10px;
border-radius: 999px;
background: #e5e7eb;
color: #374151;
font-weight: 700;
font-size: 0.85rem;
}
.row-inactive {
opacity: 0.78;
background: #fcfcfd !important;
@@ -497,6 +600,9 @@ $worksheets = $pdo->query("
$isActive = ($status === 'active');
$revisionLabel = getRevisionLabel($ws['revision_code'] ?? '');
$worksheetLabel = getWorksheetLabel($ws['worksheet_number'] ?? 0);
$wasRevised = !$isActive && (int)($ws['newer_count'] ?? 0) > 0;
$canReactivate = !$isActive && (int)($ws['active_siblings'] ?? 0) === 0;
$wsFullName = ($ws['matrice_nome'] ?? 'Foglio') . ' ' . $worksheetLabel . ' rev. ' . $revisionLabel;
?>
<tr class="<?= !$isActive ? 'row-inactive' : '' ?>">
<td>
@@ -512,8 +618,10 @@ $worksheets = $pdo->query("
<td>
<?php if ($isActive): ?>
<span class="badge-status-active">Attivo</span>
<?php elseif ($wasRevised): ?>
<span class="badge-status-revised">Revisionato</span>
<?php else: ?>
<span class="badge-status-inactive">Inattivo</span>
<span class="badge-status-inactive">Disattivato</span>
<?php endif; ?>
</td>
<td><?= (int)$ws['mixes_count'] ?></td>
@@ -541,6 +649,22 @@ $worksheets = $pdo->query("
REV
</button>
<?php if ($isActive): ?>
<button class="btn btn-sm btn-outline-warning toggle-ws-status"
data-id="<?= (int)$ws['id'] ?>"
data-action="deactivate"
data-name="<?= h($wsFullName) ?>">
⏸️ Disattiva
</button>
<?php elseif ($canReactivate): ?>
<button class="btn btn-sm btn-outline-success toggle-ws-status"
data-id="<?= (int)$ws['id'] ?>"
data-action="reactivate"
data-name="<?= h($wsFullName) ?>">
▶️ Riattiva
</button>
<?php endif; ?>
<button class="btn btn-sm btn-outline-danger delete-ws"
data-id="<?= (int)$ws['id'] ?>"
data-name="<?= h(($ws['matrice_nome'] ?? 'Foglio') . ' ' . $worksheetLabel . ' rev. ' . $revisionLabel) ?>">
@@ -618,6 +742,65 @@ $worksheets = $pdo->query("
});
});
$(document).on('click', '.toggle-ws-status', function() {
const id = $(this).data('id');
const action = $(this).data('action');
const name = $('<div>').text($(this).data('name') || '').html();
const isDeact = action === 'deactivate';
Swal.fire({
title: isDeact ? 'Disattivare il foglio?' : 'Riattivare il foglio?',
html: `
<div><strong>${name}</strong></div>
<div class="mt-2 small text-muted">
${isDeact
? 'Il foglio resta nello storico ma non è più il foglio attivo della matrice. Potrai riattivarlo in seguito.'
: 'Il foglio torna a essere il foglio attivo della matrice.'}
</div>
`,
icon: 'question',
showCancelButton: true,
confirmButtonColor: isDeact ? '#f59e0b' : '#198754',
cancelButtonColor: '#6c757d',
confirmButtonText: isDeact ? 'Sì, disattiva' : 'Sì, riattiva',
cancelButtonText: 'Annulla'
}).then(result => {
if (!result.isConfirmed) return;
fetch('', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `ajax=1&action=${encodeURIComponent(action)}&id=${encodeURIComponent(id)}`
})
.then(r => r.json())
.then(data => {
if (data.success) {
Swal.fire({
icon: 'success',
title: data.message || 'Fatto',
timer: 900,
showConfirmButton: false
}).then(() => location.reload());
} else {
Swal.fire({
icon: 'error',
title: 'Errore',
text: data.message || 'Operazione non riuscita'
});
}
})
.catch(() => {
Swal.fire({
icon: 'error',
title: 'Errore',
text: 'Errore di comunicazione'
});
});
});
});
$(document).on('click', '.create-revision', function() {
if ($(this).is(':disabled')) {
return;