Vue global collaborateur pour manager
This commit is contained in:
@@ -130,7 +130,7 @@ echo json_encode([
|
||||
"authorized" => true,
|
||||
"role" => $role,
|
||||
"groups" => [$role],
|
||||
"localUserId" => (int)$newUserId, // 🔹 ajout important
|
||||
"localUserId" => (int)$newUserId,
|
||||
"user" => [
|
||||
"id" => $newUserId,
|
||||
"entraUserId" => $entraUserId,
|
||||
|
||||
@@ -28,11 +28,12 @@ if ($id <= 0) {
|
||||
}
|
||||
|
||||
try {
|
||||
$stmt = $conn->prepare("
|
||||
SELECT id, Nom, Prenom, Email, Matricule, Telephone, Adresse
|
||||
FROM CollaborateurAD
|
||||
WHERE id = ? AND Actif = 1
|
||||
");
|
||||
$stmt = $conn->prepare("
|
||||
SELECT id, Nom, Prenom, Email
|
||||
FROM CollaborateurAD
|
||||
WHERE id = ?
|
||||
");
|
||||
|
||||
$stmt->bind_param("i", $id);
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result();
|
||||
|
||||
@@ -13,7 +13,6 @@ if ($conn->connect_error) {
|
||||
die(json_encode(["success" => false, "message" => "Erreur DB: " . $conn->connect_error]));
|
||||
}
|
||||
|
||||
// --- Authentification (client credentials) ---
|
||||
$tenantId = "9840a2a0-6ae1-4688-b03d-d2ec291be0f9";
|
||||
$clientId = "4bb4cc24-bac3-427c-b02c-5d14fc67b561";
|
||||
$clientSecret = "ViC8Q~n4F5YweE18wjS0kfhp3kHh6LB2gZ76_b4R";
|
||||
@@ -42,8 +41,9 @@ if (!$accessToken) {
|
||||
}
|
||||
|
||||
// --- ID du groupe cible (Ensup-Groupe) ---
|
||||
$groupId = "c1ea877c-6bca-4f47-bfad-f223640813a0"; // 🔹 Mets l'Object ID de ton groupe ici
|
||||
$groupId = "c1ea877c-6bca-4f47-bfad-f223640813a0";
|
||||
|
||||
// --- Récupérer infos du groupe ---
|
||||
$urlGroup = "https://graph.microsoft.com/v1.0/groups/$groupId?\$select=id,displayName,description,mail,createdDateTime";
|
||||
$ch = curl_init($urlGroup);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Bearer $accessToken"]);
|
||||
@@ -57,31 +57,6 @@ if (!isset($group["id"])) {
|
||||
}
|
||||
|
||||
$displayName = $group["displayName"] ?? "";
|
||||
$description = $group["description"] ?? "";
|
||||
$mail = $group["mail"] ?? "";
|
||||
$createdAt = null;
|
||||
if (!empty($group["createdDateTime"])) {
|
||||
$dt = new DateTime($group["createdDateTime"]);
|
||||
$createdAt = $dt->format("Y-m-d H:i:s"); // format MySQL
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// --- Insérer / mettre à jour le groupe dans EntraGroups ---
|
||||
$stmt = $conn->prepare("INSERT INTO EntraGroups (Id, DisplayName, Description, Mail, CreatedAt, UpdatedAt, SyncDate, IsActive)
|
||||
VALUES (?, ?, ?, ?, ?, NOW(), NOW(), 1)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
DisplayName=?, Description=?, Mail=?, UpdatedAt=NOW(), SyncDate=NOW(), IsActive=1");
|
||||
if ($stmt) {
|
||||
$stmt->bind_param("ssssssss",
|
||||
$groupId, $displayName, $description, $mail, $createdAt,
|
||||
$displayName, $description, $mail
|
||||
);
|
||||
$stmt->execute();
|
||||
}
|
||||
|
||||
|
||||
|
||||
// --- Récupérer les membres du groupe ---
|
||||
$urlMembers = "https://graph.microsoft.com/v1.0/groups/$groupId/members?\$select=id,givenName,surname,mail,department,jobTitle";
|
||||
@@ -100,23 +75,25 @@ foreach ($members as $m) {
|
||||
$nom = $m["surname"] ?? "";
|
||||
$email = $m["mail"] ?? "";
|
||||
$service = $m["department"] ?? "";
|
||||
$role = "Collaborateur"; // par défaut
|
||||
|
||||
if (!$email) continue;
|
||||
|
||||
// Insertion ou mise à jour de l’utilisateur
|
||||
$stmt = $conn->prepare("INSERT INTO CollaborateurAD (entraUserId, prenom, nom, email, service, role)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE prenom=?, nom=?, email=?, service=?, role=?");
|
||||
ON DUPLICATE KEY UPDATE prenom=?, nom=?, email=?, service=?");
|
||||
if ($stmt) {
|
||||
$stmt->bind_param("sssssssssss",
|
||||
$role = "Collaborateur"; // attribué uniquement si nouvel utilisateur
|
||||
$stmt->bind_param("ssssssssss",
|
||||
$entraUserId, $prenom, $nom, $email, $service, $role,
|
||||
$prenom, $nom, $email, $service, $role
|
||||
$prenom, $nom, $email, $service
|
||||
);
|
||||
$stmt->execute();
|
||||
$usersInserted++;
|
||||
}
|
||||
}
|
||||
|
||||
// --- Réponse finale ---
|
||||
echo json_encode([
|
||||
"success" => true,
|
||||
"message" => "Synchronisation terminée",
|
||||
|
||||
@@ -44,149 +44,100 @@ $comment = $data['comment'] ?? '';
|
||||
try {
|
||||
$conn->begin_transaction();
|
||||
|
||||
// Vérifier si validateur est Users ou CollaborateurAD
|
||||
$isUserValidator = false;
|
||||
$stmt = $conn->prepare("SELECT ID FROM Users WHERE ID = ?");
|
||||
// Vérifier que le validateur existe dans CollaborateurAD
|
||||
$stmt = $conn->prepare("SELECT Id, prenom, nom FROM CollaborateurAD WHERE Id = ?");
|
||||
$stmt->bind_param("i", $validatorId);
|
||||
$stmt->execute();
|
||||
$res = $stmt->get_result();
|
||||
if ($res->fetch_assoc()) {
|
||||
$isUserValidator = true;
|
||||
} else {
|
||||
$stmt = $conn->prepare("SELECT Id FROM CollaborateurAD WHERE Id = ?");
|
||||
$stmt->bind_param("i", $validatorId);
|
||||
$stmt->execute();
|
||||
$res = $stmt->get_result();
|
||||
if (!$res->fetch_assoc()) {
|
||||
throw new Exception("Validateur introuvable dans Users ou CollaborateurAD");
|
||||
}
|
||||
}
|
||||
$validator = $stmt->get_result()->fetch_assoc();
|
||||
$stmt->close();
|
||||
|
||||
// Récupération demande
|
||||
if (!$validator) {
|
||||
throw new Exception("Validateur introuvable dans CollaborateurAD");
|
||||
}
|
||||
|
||||
// Récupération de la demande
|
||||
$queryCheck = "
|
||||
SELECT dc.Id, dc.EmployeeId, dc.CollaborateurADId, dc.TypeCongeId, dc.DateDebut, dc.DateFin, dc.NombreJours,
|
||||
u.Nom as UserNom, u.Prenom as UserPrenom,
|
||||
ca.nom as CADNom, ca.prenom as CADPrenom,
|
||||
SELECT dc.Id, dc.CollaborateurADId, dc.TypeCongeId, dc.DateDebut, dc.DateFin, dc.NombreJours,
|
||||
ca.prenom as CADPrenom, ca.nom as CADNom,
|
||||
tc.Nom as TypeNom
|
||||
FROM DemandeConge dc
|
||||
JOIN TypeConge tc ON dc.TypeCongeId = tc.Id
|
||||
LEFT JOIN Users u ON dc.EmployeeId = u.ID
|
||||
LEFT JOIN CollaborateurAD ca ON dc.CollaborateurADId = ca.Id
|
||||
WHERE dc.Id = ? AND dc.Statut = 'En attente'
|
||||
";
|
||||
$stmtCheck = $conn->prepare($queryCheck);
|
||||
$stmtCheck->bind_param("i", $requestId);
|
||||
$stmtCheck->execute();
|
||||
$resultCheck = $stmtCheck->get_result();
|
||||
|
||||
if (!($requestRow = $resultCheck->fetch_assoc())) {
|
||||
throw new Exception("Demande non trouvée ou déjà traitée");
|
||||
}
|
||||
$requestRow = $stmtCheck->get_result()->fetch_assoc();
|
||||
$stmtCheck->close();
|
||||
|
||||
$employeeId = $requestRow['EmployeeId'];
|
||||
if (!$requestRow) {
|
||||
throw new Exception("Demande non trouvée ou déjà traitée");
|
||||
}
|
||||
|
||||
$collaborateurId = $requestRow['CollaborateurADId'];
|
||||
$typeCongeId = $requestRow['TypeCongeId'];
|
||||
$nombreJours = $requestRow['NombreJours'];
|
||||
$employeeName = $employeeId
|
||||
? $requestRow['UserPrenom']." ".$requestRow['UserNom']
|
||||
: $requestRow['CADPrenom']." ".$requestRow['CADNom'];
|
||||
$employeeName = $requestRow['CADPrenom']." ".$requestRow['CADNom'];
|
||||
$typeNom = $requestRow['TypeNom'];
|
||||
|
||||
$newStatus = ($action === 'approve') ? 'Validée' : 'Refusée';
|
||||
|
||||
// 🔹 Mise à jour DemandeConge
|
||||
if ($isUserValidator) {
|
||||
$queryUpdate = "
|
||||
UPDATE DemandeConge
|
||||
SET Statut = ?,
|
||||
ValidateurId = ?,
|
||||
ValidateurADId = NULL,
|
||||
DateValidation = NOW(),
|
||||
CommentaireValidation = ?
|
||||
WHERE Id = ?
|
||||
";
|
||||
} else {
|
||||
$queryUpdate = "
|
||||
UPDATE DemandeConge
|
||||
SET Statut = ?,
|
||||
ValidateurId = NULL,
|
||||
ValidateurADId = ?,
|
||||
DateValidation = NOW(),
|
||||
CommentaireValidation = ?
|
||||
WHERE Id = ?
|
||||
";
|
||||
}
|
||||
$queryUpdate = "
|
||||
UPDATE DemandeConge
|
||||
SET Statut = ?,
|
||||
ValidateurId = ?,
|
||||
ValidateurADId = ?,
|
||||
DateValidation = NOW(),
|
||||
CommentaireValidation = ?
|
||||
WHERE Id = ?
|
||||
";
|
||||
$stmtUpdate = $conn->prepare($queryUpdate);
|
||||
$stmtUpdate->bind_param("sisi", $newStatus, $validatorId, $comment, $requestId);
|
||||
$stmtUpdate->bind_param("siisi", $newStatus, $validatorId, $validatorId, $comment, $requestId);
|
||||
$stmtUpdate->execute();
|
||||
$stmtUpdate->close();
|
||||
|
||||
// 🔹 Déduction solde (seulement Users, pas AD, hors maladie)
|
||||
if ($action === 'approve' && $typeNom !== 'Congé maladie' && $employeeId) {
|
||||
$currentDate = new DateTime();
|
||||
$year = ($typeNom === 'Congé payé' && (int)$currentDate->format('m') < 6)
|
||||
? $currentDate->format('Y') - 1
|
||||
: $currentDate->format('Y');
|
||||
|
||||
// 🔹 Déduction solde (pas maladie)
|
||||
if ($action === 'approve' && $typeNom !== 'Congé maladie' && $collaborateurId) {
|
||||
$year = date("Y");
|
||||
$queryDeduct = "
|
||||
UPDATE CompteurConges
|
||||
SET Solde = GREATEST(0, Solde - ?)
|
||||
WHERE EmployeeId = ? AND TypeCongeId = ? AND Annee = ?
|
||||
WHERE CollaborateurADId = ? AND TypeCongeId = ? AND Annee = ?
|
||||
";
|
||||
$stmtDeduct = $conn->prepare($queryDeduct);
|
||||
$stmtDeduct->bind_param("diii", $nombreJours, $employeeId, $typeCongeId, $year);
|
||||
$stmtDeduct->bind_param("diii", $nombreJours, $collaborateurId, $typeCongeId, $year);
|
||||
$stmtDeduct->execute();
|
||||
$stmtDeduct->close();
|
||||
}
|
||||
|
||||
// 🔹 Notification (User ou CollaborateurAD)
|
||||
// 🔹 Notification
|
||||
$notificationTitle = ($action === 'approve') ? 'Demande approuvée' : 'Demande refusée';
|
||||
$notificationMessage = "Votre demande de $typeNom a été " . (($action === 'approve') ? "approuvée" : "refusée");
|
||||
if ($comment) $notificationMessage .= " (Commentaire: $comment)";
|
||||
$notifType = ($action === 'approve') ? 'Success' : 'Error';
|
||||
|
||||
if ($employeeId) {
|
||||
$queryNotif = "
|
||||
INSERT INTO Notifications (UserId, CollaborateurADId, Titre, Message, Type, DemandeCongeId)
|
||||
VALUES (?, NULL, ?, ?, ?, ?)
|
||||
";
|
||||
$stmtNotif = $conn->prepare($queryNotif);
|
||||
$stmtNotif->bind_param("isssi", $employeeId, $notificationTitle, $notificationMessage, $notifType, $requestId);
|
||||
$stmtNotif->execute();
|
||||
$stmtNotif->close();
|
||||
} elseif ($collaborateurId) {
|
||||
$queryNotif = "
|
||||
INSERT INTO Notifications (UserId, CollaborateurADId, Titre, Message, Type, DemandeCongeId)
|
||||
VALUES (NULL, ?, ?, ?, ?, ?)
|
||||
";
|
||||
$stmtNotif = $conn->prepare($queryNotif);
|
||||
$stmtNotif->bind_param("isssi", $collaborateurId, $notificationTitle, $notificationMessage, $notifType, $requestId);
|
||||
$stmtNotif->execute();
|
||||
$stmtNotif->close();
|
||||
}
|
||||
$queryNotif = "
|
||||
INSERT INTO Notifications (CollaborateurADId, Titre, Message, Type, DemandeCongeId)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
";
|
||||
$stmtNotif = $conn->prepare($queryNotif);
|
||||
$stmtNotif->bind_param("isssi", $collaborateurId, $notificationTitle, $notificationMessage, $notifType, $requestId);
|
||||
$stmtNotif->execute();
|
||||
$stmtNotif->close();
|
||||
|
||||
// 🔹 Historique (User ou CollaborateurAD)
|
||||
// 🔹 Historique
|
||||
$actionText = ($action === 'approve') ? 'Validation congé' : 'Refus congé';
|
||||
$actionDetails = "$actionText $employeeName ($typeNom)";
|
||||
if ($comment) $actionDetails .= " - $comment";
|
||||
|
||||
if ($isUserValidator) {
|
||||
$queryHistory = "
|
||||
INSERT INTO HistoriqueActions (UserId, CollaborateurADId, Action, Details, DemandeCongeId)
|
||||
VALUES (?, NULL, ?, ?, ?)
|
||||
";
|
||||
$stmtHistory = $conn->prepare($queryHistory);
|
||||
$stmtHistory->bind_param("issi", $validatorId, $actionText, $actionDetails, $requestId);
|
||||
} else {
|
||||
$queryHistory = "
|
||||
INSERT INTO HistoriqueActions (UserId, CollaborateurADId, Action, Details, DemandeCongeId)
|
||||
VALUES (NULL, ?, ?, ?, ?)
|
||||
";
|
||||
$stmtHistory = $conn->prepare($queryHistory);
|
||||
$stmtHistory->bind_param("issi", $validatorId, $actionText, $actionDetails, $requestId);
|
||||
}
|
||||
$queryHistory = "
|
||||
INSERT INTO HistoriqueActions (CollaborateurADId, Action, Details, DemandeCongeId)
|
||||
VALUES (?, ?, ?, ?)
|
||||
";
|
||||
$stmtHistory = $conn->prepare($queryHistory);
|
||||
$stmtHistory->bind_param("issi", $validatorId, $actionText, $actionDetails, $requestId);
|
||||
$stmtHistory->execute();
|
||||
$stmtHistory->close();
|
||||
|
||||
@@ -204,4 +155,3 @@ try {
|
||||
}
|
||||
|
||||
$conn->close();
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user