deadline feature
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
require_once(__DIR__ . '/auth_check.php');
|
||||
header('Content-Type: application/json');
|
||||
require_once(__DIR__ . '/../../class/db-functions.php');
|
||||
|
||||
try {
|
||||
$db = DBHandlerSelect::getInstance();
|
||||
$pdo = $db->getConnection();
|
||||
|
||||
$start = $_GET['start'] ?? null;
|
||||
$end = $_GET['end'] ?? null;
|
||||
$filterStatus = $_GET['status'] ?? '';
|
||||
$filterDept = $_GET['department'] ?? '';
|
||||
$filterEmployee = $_GET['employee'] ?? '';
|
||||
|
||||
$sql = "SELECT DISTINCT d.id, d.topic, d.category, d.due_date, d.status, d.notification_days, d.departments
|
||||
FROM scad_deadlines d
|
||||
LEFT JOIN scad_deadline_employee de ON de.deadline_id = d.id
|
||||
LEFT JOIN employees e ON e.id = de.employee_id";
|
||||
|
||||
$where = [];
|
||||
$params = [];
|
||||
|
||||
if ($start && $end) {
|
||||
$where[] = "d.due_date >= ? AND d.due_date <= ?";
|
||||
$params[] = $start;
|
||||
$params[] = $end;
|
||||
}
|
||||
|
||||
if ($filterStatus === 'non-completata') {
|
||||
$where[] = "d.status != 'completed'";
|
||||
} elseif ($filterStatus === 'completata') {
|
||||
$where[] = "d.status = 'completed'";
|
||||
} elseif ($filterStatus === 'scaduta') {
|
||||
$where[] = "d.status = 'active' AND d.due_date < CURDATE()";
|
||||
} elseif ($filterStatus === 'in-scadenza') {
|
||||
$where[] = "d.status = 'active' AND d.due_date >= CURDATE() AND d.due_date <= DATE_ADD(CURDATE(), INTERVAL d.notification_days DAY)";
|
||||
} elseif ($filterStatus === 'attiva') {
|
||||
$where[] = "d.status = 'active' AND d.due_date > DATE_ADD(CURDATE(), INTERVAL d.notification_days DAY)";
|
||||
}
|
||||
|
||||
if ($filterDept) {
|
||||
$where[] = "(e.department = ? OR FIND_IN_SET(?, d.departments))";
|
||||
$params[] = $filterDept;
|
||||
$params[] = $filterDept;
|
||||
}
|
||||
|
||||
if ($filterEmployee) {
|
||||
$where[] = "CONCAT(e.first_name, ' ', e.last_name) = ?";
|
||||
$params[] = $filterEmployee;
|
||||
}
|
||||
|
||||
if (!empty($where)) {
|
||||
$sql .= " WHERE " . implode(' AND ', $where);
|
||||
}
|
||||
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
$deadlines = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
$today = date('Y-m-d');
|
||||
$events = [];
|
||||
|
||||
foreach ($deadlines as $d) {
|
||||
$isCompleted = $d['status'] === 'completed';
|
||||
$isOverdue = !$isCompleted && $d['due_date'] < $today;
|
||||
$approachDate = date('Y-m-d', strtotime($today . ' + ' . (int)$d['notification_days'] . ' days'));
|
||||
$isApproaching = !$isCompleted && !$isOverdue && $d['due_date'] <= $approachDate;
|
||||
|
||||
if ($isCompleted) { $color = '#198754'; }
|
||||
elseif ($isOverdue) { $color = '#dc3545'; }
|
||||
elseif ($isApproaching) { $color = '#e8930c'; }
|
||||
else { $color = '#5a8fd8'; }
|
||||
|
||||
$title = $d['topic'];
|
||||
if ($d['category']) $title = $d['category'] . ': ' . $title;
|
||||
|
||||
$events[] = [
|
||||
'id' => $d['id'],
|
||||
'title' => $title,
|
||||
'start' => $d['due_date'],
|
||||
'backgroundColor' => $color,
|
||||
'borderColor' => $color,
|
||||
'url' => 'scadenzario/detail.php?id=' . $d['id'],
|
||||
];
|
||||
}
|
||||
|
||||
echo json_encode($events);
|
||||
|
||||
} catch (Exception $e) {
|
||||
echo json_encode([]);
|
||||
}
|
||||
Reference in New Issue
Block a user