Files
zibo-dashboard/public/userarea/manutenzioni/ajax/delete_intervention.php
T
2026-08-12 21:29:55 +03:00

56 lines
1.4 KiB
PHP

<?php
require_once __DIR__ . '/auth_check.php';
mnt_require_permission('maintenance.manage');
try {
$pdo = mnt_pdo();
$id = (int)($_POST['id'] ?? 0);
if ($id <= 0) {
mnt_json_fail('ID non valido.');
}
$stmt = $pdo->prepare("SELECT * FROM maint_interventions WHERE id = ?");
$stmt->execute([$id]);
$intervention = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$intervention) {
mnt_json_fail('Intervento non trovato.');
}
$stmt = $pdo->prepare("SELECT stored_name FROM maint_intervention_files WHERE intervention_id = ?");
$stmt->execute([$id]);
$storedNames = $stmt->fetchAll(PDO::FETCH_COLUMN);
if (!empty($intervention['signature_path'])) {
$storedNames[] = $intervention['signature_path'];
}
$pdo->prepare("DELETE FROM maint_interventions WHERE id = ?")->execute([$id]);
foreach (array_filter($storedNames) as $storedName) {
mnt_delete_stored_file($storedName);
}
// Removing the last completed intervention must roll the due date back
mnt_recalc_maintenance($pdo, (int)$intervention['maintenance_id']);
mnt_log(
$pdo,
'intervention_deleted',
(int)$intervention['equipment_id'],
(int)$intervention['maintenance_id'],
null,
$currentUserId,
null,
'Intervento del ' . $intervention['performed_at']
);
mnt_json_ok();
} catch (Throwable $e) {
mnt_json_fail('Errore: ' . $e->getMessage());
}