modification concernant l'affichage de type de congés dans la pagedemande+Affichage du service du collabrateur et son rôle+ version mobile de la page demande
This commit is contained in:
@@ -1,278 +1,148 @@
|
||||
<?php
|
||||
// Récupération des compteurs de congés avec gestion des exercices
|
||||
// Exercice CP: 01/06 au 31/05 | Exercice RTT: 01/01 au 31/12
|
||||
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
header("Access-Control-Allow-Methods: GET, OPTIONS");
|
||||
header("Access-Control-Allow-Headers: Content-Type");
|
||||
|
||||
// Gère la requête OPTIONS (pré-vol CORS)
|
||||
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
|
||||
http_response_code(200);
|
||||
exit();
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
// Log des erreurs pour debug
|
||||
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) { echo json_encode(["success"=>false,"message"=>"Erreur DB: ".$conn->connect_error]); exit(); }
|
||||
|
||||
$host = "192.168.0.4";
|
||||
$dbname = "DemandeConge";
|
||||
$username = "wpuser";
|
||||
$password = "-2b/)ru5/Bi8P[7_";
|
||||
$userId = isset($_GET['user_id']) ? (int)$_GET['user_id'] : null;
|
||||
if (!$userId) { echo json_encode(["success"=>false,"message"=>"user_id manquant"]); $conn->close(); exit(); }
|
||||
|
||||
// IMPORTANT: Changer ces paramètres pour votre configuration locale
|
||||
// $host = "localhost";
|
||||
// $username = "root";
|
||||
// $password = "";
|
||||
function getLeaveYear($date=null){ $d=$date?new DateTime($date):new DateTime(); $y=(int)$d->format('Y'); return ((int)$d->format('m')<6)?$y-1:$y;}
|
||||
function getRTTYear($date=null){ $d=$date?new DateTime($date):new DateTime(); return (int)$d->format('Y');}
|
||||
function getWorkingDays($start,$end){ $c=new DateTime($start); $e=new DateTime($end); $days=0; while($c<=$e){ $n=(int)$c->format('N'); if($n<6) $days++; $c->modify('+1 day'); } return $days;}
|
||||
|
||||
// Crée une nouvelle connexion à la base de données
|
||||
$conn = new mysqli($host, $username, $password, $dbname);
|
||||
// Récupérer les typeIds utiles
|
||||
function getTypeId($conn,$name){ $s=$conn->prepare("SELECT Id FROM TypeConge WHERE Nom=?"); $s->bind_param("s",$name); $s->execute(); $res=$s->get_result(); $id=null; if($r=$res->fetch_assoc()) $id=(int)$r['Id']; $s->close(); return $id; }
|
||||
$cpTypeId = getTypeId($conn,'Congé payé');
|
||||
$rttTypeId = getTypeId($conn,'RTT');
|
||||
$absTypeId = getTypeId($conn,'Congé maladie');
|
||||
|
||||
// Vérifie la connexion
|
||||
if ($conn->connect_error) {
|
||||
error_log("Erreur connexion DB getLeaveCounters: " . $conn->connect_error);
|
||||
echo json_encode(["success" => false, "message" => "Erreur de connexion à la base de données : " . $conn->connect_error]);
|
||||
exit();
|
||||
$leaveYear = getLeaveYear();
|
||||
$rttYear = getRTTYear();
|
||||
$currentDate = date('Y-m-d');
|
||||
|
||||
// --- Soldes initiaux (CompteurConges) restent inchangés ---
|
||||
$cpSolde = 0; $rttSolde = 0; $absSolde = 0;
|
||||
if ($cpTypeId !== null) {
|
||||
$q="SELECT Solde FROM CompteurConges WHERE EmployeeId=? AND TypeCongeId=? AND Annee=?";
|
||||
$s=$conn->prepare($q); $s->bind_param("iii",$userId,$cpTypeId,$leaveYear); $s->execute(); $res=$s->get_result(); if($r=$res->fetch_assoc()) $cpSolde=$r['Solde']; $s->close();
|
||||
}
|
||||
if ($rttTypeId !== null) {
|
||||
$q="SELECT Solde FROM CompteurConges WHERE EmployeeId=? AND TypeCongeId=? AND Annee=?";
|
||||
$s=$conn->prepare($q); $s->bind_param("iii",$userId,$rttTypeId,$rttYear); $s->execute(); $res=$s->get_result(); if($r=$res->fetch_assoc()) $rttSolde=$r['Solde']; $s->close();
|
||||
}
|
||||
if ($absTypeId !== null) {
|
||||
$q="SELECT Solde FROM CompteurConges WHERE EmployeeId=? AND TypeCongeId=? AND Annee=?";
|
||||
$s=$conn->prepare($q); $s->bind_param("iii",$userId,$absTypeId,$rttYear); $s->execute(); $res=$s->get_result(); if($r=$res->fetch_assoc()) $absSolde=$r['Solde']; $s->close();
|
||||
}
|
||||
|
||||
// Récupère l'ID utilisateur depuis les paramètres de requête GET
|
||||
$userId = $_GET['user_id'] ?? null;
|
||||
|
||||
error_log("=== DEBUT getLeaveCounters.php ===");
|
||||
error_log("getLeaveCounters - user_id reçu: " . ($userId ?? 'NULL'));
|
||||
error_log("getLeaveCounters - Toutes les variables GET: " . print_r($_GET, true));
|
||||
|
||||
if ($userId === null) {
|
||||
error_log("getLeaveCounters - user_id manquant");
|
||||
echo json_encode(["success" => false, "message" => "ID utilisateur manquant."]);
|
||||
exit();
|
||||
}
|
||||
// Fonction pour déterminer l'exercice des congés payés (01/06 au 31/05)
|
||||
function getLeaveYear($date = null) {
|
||||
if ($date === null) {
|
||||
$date = new DateTime();
|
||||
} else {
|
||||
$date = new DateTime($date);
|
||||
}
|
||||
|
||||
$currentYear = (int)$date->format('Y');
|
||||
$currentMonth = (int)$date->format('m');
|
||||
|
||||
// Si on est avant le 1er juin, l'exercice a commencé l'année précédente
|
||||
if ($currentMonth < 6) {
|
||||
return $currentYear - 1;
|
||||
}
|
||||
// Si on est le 1er juin ou après, l'exercice a commencé cette année
|
||||
return $currentYear;
|
||||
}
|
||||
|
||||
// Fonction pour déterminer l'année RTT (01/01 au 31/12)
|
||||
function getRTTYear($date = null) {
|
||||
if ($date === null) {
|
||||
$date = new DateTime();
|
||||
} else {
|
||||
$date = new DateTime($date);
|
||||
}
|
||||
|
||||
return (int)$date->format('Y');
|
||||
}
|
||||
|
||||
// Récupère l'ID utilisateur depuis les paramètres de requête GET
|
||||
$userId = $_GET['user_id'] ?? null;
|
||||
|
||||
if ($userId === null) {
|
||||
echo json_encode(["success" => false, "message" => "ID utilisateur manquant."]);
|
||||
exit();
|
||||
}
|
||||
|
||||
// Calcul des exercices selon les règles de gestion
|
||||
$leaveYear = getLeaveYear(); // Exercice CP (01/06 au 31/05)
|
||||
$rttYear = getRTTYear(); // Exercice RTT (01/01 au 31/12)
|
||||
$currentDate = date('Y-m-d'); // Date actuelle pour les filtres de demandes
|
||||
|
||||
// Variables pour les soldes disponibles
|
||||
$cpSolde = 0;
|
||||
$rttSolde = 0;
|
||||
$absSolde = 0;
|
||||
|
||||
// Variables pour les demandes en cours/validées
|
||||
// --- Calcul CP in process : priorité DemandeCongeType, fallback = working days on DemandeConge ---
|
||||
$cpInProcess = 0;
|
||||
$rttInProcess = 0;
|
||||
$absenteism = 0;
|
||||
|
||||
// --- FONCTION UTILITAIRE POUR CALCULER LES JOURS OUVRÉS (hors week-ends) ---
|
||||
function getWorkingDays($startDate, $endDate) {
|
||||
$workingDays = 0;
|
||||
$current = new DateTime($startDate);
|
||||
$end = new DateTime($endDate);
|
||||
|
||||
while ($current <= $end) {
|
||||
$dayOfWeek = (int)$current->format('N'); // 1 (pour Lundi) à 7 (pour Dimanche)
|
||||
if ($dayOfWeek < 6) { // Si ce n'est ni Samedi (6) ni Dimanche (7)
|
||||
$workingDays++;
|
||||
if ($cpTypeId !== null) {
|
||||
$sql = "
|
||||
SELECT dc.Id, dc.DateDebut, dc.DateFin, dct.NombreJours
|
||||
FROM DemandeConge dc
|
||||
LEFT JOIN DemandeCongeType dct
|
||||
ON dct.DemandeCongeId = dc.Id AND dct.TypeCongeId = ?
|
||||
WHERE dc.EmployeeId = ?
|
||||
AND dc.Statut IN ('En attente','Validée')
|
||||
AND dc.DateFin >= ?
|
||||
AND (dct.NombreJours IS NOT NULL OR FIND_IN_SET(?, dc.TypeCongeId))
|
||||
";
|
||||
$s = $conn->prepare($sql);
|
||||
$s->bind_param("iiss", $cpTypeId, $userId, $currentDate, $cpTypeId);
|
||||
$s->execute();
|
||||
$res = $s->get_result();
|
||||
while ($r = $res->fetch_assoc()) {
|
||||
if ($r['NombreJours'] !== null) {
|
||||
$cpInProcess += (float)$r['NombreJours'];
|
||||
} else {
|
||||
$cpInProcess += getWorkingDays($r['DateDebut'], $r['DateFin']);
|
||||
}
|
||||
$current->modify('+1 day');
|
||||
}
|
||||
return $workingDays;
|
||||
$s->close();
|
||||
}
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
|
||||
// --- Récupération du Solde de Congé Payé (CP) ---
|
||||
$queryCPSolde = "SELECT cc.Solde FROM CompteurConges cc
|
||||
JOIN TypeConge tc ON cc.TypeCongeId = tc.Id
|
||||
WHERE cc.EmployeeId = ? AND tc.Nom = 'Congé payé' AND cc.Annee = ?";
|
||||
$stmtCPSolde = $conn->prepare($queryCPSolde);
|
||||
if ($stmtCPSolde === false) {
|
||||
error_log("Erreur de préparation de la requête CP Solde : " . $conn->error);
|
||||
} else {
|
||||
$stmtCPSolde->bind_param("ii", $userId, $leaveYear);
|
||||
$stmtCPSolde->execute();
|
||||
$resultCPSolde = $stmtCPSolde->get_result();
|
||||
if ($rowCPSolde = $resultCPSolde->fetch_assoc()) {
|
||||
$cpSolde = $rowCPSolde['Solde'];
|
||||
// --- Calcul RTT in process (même logique) ---
|
||||
$rttInProcess = 0;
|
||||
if ($rttTypeId !== null) {
|
||||
$sql = "
|
||||
SELECT dc.Id, dc.DateDebut, dc.DateFin, dct.NombreJours
|
||||
FROM DemandeConge dc
|
||||
LEFT JOIN DemandeCongeType dct
|
||||
ON dct.DemandeCongeId = dc.Id AND dct.TypeCongeId = ?
|
||||
WHERE dc.EmployeeId = ?
|
||||
AND dc.Statut IN ('En attente','Validée')
|
||||
AND dc.DateFin >= ?
|
||||
AND (dct.NombreJours IS NOT NULL OR FIND_IN_SET(?, dc.TypeCongeId))
|
||||
";
|
||||
$s = $conn->prepare($sql);
|
||||
$s->bind_param("iiss", $rttTypeId, $userId, $currentDate, $rttTypeId);
|
||||
$s->execute();
|
||||
$res = $s->get_result();
|
||||
while ($r = $res->fetch_assoc()) {
|
||||
if ($r['NombreJours'] !== null) {
|
||||
$rttInProcess += (float)$r['NombreJours'];
|
||||
} else {
|
||||
$rttInProcess += getWorkingDays($r['DateDebut'], $r['DateFin']);
|
||||
}
|
||||
}
|
||||
$stmtCPSolde->close();
|
||||
$s->close();
|
||||
}
|
||||
|
||||
// --- Récupération du Solde de RTT ---
|
||||
$queryRTTSolde = "SELECT cc.Solde FROM CompteurConges cc
|
||||
JOIN TypeConge tc ON tc.Id = cc.TypeCongeId
|
||||
WHERE cc.EmployeeId = ? AND tc.Nom = 'RTT' AND cc.Annee = ?";
|
||||
$stmtRTTSolde = $conn->prepare($queryRTTSolde);
|
||||
if ($stmtRTTSolde === false) {
|
||||
error_log("Erreur de préparation de la requête RTT Solde : " . $conn->error);
|
||||
} else {
|
||||
$stmtRTTSolde->bind_param("ii", $userId, $rttYear);
|
||||
$stmtRTTSolde->execute();
|
||||
$resultRTTSolde = $stmtRTTSolde->get_result();
|
||||
if ($rowRTTSolde = $resultRTTSolde->fetch_assoc()) {
|
||||
$rttSolde = $rowRTTSolde['Solde'];
|
||||
// --- Calcul absenteisme (validation) : priorité DemandeCongeType, fallback = DATEDIFF+1 ---
|
||||
$absenteism = 0;
|
||||
if ($absTypeId !== null) {
|
||||
$sql = "
|
||||
SELECT dc.DateDebut, dc.DateFin, dct.NombreJours
|
||||
FROM DemandeConge dc
|
||||
LEFT JOIN DemandeCongeType dct
|
||||
ON dct.DemandeCongeId = dc.Id AND dct.TypeCongeId = ?
|
||||
WHERE dc.EmployeeId = ?
|
||||
AND dc.Statut = 'Validée'
|
||||
AND (dct.NombreJours IS NOT NULL OR FIND_IN_SET(?, dc.TypeCongeId))
|
||||
";
|
||||
$s = $conn->prepare($sql);
|
||||
$s->bind_param("iii", $absTypeId, $userId, $absTypeId);
|
||||
$s->execute();
|
||||
$res = $s->get_result();
|
||||
while ($r = $res->fetch_assoc()) {
|
||||
if ($r['NombreJours'] !== null) {
|
||||
$absenteism += (float)$r['NombreJours'];
|
||||
} else {
|
||||
// fallback : DATEDIFF + 1
|
||||
$d1 = new DateTime($r['DateDebut']); $d2 = new DateTime($r['DateFin']);
|
||||
$absenteism += ($d2->diff($d1)->days + 1);
|
||||
}
|
||||
}
|
||||
$stmtRTTSolde->close();
|
||||
$s->close();
|
||||
}
|
||||
|
||||
// --- Récupération du Solde de Congé Maladie (ABS) ---
|
||||
$queryABSSolde = "SELECT cc.Solde FROM CompteurConges cc
|
||||
JOIN TypeConge tc ON tc.Id = cc.TypeCongeId
|
||||
WHERE cc.EmployeeId = ? AND tc.Nom = 'Congé maladie' AND cc.Annee = ?";
|
||||
$stmtABSSolde = $conn->prepare($queryABSSolde);
|
||||
if ($stmtABSSolde === false) {
|
||||
error_log("Erreur de préparation de la requête ABS Solde : " . $conn->error);
|
||||
} else {
|
||||
$stmtABSSolde->bind_param("ii", $userId, $rttYear);
|
||||
$stmtABSSolde->execute();
|
||||
$resultABSSolde = $stmtABSSolde->get_result();
|
||||
if ($rowABSSolde = $resultABSSolde->fetch_assoc()) {
|
||||
$absSolde = $rowABSSolde['Solde'];
|
||||
}
|
||||
$stmtABSSolde->close();
|
||||
}
|
||||
$availableCPCalculated = max(0, $cpSolde - $cpInProcess);
|
||||
$availableRTTCalculated = max(0, $rttSolde - $rttInProcess);
|
||||
|
||||
|
||||
// --- Calcul des Congés Payés (CP) en cours (demandes 'En attente' ou 'Validée' dont la fin est >= date actuelle) ---
|
||||
// Cette requête sélectionne les dates pour le calcul en PHP
|
||||
$queryCPInProcessDates = "SELECT dc.DateDebut, dc.DateFin FROM DemandeConge dc
|
||||
JOIN TypeConge tc ON dc.TypeCongeId = tc.Id
|
||||
WHERE dc.EmployeeId = ?
|
||||
AND tc.Nom = 'Congé payé'
|
||||
AND dc.Statut IN ('En attente', 'Validée')
|
||||
AND dc.DateFin >= ?";
|
||||
$stmtCPInProcessDates = $conn->prepare($queryCPInProcessDates);
|
||||
if ($stmtCPInProcessDates === false) {
|
||||
error_log("Erreur de préparation de la requête CP en cours dates : " . $conn->error);
|
||||
} else {
|
||||
$stmtCPInProcessDates->bind_param("is", $userId, $currentDate);
|
||||
$stmtCPInProcessDates->execute();
|
||||
$resultCPInProcessDates = $stmtCPInProcessDates->get_result();
|
||||
while ($row = $resultCPInProcessDates->fetch_assoc()) {
|
||||
$cpInProcess += getWorkingDays($row['DateDebut'], $row['DateFin']);
|
||||
}
|
||||
$stmtCPInProcessDates->close();
|
||||
}
|
||||
|
||||
// --- Calcul des RTT en cours (mêmes critères que CP, mais pour RTT) ---
|
||||
$queryRTTInProcessDates = "SELECT dc.DateDebut, dc.DateFin FROM DemandeConge dc
|
||||
JOIN TypeConge tc ON dc.TypeCongeId = tc.Id
|
||||
WHERE dc.EmployeeId = ?
|
||||
AND tc.Nom = 'RTT'
|
||||
AND dc.Statut IN ('En attente', 'Validée')
|
||||
AND dc.DateFin >= ?";
|
||||
$stmtRTTInProcessDates = $conn->prepare($queryRTTInProcessDates);
|
||||
if ($stmtRTTInProcessDates === false) {
|
||||
error_log("Erreur de préparation de la requête RTT en cours dates : " . $conn->error);
|
||||
} else {
|
||||
$stmtRTTInProcessDates->bind_param("is", $userId, $currentDate);
|
||||
$stmtRTTInProcessDates->execute();
|
||||
$resultRTTInProcessDates = $stmtRTTInProcessDates->get_result();
|
||||
while ($row = $resultRTTInProcessDates->fetch_assoc()) {
|
||||
$rttInProcess += getWorkingDays($row['DateDebut'], $row['DateFin']);
|
||||
}
|
||||
$stmtRTTInProcessDates->close();
|
||||
}
|
||||
|
||||
|
||||
// --- Calcul des jours d'absence (ABS) (somme des jours DATEDIFF à partir de DemandeConge) ---
|
||||
// Note: Ici, on ne modifie pas le calcul, car l'absentéisme maladie est souvent compté sur tous les jours, y compris week-ends, pour le suivi global.
|
||||
// Si vous devez exclure les week-ends pour les ABS, appliquez getWorkingDays ici aussi.
|
||||
$queryABSInProcess = "SELECT SUM(DATEDIFF(dc.DateFin, dc.DateDebut) + 1) AS total_abs FROM DemandeConge dc
|
||||
JOIN TypeConge tc ON dc.TypeCongeId = tc.Id
|
||||
WHERE dc.EmployeeId = ?
|
||||
AND tc.Nom = 'Congé maladie'
|
||||
AND dc.Statut = 'Validée'";
|
||||
$stmtABSInProcess = $conn->prepare($queryABSInProcess);
|
||||
if ($stmtABSInProcess === false) {
|
||||
error_log("Erreur de préparation de la requête ABS en cours : " . $conn->error);
|
||||
} else {
|
||||
$stmtABSInProcess->bind_param("i", $userId);
|
||||
$stmtABSInProcess->execute();
|
||||
$resultABSInProcess = $stmtABSInProcess->get_result();
|
||||
if ($rowABSInProcess = $resultABSInProcess->fetch_assoc()) {
|
||||
$absenteism = $rowABSInProcess['total_abs'] ?? 0;
|
||||
}
|
||||
$stmtABSInProcess->close();
|
||||
}
|
||||
|
||||
// --- Calcul des soldes disponibles réels (déduction "douce" pour l'affichage/validation frontend) ---
|
||||
$availableCPCalculated = $cpSolde - $cpInProcess;
|
||||
if ($availableCPCalculated < 0) {
|
||||
$availableCPCalculated = 0;
|
||||
}
|
||||
|
||||
$availableRTTCalculated = $rttSolde - $rttInProcess;
|
||||
if ($availableRTTCalculated < 0) {
|
||||
$availableRTTCalculated = 0;
|
||||
}
|
||||
|
||||
|
||||
// Renvoie les compteurs sous format JSON
|
||||
echo json_encode([
|
||||
"success" => true,
|
||||
"message" => "Compteurs récupérés avec succès.",
|
||||
"counters" => [
|
||||
"availableCP" => (int)$availableCPCalculated, // CP: Solde brut - jours ouvrés en cours/validés futurs
|
||||
"availableRTT" => (int)$availableRTTCalculated, // RTT: Solde brut - jours ouvrés en cours/validés futurs
|
||||
"availableABS" => (int)$absSolde, // ABS: Solde brut (sans déduction des jours en cours)
|
||||
"rttInProcess" => (int)$rttInProcess, // RTT: Jours ouvrés en attente/validés futurs (pour information)
|
||||
"absenteism" => (int)$absenteism // ABS: Jours d'absence maladie validés/pris (pour information)
|
||||
],
|
||||
"debug_values" => [
|
||||
"initial_cp_solde" => (int)$cpSolde,
|
||||
"cp_en_cours" => (int)$cpInProcess,
|
||||
"calculated_available_cp" => (int)$availableCPCalculated,
|
||||
"initial_rtt_solde" => (int)$rttSolde,
|
||||
"rtt_en_cours" => (int)$rttInProcess,
|
||||
"calculated_available_rtt" => (int)$availableRTTCalculated,
|
||||
"leave_year" => $leaveYear,
|
||||
"rtt_year" => $rttYear,
|
||||
"current_date_php" => $currentDate,
|
||||
"user_id_php" => (int)$userId
|
||||
]
|
||||
"success" => true,
|
||||
"message" => "Compteurs récupérés avec succès.",
|
||||
"counters" => [
|
||||
"availableCP" => (int)$availableCPCalculated,
|
||||
"availableRTT" => (int)$availableRTTCalculated,
|
||||
"availableABS" => (int)$absSolde,
|
||||
"rttInProcess" => (int)$rttInProcess,
|
||||
"absenteism" => (int)$absenteism
|
||||
],
|
||||
"debug" => [
|
||||
"cpSolde"=>$cpSolde,"cpInProcess"=>$cpInProcess,
|
||||
"rttSolde"=>$rttSolde,"rttInProcess"=>$rttInProcess,
|
||||
"absSolde"=>$absSolde,"absenteism"=>$absenteism
|
||||
]
|
||||
]);
|
||||
|
||||
$conn->close();
|
||||
?>
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user