168 lines
5.1 KiB
PHP
168 lines
5.1 KiB
PHP
<?php
|
|
// Récupération des demandes de congés avec gestion des exercices
|
|
header("Access-Control-Allow-Origin: *");
|
|
header("Access-Control-Allow-Methods: GET, OPTIONS");
|
|
header("Access-Control-Allow-Headers: Content-Type");
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
|
|
http_response_code(200);
|
|
exit();
|
|
}
|
|
|
|
header("Content-Type: application/json");
|
|
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
$host = "192.168.0.4";
|
|
$dbname = "DemandeConge";
|
|
$username = "wpuser";
|
|
$password = "-2b/)ru5/Bi8P[7_";
|
|
|
|
$conn = new mysqli($host, $username, $password, $dbname);
|
|
|
|
if ($conn->connect_error) {
|
|
error_log("Erreur connexion DB getRequests: " . $conn->connect_error);
|
|
echo json_encode(["success" => false, "message" => "Erreur de connexion à la base de données : " . $conn->connect_error]);
|
|
exit();
|
|
}
|
|
|
|
$userId = $_GET['user_id'] ?? null;
|
|
|
|
error_log("=== DEBUT getRequests.php ===");
|
|
error_log("getRequests - user_id reçu: " . ($userId ?? 'NULL'));
|
|
error_log("getRequests - Toutes les variables GET: " . print_r($_GET, true));
|
|
|
|
if ($userId === null) {
|
|
error_log("getRequests - user_id manquant");
|
|
echo json_encode(["success" => false, "message" => "ID utilisateur manquant."]);
|
|
exit();
|
|
}
|
|
|
|
$checkUserQuery = "SELECT ID, Nom, Prenom FROM Users WHERE ID = ?";
|
|
$checkUserStmt = $conn->prepare($checkUserQuery);
|
|
if ($checkUserStmt) {
|
|
$checkUserStmt->bind_param("i", $userId);
|
|
$checkUserStmt->execute();
|
|
$userResult = $checkUserStmt->get_result();
|
|
if ($userRow = $userResult->fetch_assoc()) {
|
|
error_log("getRequests - Utilisateur trouvé: " . $userRow['Prenom'] . " " . $userRow['Nom']);
|
|
} else {
|
|
error_log("getRequests - ATTENTION: Utilisateur ID $userId non trouvé dans la table Users");
|
|
}
|
|
$checkUserStmt->close();
|
|
}
|
|
|
|
function getWorkingDays($startDate, $endDate) {
|
|
$workingDays = 0;
|
|
$current = new DateTime($startDate);
|
|
$end = new DateTime($endDate);
|
|
while ($current <= $end) {
|
|
$dayOfWeek = (int)$current->format('N');
|
|
if ($dayOfWeek < 6) {
|
|
$workingDays++;
|
|
}
|
|
$current->modify('+1 day');
|
|
}
|
|
return $workingDays;
|
|
}
|
|
|
|
try {
|
|
$query = "
|
|
SELECT
|
|
dc.Id,
|
|
dc.DateDebut,
|
|
dc.DateFin,
|
|
dc.Statut,
|
|
dc.DateDemande,
|
|
dc.Commentaire,
|
|
dc.Validateur,
|
|
dc.DocumentJoint, -- 👈 CHAMP AJOUTÉ ICI
|
|
tc.Nom as TypeConge
|
|
FROM DemandeConge dc
|
|
JOIN TypeConge tc ON dc.TypeCongeId = tc.Id
|
|
WHERE dc.EmployeeId = ?
|
|
ORDER BY dc.DateDemande DESC
|
|
";
|
|
|
|
$stmt = $conn->prepare($query);
|
|
if ($stmt === false) {
|
|
throw new Exception("Erreur de préparation de la requête : " . $conn->error);
|
|
}
|
|
|
|
$stmt->bind_param("i", $userId);
|
|
$stmt->execute();
|
|
$result = $stmt->get_result();
|
|
|
|
$requests = [];
|
|
|
|
while ($row = $result->fetch_assoc()) {
|
|
$workingDays = getWorkingDays($row['DateDebut'], $row['DateFin']);
|
|
|
|
$displayType = $row['TypeConge'];
|
|
switch ($row['TypeConge']) {
|
|
case 'Congé payé':
|
|
$displayType = 'Congés payés';
|
|
break;
|
|
case 'RTT':
|
|
$displayType = 'RTT';
|
|
break;
|
|
case 'Congé maladie':
|
|
$displayType = 'Congé maladie';
|
|
break;
|
|
}
|
|
|
|
$startDate = new DateTime($row['DateDebut']);
|
|
$endDate = new DateTime($row['DateFin']);
|
|
$submittedDate = new DateTime($row['DateDemande']);
|
|
|
|
if ($row['DateDebut'] === $row['DateFin']) {
|
|
$dateDisplay = $startDate->format('d/m/Y');
|
|
} else {
|
|
$dateDisplay = $startDate->format('d/m/Y') . ' - ' . $endDate->format('d/m/Y');
|
|
}
|
|
|
|
// 👇 GÉNÉRATION DU LIEN VERS LE FICHIER
|
|
$fileUrl = null;
|
|
if ($row['TypeConge'] === 'Congé maladie' && !empty($row['DocumentJoint'])) {
|
|
$fileName = basename($row['DocumentJoint']);
|
|
$fileUrl = 'http://localhost/GTA/project/uploads/'. $fileName;
|
|
}
|
|
|
|
$requests[] = [
|
|
'id' => (int)$row['Id'],
|
|
'type' => $displayType,
|
|
'startDate' => $row['DateDebut'],
|
|
'endDate' => $row['DateFin'],
|
|
'dateDisplay' => $dateDisplay,
|
|
'days' => $workingDays,
|
|
'status' => $row['Statut'],
|
|
'reason' => $row['Commentaire'] ?: 'Aucun commentaire',
|
|
'submittedAt' => $row['DateDemande'],
|
|
'submittedDisplay' => $submittedDate->format('d/m/Y'),
|
|
'validator' => $row['Validateur'] ?: null,
|
|
'fileUrl' => $fileUrl
|
|
];
|
|
}
|
|
|
|
$stmt->close();
|
|
|
|
echo json_encode([
|
|
"success" => true,
|
|
"message" => "Demandes récupérées avec succès.",
|
|
"requests" => $requests,
|
|
"total" => count($requests)
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
error_log("Erreur récupération demandes : " . $e->getMessage());
|
|
echo json_encode([
|
|
"success" => false,
|
|
"message" => "Erreur lors de la récupération des demandes : " . $e->getMessage()
|
|
]);
|
|
}
|
|
|
|
$conn->close();
|
|
?>
|