Vue global collaborateur pour manager

This commit is contained in:
2025-08-28 11:59:58 +02:00
parent ed4a7c02ca
commit ac0ae03904
14 changed files with 733 additions and 268 deletions

View File

@@ -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();
?>