prepare("SELECT id, equipment_id, title FROM maint_maintenances WHERE id = ?"); $stmt->execute([$id]); $maintenance = $stmt->fetch(PDO::FETCH_ASSOC); if (!$maintenance) { mnt_json_fail('Manutenzione non trovata.'); } // Files of the maintenance and of its interventions (both cascade in DB) $stmt = $pdo->prepare("SELECT stored_name FROM maint_maintenance_files WHERE maintenance_id = ?"); $stmt->execute([$id]); $storedNames = $stmt->fetchAll(PDO::FETCH_COLUMN); $stmt = $pdo->prepare(" SELECT f.stored_name FROM maint_intervention_files f INNER JOIN maint_interventions i ON i.id = f.intervention_id WHERE i.maintenance_id = ? "); $stmt->execute([$id]); $storedNames = array_merge($storedNames, $stmt->fetchAll(PDO::FETCH_COLUMN)); $stmt = $pdo->prepare("SELECT signature_path FROM maint_interventions WHERE maintenance_id = ? AND signature_path IS NOT NULL"); $stmt->execute([$id]); $storedNames = array_merge($storedNames, $stmt->fetchAll(PDO::FETCH_COLUMN)); $pdo->prepare("DELETE FROM maint_maintenances WHERE id = ?")->execute([$id]); foreach (array_filter($storedNames) as $storedName) { mnt_delete_stored_file($storedName); } mnt_log($pdo, 'maintenance_deleted', (int)$maintenance['equipment_id'], null, null, $currentUserId, null, $maintenance['title']); mnt_json_ok(); } catch (Throwable $e) { mnt_json_fail('Errore: ' . $e->getMessage()); }