81 lines
2.8 KiB
PHP
81 lines
2.8 KiB
PHP
<?php
|
|
|
|
require_once __DIR__ . '/auth_check.php';
|
|
|
|
mnt_require_permission('maintenance.calendar.view');
|
|
|
|
/**
|
|
* FullCalendar feed for maintenance due dates.
|
|
* Optional filters: type=scheduled|extraordinary, category=<id>
|
|
*/
|
|
try {
|
|
$pdo = mnt_pdo();
|
|
|
|
$start = (string)($_GET['start'] ?? date('Y-m-01', strtotime('-6 months')));
|
|
$end = (string)($_GET['end'] ?? date('Y-m-t', strtotime('+12 months')));
|
|
$type = (string)($_GET['type'] ?? '');
|
|
$categoryId = (int)($_GET['category'] ?? 0);
|
|
|
|
// Le attrezzature dismesse o fuori servizio restano fuori dalle viste di
|
|
// pianificazione: è a questo che serve lo stato (capitolato §1).
|
|
$where = [
|
|
"m.is_active = 1",
|
|
"e.status = 'active'",
|
|
"m.next_due_date IS NOT NULL",
|
|
"m.next_due_date BETWEEN ? AND ?",
|
|
];
|
|
$params = [substr($start, 0, 10), substr($end, 0, 10)];
|
|
|
|
if (array_key_exists($type, mnt_intervention_types())) {
|
|
$where[] = "m.intervention_type = ?";
|
|
$params[] = $type;
|
|
}
|
|
if ($categoryId > 0) {
|
|
$where[] = "e.category_id = ?";
|
|
$params[] = $categoryId;
|
|
}
|
|
|
|
$stmt = $pdo->prepare("
|
|
SELECT m.id, m.title, m.code, m.next_due_date, m.alert_days, m.is_critical, m.intervention_type,
|
|
e.id AS equipment_id, e.name AS equipment_name,
|
|
c.color AS category_color
|
|
FROM maint_maintenances m
|
|
INNER JOIN inv_equipment e ON e.id = m.equipment_id
|
|
LEFT JOIN inv_categories c ON c.id = e.category_id
|
|
WHERE " . implode(' AND ', $where) . "
|
|
ORDER BY m.next_due_date ASC
|
|
");
|
|
$stmt->execute($params);
|
|
|
|
$events = [];
|
|
|
|
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
|
|
$state = mnt_due_state($row['next_due_date'], (int)$row['alert_days']);
|
|
|
|
// Overdue/soon override the category colour so urgency wins visually
|
|
$color = $state === 'overdue' ? '#dc3545' : ($state === 'due_soon' ? '#f0a202' : ($row['category_color'] ?: '#2f7d8f'));
|
|
|
|
$events[] = [
|
|
'id' => (int)$row['id'],
|
|
'title' => ($row['code'] ? $row['code'] . ' — ' : '') . $row['equipment_name'] . ': ' . $row['title'],
|
|
'start' => $row['next_due_date'],
|
|
'allDay' => true,
|
|
'backgroundColor' => $color,
|
|
'borderColor' => $color,
|
|
'extendedProps' => [
|
|
'equipmentId' => (int)$row['equipment_id'],
|
|
'equipmentName' => $row['equipment_name'],
|
|
'maintenanceTitle' => $row['title'],
|
|
'isCritical' => (int)$row['is_critical'] === 1,
|
|
'state' => $state,
|
|
'type' => $row['intervention_type'],
|
|
],
|
|
];
|
|
}
|
|
|
|
echo json_encode($events);
|
|
} catch (Throwable $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['error' => $e->getMessage()]);
|
|
}
|