getData.php 28 KB
Newer Older
Hamza Arfaoui's avatar
Hamza Arfaoui committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);


session_start();
require_once '../../../inc/confNetisse.php';
require_once '../../../admin/functions.php';

$result = array("status" =>false, "content" => "");

$vars = $_GET;

$DATA = [];

$filter = "";
$values = [];

if(isset($vars['ap']) && $vars['ap']) {
	$filter .= " AND a.id_agence_principale = :ap";
	$values["ap"] = $vars['ap'];
}
if(isset($vars['agence']) && $vars['agence']) {
	$filter .= " AND h.id_agence = :agence";
	$values["agence"] = $vars['agence'];
}
if(isset($_SESSION['profil']) && $_SESSION['profil'] != 100 && $filter == ''){
	$agences = implode(',', $_SESSION['agences']);
	$filter = " AND h.id_agence IN(".$agences.")";
}

switch ($vars['datePreset'])
{
	case 'today':
		$startDate = 'today';
		$endDate = 'today';
		break;

	case 'yesterday':
		$startDate = 'yesterday';
		$endDate = 'yesterday';
		break;

	case 'current-week':
		$startDate = 'next monday -1 week';
		$endDate = date('Y-m-d');
		break;

	case 'last-week':
		$startDate = 'monday last week';
		$endDate = 'sunday last week';
		break;

	case 'last-7-days':
		$startDate = '-7 days';
		$endDate = 'yesterday';
		break;

	case 'current-month':
		$startDate =  date('Y-m-d', mktime(0,0,0,date('m'),1,date('Y')));
		$endDate = date('Y-m-d');
		break;

	case 'last-month':
		$startDate = date("Y-m-d", mktime(0, 0, 0, date("m") - 1, 1));
		$endDate = date("Y-m-d", mktime(0, 0, 0, date("m"), 0));
		break;

	case 'last-30-days':
		$startDate = '-30 days';
		$endDate = 'yesterday';
		break;

	case 'current-year':
		$startDate = '1/1 this year';
		$endDate = date('Y-m-d');
		break;

	case 'last-year':
		$startDate = '1/1 last year';
		$endDate = '1/1 this year -1 day';
		break;

	case 'custom':
		$startDate = $vars['startDate'];
		$endDate = $vars['endDate'];
		break;
        case 'custom-year':
		$startDate = $vars['startYear'];
		$endDate = $vars['endYear'];
		break;
}


$vars['startDate'] = $vars['datePreset'] == 'custom-year' ? date('Y-m-d', strtotime($startDate."-01-01 00:00:00")) : date('Y-m-d 00:00:00',strtotime($startDate));
$vars['endDate'] = $vars['datePreset'] == 'custom-year' ? date('Y-m-d', strtotime($endDate."-12-31 23:59:59")) : date('Y-m-d 23:59:59',strtotime($endDate));

$values['startDate'] = $vars['startDate'];
$values['endDate'] = $vars['endDate'];


/**
 * Get agents RDV en agence STATS
 */

$q = "SELECT c.nom, c.prenom, c.id_agent matricule, h.id_agence, a.nom_agence, f.libelle as fonction_conseiller, @agent:=h.id_action AS agent,
COALESCE(SUM(CASE WHEN rdv=1 AND h.id_conseiller = c2.id_conseiller THEN 1 ELSE 0 END),0) AS nb_rdv_with_agent,
COALESCE(SUM(CASE WHEN rdv=1 AND h.id_action = c.id_conseiller THEN 1 ELSE 0 END),0) AS nb_rdv_handled_by_agent,
COALESCE(SUM(CASE WHEN rdv=0 AND h.id_action = c.id_conseiller THEN 1 ELSE 0 END),0) AS nb_srdv_handled_by_agent,
COALESCE(SUM(CASE WHEN rdv=5 AND h.id_action = c.id_conseiller THEN 1 ELSE 0 END),0) AS nb_ship_handled_by_agent,
COALESCE(AVG(CASE WHEN date_priseencharge > date_arrivee AND rdv=1 THEN timestampdiff(SECOND,date_arrivee,date_priseencharge) END),0) AS da_rdv_agent,
COALESCE(AVG(CASE WHEN date_priseencharge > date_arrivee AND rdv=0 THEN timestampdiff(SECOND,date_arrivee,date_priseencharge) END),0) AS da_srdv_agent,
COALESCE(AVG(CASE WHEN date_priseencharge > date_arrivee AND rdv=5 THEN timestampdiff(SECOND,date_arrivee,date_priseencharge) END),0) AS da_ship_agent,
COALESCE(AVG(CASE WHEN date_cloture > date_priseencharge AND rdv=1 THEN timestampdiff(SECOND,date_priseencharge,date_cloture) END),0) AS de_rdv_agent,
COALESCE(AVG(CASE WHEN date_cloture > date_priseencharge AND rdv=0 THEN timestampdiff(SECOND,date_priseencharge,date_cloture) END),0) AS de_srdv_agent,
COALESCE(AVG(CASE WHEN date_cloture > date_priseencharge AND rdv=5 THEN timestampdiff(SECOND,date_priseencharge,date_cloture) END),0) AS de_ship_agent
FROM `ca_historique_clients` h
LEFT JOIN ca_conseillers c ON c.id_conseiller = h.id_action
LEFT JOIN ca_conseillers c2 ON c2.id_conseiller = h.id_conseiller
LEFT JOIN ca_fonctions f ON f.id_fonction = c.fonction
LEFT JOIN ca_agences a ON a.id_agence = h.id_agence
WHERE a.supp=0 AND c.supp=0 AND c.nom IS NOT NULL AND date_arrivee BETWEEN :startDate AND :endDate ".$filter."
GROUP BY id_action, h.id_agence
ORDER BY id_action, h.id_agence";
// var_dump($values);
$r = excuteQuery($q, $values);
if (!$r['status']) {
	$result["content"] = $r['stmt']->errorInfo();
}
    
while($o = $r['stmt']->fetch(PDO::FETCH_OBJ)) {
	if(!isset($DATA[$o->matricule])) {
		$DATA[$o->matricule] = [];
	}
	if(!isset($DATA[$o->matricule][$o->id_agence])) {
		$o->nb_rdv_with_agent = 0;
		$DATA[$o->matricule][$o->id_agence] = $o;
	} else {
		// $DATA[$o->matricule][$o->id_agence]->nb_rdv_with_agent += $o->nb_rdv_with_agent;
		$DATA[$o->matricule][$o->id_agence]->nb_rdv_handled_by_agent += $o->nb_rdv_handled_by_agent;
		$DATA[$o->matricule][$o->id_agence]->nb_srdv_handled_by_agent += $o->nb_srdv_handled_by_agent;
		$DATA[$o->matricule][$o->id_agence]->nb_ship_handled_by_agent += $o->nb_ship_handled_by_agent;
		$DATA[$o->matricule][$o->id_agence]->da_rdv_agent = ($DATA[$o->matricule][$o->id_agence]->da_rdv_agent + $o->da_rdv_agent)/2;
		$DATA[$o->matricule][$o->id_agence]->da_srdv_agent = ($DATA[$o->matricule][$o->id_agence]->da_srdv_agent + $o->da_srdv_agent)/2;
		$DATA[$o->matricule][$o->id_agence]->da_ship_agent = ($DATA[$o->matricule][$o->id_agence]->da_ship_agent + $o->da_ship_agent)/2;
		$DATA[$o->matricule][$o->id_agence]->de_rdv_agent = ($DATA[$o->matricule][$o->id_agence]->de_rdv_agent + $o->de_rdv_agent)/2;
		$DATA[$o->matricule][$o->id_agence]->de_srdv_agent = ($DATA[$o->matricule][$o->id_agence]->de_srdv_agent + $o->de_srdv_agent)/2;
		$DATA[$o->matricule][$o->id_agence]->de_ship_agent = ($DATA[$o->matricule][$o->id_agence]->de_ship_agent + $o->de_ship_agent)/2;
	}
	
}

/**************

FOR RDV WITH AGENTS

*****************/
$q = "SELECT c.nom, c.prenom, c.id_agent matricule, h.id_agence, a.nom_agence, f.libelle as fonction_conseiller, @agent:=h.id_action AS agent,
COALESCE(SUM(CASE WHEN rdv=1 AND h.id_conseiller = c.id_conseiller THEN 1 ELSE 0 END),0) AS nb_rdv_with_agent,
COALESCE(SUM(CASE WHEN rdv=1 AND h.id_action = c.id_conseiller THEN 1 ELSE 0 END),0) AS nb_rdv_handled_by_agent,
COALESCE(SUM(CASE WHEN rdv=0 AND h.id_action = c.id_conseiller THEN 1 ELSE 0 END),0) AS nb_srdv_handled_by_agent,
COALESCE(SUM(CASE WHEN rdv=5 AND h.id_action = c.id_conseiller THEN 1 ELSE 0 END),0) AS nb_ship_handled_by_agent,
COALESCE(AVG(CASE WHEN date_priseencharge > date_arrivee AND rdv=1 THEN timestampdiff(SECOND,date_arrivee,date_priseencharge) END),0) AS da_rdv_agent,
COALESCE(AVG(CASE WHEN date_priseencharge > date_arrivee AND rdv=0 THEN timestampdiff(SECOND,date_arrivee,date_priseencharge) END),0) AS da_srdv_agent,
COALESCE(AVG(CASE WHEN date_priseencharge > date_arrivee AND rdv=5 THEN timestampdiff(SECOND,date_arrivee,date_priseencharge) END),0) AS da_ship_agent,
COALESCE(AVG(CASE WHEN date_cloture > date_priseencharge AND rdv=1 THEN timestampdiff(SECOND,date_priseencharge,date_cloture) END),0) AS de_rdv_agent,
COALESCE(AVG(CASE WHEN date_cloture > date_priseencharge AND rdv=0 THEN timestampdiff(SECOND,date_priseencharge,date_cloture) END),0) AS de_srdv_agent,
COALESCE(AVG(CASE WHEN date_cloture > date_priseencharge AND rdv=5 THEN timestampdiff(SECOND,date_priseencharge,date_cloture) END),0) AS de_ship_agent
FROM `ca_historique_clients` h
LEFT JOIN ca_conseillers c ON c.id_conseiller = h.id_conseiller
LEFT JOIN ca_fonctions f ON f.id_fonction = c.fonction
LEFT JOIN ca_agences a ON a.id_agence = h.id_agence
WHERE a.supp=0 AND c.supp=0 AND c.nom IS NOT NULL AND date_arrivee BETWEEN :startDate AND :endDate ".$filter."
GROUP BY h.id_conseiller, h.id_agence, c.nom, c.prenom, c.id_agent, a.nom_agence, f.libelle
ORDER BY id_action, h.id_agence";
// var_dump($values);
$r = excuteQuery($q, $values);
if (!$r['status']) {
	$result["content"] = $r['stmt']->errorInfo();
}
    
while($o = $r['stmt']->fetch(PDO::FETCH_OBJ)) {
	if(!isset($DATA[$o->matricule])) {
		$DATA[$o->matricule] = [];
	}
	if(!isset($DATA[$o->matricule][$o->id_agence])) {
		$DATA[$o->matricule][$o->id_agence] = $o;
	} else {
		$DATA[$o->matricule][$o->id_agence]->nb_rdv_with_agent += intval($o->nb_rdv_with_agent);
	}
	
}
/*********************/

/**
 * Get agents RDV à distance STATS
 */

$visio = (isset($GLOBALS["PILE_CONF"]["VISIO"]) && isset($GLOBALS["PILE_CONF"]["VISIO"]["ENABLED"]) && $GLOBALS["PILE_CONF"]["VISIO"]["ENABLED"]) ? true : false;

if($visio){

	$filter_date = " AND date_debut BETWEEN :startDate AND :endDate";

	$q = "SELECT c.nom, c.prenom,c.id_agent matricule, h.id_agence, a.nom_agence, f.libelle as fonction_conseiller,
	0 AS nb_rdv_visio_with_agent,
	COALESCE(SUM(CASE WHEN h.id_conseiller=c.id_conseiller THEN 1 ELSE 0 END),0) AS nb_rdv_visio_handled_by_agent,
	COALESCE(AVG(CASE WHEN date_priseencharge > h.date_debut THEN timestampdiff(SECOND,h.date_debut,date_priseencharge) END),0) AS da_rdv_visio_agent
	FROM ca_historique_rdv_visio h
	LEFT JOIN ca_rdv_calendar r ON r.id = h.id
	LEFT JOIN ca_conseillers c ON c.id_conseiller = h.id_conseiller
	LEFT JOIN ca_fonctions f ON f.id_fonction = c.fonction
	LEFT JOIN ca_agences a ON a.id_agence = h.id_agence
	WHERE a.supp=0 AND c.supp=0 AND c.nom IS NOT NULL AND h.rdvtype IN (2,3) AND h.date_debut BETWEEN :startDate AND :endDate ".$filter."
	GROUP BY h.id_conseiller, h.id_agence, c.nom, c.prenom, c.id_agent, a.nom_agence, f.libelle
	ORDER BY h.id_conseiller, h.id_agence";

	$r = excuteQuery($q, $values);
	if (!$r['status']) {
		$result["content"] = $r['stmt']->errorInfo();
	}
	while($o = $r['stmt']->fetch(PDO::FETCH_OBJ)) {
		if(!isset($DATA[$o->matricule])) {
			$DATA[$o->matricule] = array();
		}
		if(isset($DATA[$o->matricule][$o->id_agence])) {
			$row = $DATA[$o->matricule][$o->id_agence];
			$row->nb_rdv_visio_handled_by_agent += $o->nb_rdv_visio_handled_by_agent;
			$row->da_rdv_visio_agent += $o->da_rdv_visio_agent;
			$DATA[$o->matricule][$o->id_agence] = $row;
		} else {
			$DATA[$o->matricule][$o->id_agence] = $o;
		}
	}

	/***** SOLLICITES ****/

	$q = "SELECT c.nom, c.prenom,c.id_agent matricule, h.id_agence, a.nom_agence, f.libelle as fonction_conseiller,
	COALESCE(SUM(CASE WHEN h.matricule=c.id_agent THEN 1 ELSE 0 END),0) AS nb_rdv_visio_with_agent,
	0 AS nb_rdv_visio_handled_by_agent,
	0 AS da_rdv_visio_agent
	FROM ca_historique_rdv_visio h
	LEFT JOIN ca_rdv_calendar r ON r.id = h.id
	LEFT JOIN ca_conseillers c ON c.id_agent = h.matricule
	LEFT JOIN ca_fonctions f ON f.id_fonction = c.fonction
	LEFT JOIN ca_agences a ON a.id_agence = h.id_agence
	WHERE a.supp=0 AND c.supp=0 AND c.nom IS NOT NULL AND h.rdvtype IN (2,3) AND h.date_debut BETWEEN :startDate AND :endDate ".$filter."
	GROUP BY h.id_conseiller, h.id_agence, c.nom, c.prenom, c.id_agent, a.nom_agence, f.libelle
	ORDER BY h.id_conseiller, h.id_agence";

	$r = excuteQuery($q, $values);
	if (!$r['status']) {
		$result["content"] = $r['stmt']->errorInfo();
	}
	while($o = $r['stmt']->fetch(PDO::FETCH_OBJ)) {
		if(!isset($DATA[$o->matricule])) {
			$DATA[$o->matricule] = array();
		}
		if(isset($DATA[$o->matricule][$o->id_agence])) {
			$row = $DATA[$o->matricule][$o->id_agence];
			$row->nb_rdv_visio_with_agent += $o->nb_rdv_visio_with_agent;
			$DATA[$o->matricule][$o->id_agence] = $row;
		} else {
			$DATA[$o->matricule][$o->id_agence] = $o;
		}
	}
}

$agentGlobals = new stdClass();
$agencyGlobals = new stdClass();
?>
<table id="agentsTable">
	<thead>
		<tr>
			<th colspan="4">Conseiller</th>
			<th rowspan="2">Agence</th>
			<th colspan="7">En agence</th>
			<?php if($visio){ ?>
				<th colspan="3">A distance</th>
			<?php } ?>
			<th colspan="4">Global</th>
		</tr>
		<tr>
			<th>Matricule</th>
			<th>Nom</th>
			<th>Prénom</th>
			<th>Fonction</th>
			<th>Nombre de sollicitations RDV en agence</th>
			<th>Nombre de clients avec RDV pris en charge</th>
			<th>Nombre de clients sans RDV pris en charge</th>
			<th>Nombre de clients Livraison pris en charge</th>
			<th>Durée moyenne d'attente clients avec RDV</th>
			<th>Durée moyenne d'attente clients sans RDV</th>
			<th>Durée moyenne d'attente clients Livraion</th>
			<th>Durée moyenne d'entretien clients avec RDV</th>
			<th>Durée moyenne d'entretien clients sans RDV</th>
			<th>Durée moyenne d'entretien clients Livraison</th>
			<?php if($visio){ ?>
				<th>Nombre de sollicitations RDV à distance (tél, visio, …)</th>
				<th>Nombre de prises en charge RDV à distance</th>
				<th>Durée moyenne d'attente RDV à distance</th>
			<?php } ?>
			<th>Nombre total de prises en charge</th>
		</tr>
	</thead>
	<tbody>
	  <?php 
	  		$agencyGlobals_comp = 0;
			$agencyGlobals_rdv_comp = 0;
			$agencyGlobals_srdv_comp = 0;
			$agencyGlobals_visio_comp = 0;
			$agencyGlobals_nb_rdv_with_agent = 0;
			$agencyGlobals_nb_rdv_handled_by_agent = 0;
			$agencyGlobals_nb_srdv_handled_by_agent = 0;
			$agencyGlobals_nb_ship_handled_by_agent = 0;
			$agencyGlobals_da_rdv_agent = 0;
			$agencyGlobals_da_srdv_agent = 0;
			$agencyGlobals_da_ship_agent = 0;
			$agencyGlobals_de_rdv_agent = 0;
			$agencyGlobals_de_srdv_agent = 0;
			$agencyGlobals_de_ship_agent = 0;
			$agencyGlobals_nb_rdv_visio_with_agent = 0;
			$agencyGlobals_nb_rdv_visio_handled_by_agent = 0;
			$agencyGlobals_da_rdv_visio_agent = 0;
			$agencyGlobals_global = 0;
			$id_agence = 0;

	  	foreach($DATA as $matricule => $item) : ?>
		<?php $agentGlobals->{$matricule} = new stdClass(); $comp = 0; $zeroRDV = 0; $zeroSRDV = 0; $zeroSHIP = 0; $zeroVisio = 0;
			$agentGlobals->{$matricule}->nb_rdv_with_agent = $agentGlobals->{$matricule}->nb_rdv_with_agent ?? 0;
			$agentGlobals->{$matricule}->nb_rdv_handled_by_agent = $agentGlobals->{$matricule}->nb_rdv_handled_by_agent ?? 0;
			$agentGlobals->{$matricule}->nb_srdv_handled_by_agent = $agentGlobals->{$matricule}->nb_srdv_handled_by_agent ?? 0;
			$agentGlobals->{$matricule}->nb_ship_handled_by_agent = $agentGlobals->{$matricule}->nb_ship_handled_by_agent ?? 0;
			$agentGlobals->{$matricule}->da_rdv_agent = $agentGlobals->{$matricule}->da_rdv_agent ?? 0;
			$agentGlobals->{$matricule}->da_srdv_agent = $agentGlobals->{$matricule}->da_srdv_agent ?? 0;
			$agentGlobals->{$matricule}->da_ship_agent = $agentGlobals->{$matricule}->da_ship_agent ?? 0;
			$agentGlobals->{$matricule}->de_rdv_agent = $agentGlobals->{$matricule}->de_rdv_agent ?? 0;
			$agentGlobals->{$matricule}->de_srdv_agent = $agentGlobals->{$matricule}->de_srdv_agent ?? 0;
			$agentGlobals->{$matricule}->de_ship_agent = $agentGlobals->{$matricule}->de_ship_agent ?? 0;
			$agentGlobals->{$matricule}->nb_rdv_visio_with_agent = $agentGlobals->{$matricule}->nb_rdv_visio_with_agent ?? 0;
			$agentGlobals->{$matricule}->nb_rdv_visio_handled_by_agent = $agentGlobals->{$matricule}->nb_rdv_visio_handled_by_agent ?? 0;
			$agentGlobals->{$matricule}->da_rdv_visio_agent = 0;
			$agentGlobals->{$matricule}->global = $agentGlobals->{$matricule}->global ?? 0;

			
		?>
		<?php foreach($item as $id => $row) : ?>
		<?php 
			if($id_agence != $row->id_agence && $id_agence != 0){
				$agencyGlobals_comp = 0;
				$agencyGlobals_nb_rdv_with_agent = 0;
				$agencyGlobals_nb_rdv_handled_by_agent = 0;
				$agencyGlobals_nb_srdv_handled_by_agent = 0;
				$agencyGlobals_nb_ship_handled_by_agent = 0;
				$agencyGlobals_da_rdv_agent = 0;
				$agencyGlobals_da_srdv_agent = 0;
				$agencyGlobals_da_ship_agent = 0;
				$agencyGlobals_de_rdv_agent = 0;
				$agencyGlobals_de_srdv_agent = 0;
				$agencyGlobals_de_ship_agent = 0;
				$agencyGlobals_nb_rdv_visio_with_agent = 0;
				$agencyGlobals_nb_rdv_visio_handled_by_agent = 0;
				$agencyGlobals_da_rdv_visio_agent = 0;
				$agencyGlobals_global = 0;
			}
			if(empty($agencyGlobals->{$row->nom_agence})) {
				// echo '<li>--- New agency';
				$agencyGlobals->{$row->nom_agence} = new stdClass(); 
				$agencyGlobals->{$row->nom_agence}->nb_rdv_with_agent = 0;
				$agencyGlobals->{$row->nom_agence}->nb_rdv_handled_by_agent = 0;
				$agencyGlobals->{$row->nom_agence}->nb_srdv_handled_by_agent = 0;
				$agencyGlobals->{$row->nom_agence}->nb_ship_handled_by_agent = 0;
				$agencyGlobals->{$row->nom_agence}->da_rdv_agent = 0;
				$agencyGlobals->{$row->nom_agence}->da_srdv_agent = 0;
				$agencyGlobals->{$row->nom_agence}->da_ship_agent = 0;
				$agencyGlobals->{$row->nom_agence}->de_rdv_agent = 0;
				$agencyGlobals->{$row->nom_agence}->de_srdv_agent = 0;
				$agencyGlobals->{$row->nom_agence}->de_ship_agent = 0;
				$agencyGlobals->{$row->nom_agence}->global = 0;
			}

		?>
		<tr>
			<td><?=$matricule?></td>
			<td><?=$row->nom?></td>
			<td><?=$row->prenom?></td>
			<td><?=$row->fonction_conseiller?></td>
			<td><?=$row->nom_agence?></td>
			
			<td><?=$row->nb_rdv_with_agent?></td>
			<td><?=$row->nb_rdv_handled_by_agent?></td>
			<td><?=$row->nb_srdv_handled_by_agent?></td>
			<td><?=$row->nb_ship_handled_by_agent?></td>
			<td><?=sec2hms($row->da_rdv_agent)?></td>
			<td><?=sec2hms($row->da_srdv_agent)?></td>
			<td><?=sec2hms($row->da_ship_agent)?></td>
			<td><?=sec2hms($row->de_rdv_agent)?></td>
			<td><?=sec2hms($row->de_srdv_agent)?></td>
			<td><?=sec2hms($row->de_ship_agent)?></td>
			<?php if($visio){ ?>
				<td><?=$row->nb_rdv_visio_with_agent??'0'?></td>
				<td><?=$row->nb_rdv_visio_handled_by_agent??'0'?></td>
				<td><?= ($visio ? sec2hms($row->da_rdv_visio_agent) : '-')?></td>
			<?php } ?>
			<td><?= ($row->nb_rdv_handled_by_agent + $row->nb_srdv_handled_by_agent + $row->nb_ship_handled_by_agent + ($row->nb_rdv_visio_handled_by_agent ?? 0)) ?></td>
		</tr>
		<?php 
			if($row->nb_rdv_handled_by_agent == 0) $zeroRDV++;
			if($row->nb_srdv_handled_by_agent == 0) $zeroSRDV++;
			if($row->nb_ship_handled_by_agent == 0) $zeroSHIP++;
			if($visio){
				if($row->nb_rdv_visio_with_agent == 0) $zeroVisio++;
			}
			$comp ++;
			$agentGlobals->{$matricule}->nb_rdv_with_agent += $row->nb_rdv_with_agent;
			$agentGlobals->{$matricule}->nb_rdv_handled_by_agent += $row->nb_rdv_handled_by_agent;
			$agentGlobals->{$matricule}->nb_srdv_handled_by_agent += $row->nb_srdv_handled_by_agent;
			$agentGlobals->{$matricule}->nb_ship_handled_by_agent += $row->nb_ship_handled_by_agent;
			$agentGlobals->{$matricule}->da_rdv_agent += $row->nb_rdv_handled_by_agent ? $row->da_rdv_agent : 0;
			$agentGlobals->{$matricule}->da_srdv_agent += $row->nb_srdv_handled_by_agent ? $row->da_srdv_agent : 0;
			$agentGlobals->{$matricule}->da_ship_agent += $row->nb_ship_handled_by_agent ? $row->da_ship_agent : 0;
			$agentGlobals->{$matricule}->de_rdv_agent += $row->nb_rdv_handled_by_agent ? $row->de_rdv_agent : 0;
			$agentGlobals->{$matricule}->de_srdv_agent += $row->nb_srdv_handled_by_agent ? $row->de_srdv_agent : 0;
			$agentGlobals->{$matricule}->de_ship_agent += $row->nb_ship_handled_by_agent ? $row->de_ship_agent : 0;
			if($visio){
				$agentGlobals->{$matricule}->nb_rdv_visio_with_agent += $row->nb_rdv_visio_with_agent;
				$agentGlobals->{$matricule}->nb_rdv_visio_handled_by_agent += $row->nb_rdv_visio_handled_by_agent;
				$agentGlobals->{$matricule}->da_rdv_visio_agent += $row->nb_rdv_visio_handled_by_agent ? $row->da_rdv_visio_agent : 0;
			}
			$agentGlobals->{$matricule}->global += intval($row->nb_rdv_handled_by_agent) + intval($row->nb_srdv_handled_by_agent) + intval($row->nb_ship_handled_by_agent) + ($visio ? intval($row->nb_rdv_visio_handled_by_agent) : 0);
				  
			$agencyGlobals->{$row->nom_agence}->comp = $comp;
			$agencyGlobals->{$row->nom_agence}->nb_rdv_with_agent += $row->nb_rdv_with_agent;
			$agencyGlobals->{$row->nom_agence}->nb_rdv_handled_by_agent += $row->nb_rdv_handled_by_agent;
			$agencyGlobals->{$row->nom_agence}->nb_srdv_handled_by_agent += $row->nb_srdv_handled_by_agent;
			$agencyGlobals->{$row->nom_agence}->nb_ship_handled_by_agent += $row->nb_ship_handled_by_agent;
			$agencyGlobals->{$row->nom_agence}->da_rdv_agent += ($row->nb_rdv_handled_by_agent ? $row->da_rdv_agent : 0);
			$agencyGlobals->{$row->nom_agence}->da_srdv_agent += ($row->nb_srdv_handled_by_agent ? $row->da_srdv_agent : 0);
			$agencyGlobals->{$row->nom_agence}->da_ship_agent += ($row->nb_ship_handled_by_agent ? $row->da_ship_agent : 0);
			$agencyGlobals->{$row->nom_agence}->de_rdv_agent += $row->nb_rdv_handled_by_agent ? $row->de_rdv_agent : 0;
			$agencyGlobals->{$row->nom_agence}->de_srdv_agent += ($row->nb_srdv_handled_by_agent ? $row->de_srdv_agent : 0);
			$agencyGlobals->{$row->nom_agence}->de_ship_agent += ($row->nb_ship_handled_by_agent ? $row->de_ship_agent : 0);
			if($visio){
				$agencyGlobals->{$row->nom_agence}->nb_rdv_visio_with_agent += $row->nb_rdv_visio_with_agent;
				$agencyGlobals->{$row->nom_agence}->nb_rdv_visio_handled_by_agent += $row->nb_rdv_visio_handled_by_agent;
				$agencyGlobals->{$row->nom_agence}->da_rdv_visio_agent += ($row->nb_rdv_visio_handled_by_agent ? $row->da_rdv_visio_agent : 0);
			}
			$agencyGlobals->{$row->nom_agence}->global += $row->nb_rdv_handled_by_agent + $row->nb_srdv_handled_by_agent + $row->nb_ship_handled_by_agent + ($visio ? $row->nb_rdv_visio_handled_by_agent : 0);
			
			// echo "<li> " . $row->nom_agence . " : " . ($agencyGlobals->{$row->nom_agence}->nb_rdv_handled_by_agent);
			
			$id_agence = $row->id_agence;
			
		?>
		<?php endforeach; ?>	
		<?php
			$agentGlobals->{$matricule}->da_rdv_agent = ($comp - $zeroRDV) ? $agentGlobals->{$matricule}->da_rdv_agent/($comp - $zeroRDV) : 0;
			$agentGlobals->{$matricule}->da_srdv_agent = ($comp - $zeroSRDV) ? $agentGlobals->{$matricule}->da_srdv_agent/($comp - $zeroSRDV) : 0;
			$agentGlobals->{$matricule}->da_ship_agent = ($comp - $zeroSHIP) ? $agentGlobals->{$matricule}->da_ship_agent/($comp - $zeroSHIP) : 0;
			$agentGlobals->{$matricule}->de_rdv_agent = ($comp - $zeroRDV) ? $agentGlobals->{$matricule}->de_rdv_agent/($comp - $zeroRDV) : 0;
			$agentGlobals->{$matricule}->de_srdv_agent = ($comp - $zeroSRDV) ? $agentGlobals->{$matricule}->de_srdv_agent/($comp - $zeroSRDV) : 0;
			$agentGlobals->{$matricule}->de_ship_agent = ($comp - $zeroSHIP) ? $agentGlobals->{$matricule}->de_ship_agent/($comp - $zeroSHIP) : 0;
			$agentGlobals->{$matricule}->da_rdv_visio_agent = ($comp - $zeroVisio) ? $agentGlobals->{$matricule}->da_rdv_visio_agent/($comp - $zeroVisio) : 0;
			
			// var_dump($agencyGlobals);
		?>		
		<tr>
			<td colspan="4"></td>
			<td style="display:none"></td>	
			<td style="display:none"></td>	
			<td style="display:none"></td>	
			<td class="agent-row bold">GLOBAL</td>
			<td class="agent-row bold"><?=$agentGlobals->{$matricule}->nb_rdv_with_agent?></td>
			<td class="agent-row bold"><?=$agentGlobals->{$matricule}->nb_rdv_handled_by_agent?></td>
			<td class="agent-row bold"><?=$agentGlobals->{$matricule}->nb_srdv_handled_by_agent?></td>
			<td class="agent-row bold"><?=$agentGlobals->{$matricule}->nb_ship_handled_by_agent?></td>
			<td class="agent-row bold"><?=sec2hms($agentGlobals->{$matricule}->da_rdv_agent)?></td>
			<td class="agent-row bold"><?=sec2hms($agentGlobals->{$matricule}->da_srdv_agent)?></td>
			<td class="agent-row bold"><?=sec2hms($agentGlobals->{$matricule}->da_ship_agent)?></td>
			<td class="agent-row bold"><?=sec2hms($agentGlobals->{$matricule}->de_rdv_agent)?></td>
			<td class="agent-row bold"><?=sec2hms($agentGlobals->{$matricule}->de_srdv_agent)?></td>
			<td class="agent-row bold"><?=sec2hms($agentGlobals->{$matricule}->de_ship_agent)?></td>
			<?php if($visio){ ?>
				<td class="agent-row bold"><?=$agentGlobals->{$matricule}->nb_rdv_visio_with_agent?></td>
				<td class="agent-row bold"><?=$agentGlobals->{$matricule}->nb_rdv_visio_handled_by_agent?></td>
				<td class="agent-row bold"><?=sec2hms($agentGlobals->{$matricule}->da_rdv_visio_agent)?></td>
			<?php } ?>
			<td class="agent-row bold"><?=$agentGlobals->{$matricule}->global?></td>
		</tr>
	  <?php endforeach; ?>
		<tr>
			<td colspan="16">&nbsp;</td>
			<td style="display:none"></td>
			<td style="display:none"></td>
			<td style="display:none"></td>
			<td style="display:none"></td>
			<td style="display:none"></td>
			<td style="display:none"></td>
			<td style="display:none"></td>
			<td style="display:none"></td>
			<td style="display:none"></td>
			<td style="display:none"></td>
			<td style="display:none"></td>
			<td style="display:none"></td>
			<td style="display:none"></td>
			<td style="display:none"></td>
			<td style="display:none"></td>
		</tr>
		<?php
			$agencyGlobals_rdv_comp = array(' ' => 0);
			$agencyGlobals_srdv_comp = array(' ' => 0);
			$agencyGlobals_ship_comp = array(' ' => 0);
			$agencyGlobals_visio_comp = array(' ' => 0);
			foreach($DATA as $matricule => $item) :
				foreach($item as $id => $row) : 
				  $nom_agence = $row->nom_agence;
				  if(!empty($agentGlobals->{$matricule})) {
					if($agentGlobals->{$matricule}->nb_rdv_handled_by_agent > 0) {
						$value = isset($agencyGlobals_rdv_comp[$nom_agence]) ? $agencyGlobals_rdv_comp[$nom_agence]+1 : 1;
						$agencyGlobals_rdv_comp[$nom_agence] = $value;
					}
					if($agentGlobals->{$matricule}->nb_srdv_handled_by_agent > 0) {
						$value = isset($agencyGlobals_srdv_comp[$nom_agence]) ? $agencyGlobals_srdv_comp[$nom_agence]+1 : 1;
						$agencyGlobals_srdv_comp[$nom_agence] = $value;
					}
					if($agentGlobals->{$matricule}->nb_ship_handled_by_agent > 0) {
						$value = isset($agencyGlobals_ship_comp[$nom_agence]) ? $agencyGlobals_ship_comp[$nom_agence]+1 : 1;
						$agencyGlobals_ship_comp[$nom_agence] = $value;
					}
					if($visio && $agentGlobals->{$matricule}->nb_rdv_visio_handled_by_agent > 0) {
						$value = isset($agencyGlobals_visio_comp[$nom_agence]) ? $agencyGlobals_visio_comp[$nom_agence]+1 : 1;
						$agencyGlobals_visio_comp[$nom_agence] = $value;
					}
				  }
				endforeach; 
			endforeach;
		?>
		<?php foreach($agencyGlobals as $nom_agence => $row) : ?>
		<?php
			$rdv_comp = isset($agencyGlobals_rdv_comp[$nom_agence])&& $agencyGlobals_rdv_comp[$nom_agence] ? $agencyGlobals_rdv_comp[$nom_agence] : 0;
			$srdv_comp = isset($agencyGlobals_srdv_comp[$nom_agence]) && $agencyGlobals_srdv_comp[$nom_agence] ? $agencyGlobals_srdv_comp[$nom_agence] : 0;
			$ship_comp = isset($agencyGlobals_ship_comp[$nom_agence]) && $agencyGlobals_ship_comp[$nom_agence] ? $agencyGlobals_ship_comp[$nom_agence] : 0;
			$visio_comp = isset($agencyGlobals_visio_comp[$nom_agence]) && $agencyGlobals_visio_comp[$nom_agence] ? $agencyGlobals_visio_comp[$nom_agence] : 0;
		?>
		<tr>
			<td colspan="3"></td>
			<td style="display:none"></td>
			<td style="display:none"></td>
			<td class="global-row bold">GLOBAL AGENCE</td>
			<td class="global-row bold"><?=$nom_agence?></td>
			<td class="global-row bold"><?=$row->nb_rdv_with_agent?></td>
			<td class="global-row bold"><?=$row->nb_rdv_handled_by_agent?></td>
			<td class="global-row bold"><?=$row->nb_srdv_handled_by_agent?></td>
			<td class="global-row bold"><?=$row->nb_ship_handled_by_agent?></td>
			
			<td class="global-row bold"><?=($rdv_comp) ? sec2hms($row->da_rdv_agent/($rdv_comp)) : '00:00'?></td>
			<td class="global-row bold"><?=($srdv_comp) ? sec2hms($row->da_srdv_agent/($srdv_comp)) : '00:00'?></td>
			<td class="global-row bold"><?=($ship_comp) ? sec2hms($row->da_ship_agent/($ship_comp)) : '00:00'?></td>
			<td class="global-row bold"><?=($rdv_comp) ? sec2hms($row->de_rdv_agent/($rdv_comp)) : '00:00'?></td>
			<td class="global-row bold"><?=($srdv_comp) ? sec2hms($row->de_srdv_agent/($srdv_comp)) : '00:00'?></td>
			<td class="global-row bold"><?=($ship_comp) ? sec2hms($row->de_ship_agent/($ship_comp)) : '00:00'?></td>
			
			<?php if($visio){ ?>
				<td class="global-row bold"><?=$row->nb_rdv_visio_with_agent?></td>
				<td class="global-row bold"><?=$row->nb_rdv_visio_handled_by_agent?></td>
				<td class="global-row bold"><?=($visio_comp) ? sec2hms($row->da_rdv_visio_agent/($visio_comp)) : '00:00'?></td>
			<?php } ?>
			<td class="global-row bold"><?=$row->global?></td>
		</tr>
		<?php endforeach; ?>
	</tbody>
</table>
<?php

if(!$result["content"]) {
	$html = "Pas de résultat";
	$result = array("status" => true, "content" => $html);
}



function sec2hms($sec) {
    if($sec){
		$totalSeconds = (int) $sec;
        $hours = (int) floor($totalSeconds / 3600);
        $minutes = (int) floor(($totalSeconds - ($hours*3600))/60);
        $seconds = (int) $totalSeconds - ($hours*3600 + $minutes*60);
        $hours = $hours ? (str_pad($hours,2,"0",STR_PAD_LEFT).":") : "";
        $minutes = str_pad($minutes,2,"0",STR_PAD_LEFT);
        $seconds = str_pad($seconds,2,"0",STR_PAD_LEFT);
        return $hours.$minutes.":".$seconds;
    }
    return "00:00";
}

?>