<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use App\Repository\TacheRepository;
use App\Repository\UserRepository;
use App\Repository\ClientRepository;
use App\Repository\AdresseRepository;
use App\Repository\ActiviteRepository;
use App\Repository\UniteRepository;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use App\Entity\Unite;
use App\Entity\Client;
use App\Entity\Tache;
use App\Entity\Adresse;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Form\FormEvents;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Security;
class HomeController extends AbstractController
{
public function __construct(Security $security) {
$this->security = $security;
}
#[Route('/', name: 'app_default')]
public function home(EntityManagerInterface $em,
TacheRepository $tacheRepository,
UserRepository $userRepository): Response
{
$user = $this->security->getUser();
if($user == null){
return $this->redirectToRoute('app_login');
}
return $this->render('home/global_default.html.twig');
}
#[Route('/time', name: 'app_home_time')]
public function homeTime(EntityManagerInterface $em,
TacheRepository $tacheRepository,
UserRepository $userRepository): Response
{
$user = $this->security->getUser();
if($user == null){
return $this->redirectToRoute('app_login');
}
$today = date("Y-m-d");
$oneMonthAgo = date('Y-m-d 00:00:00', strtotime('-1 months', strtotime($today))); // Calcul de la date il y a 1 mois
$formated_from = date("Y-m-01 00:00:00", strtotime($oneMonthAgo)); // Début du mois
$currentYear = date("Y");
$input_year = $currentYear;
$valueOfMontAgo = date('m', strtotime('-1 months', strtotime($today)));
$in_date = date($currentYear.'-'.$valueOfMontAgo);
$last_day = date('t', strtotime($in_date));
if (date('m', strtotime($in_date)) == 02) {
// Vérifier si l'année est bissextile
if (date('L', strtotime($in_date))) {
$last_day = 29;
} else {
$last_day = 28;
}
}
$formated_to = date($currentYear."-".$valueOfMontAgo."-".$last_day." 23:59:59", strtotime($in_date)); // fin du mois
$from = new \DateTime(".$formated_from.", new \DateTimeZone('Europe/Dublin'));
$to = new \DateTime(".$formated_to.", new \DateTimeZone('Europe/Dublin'));
$input_month = $valueOfMontAgo;
$user_id = $user->getId();
$arr_roles = $user->getRoles();
//$taches = $tacheRepository->findByAll($from, $to);
//$taches = $tacheRepository->findByUser($from, $to, $user_id);
foreach($arr_roles as $role) {
if($role == 'ROLE_ADMIN' || $role == 'ROLE_SUPER_ADMIN') {
// renvoyer toutes les dernières taches
} else {
$taches = $tacheRepository->findByUser($from, $to, $user_id);
}
}
$allTaches = [];
for($i = 0; $i < count($taches); $i++) {
$auteurId = $taches[$i]->getAuteurId();
$auteur = $userRepository->findOneBy(['id' => $auteurId ]);
$allTaches[$i][] = [
'taches' => $taches[$i],
'auteur' => $auteur
];
}
return $this->redirectToRoute('app_home_time_index_chart');
}
#[Route('/home/time', name: 'app_home_time_index_chart', methods: ['POST', 'GET'])]
public function getAllTaches(UserRepository $userRepository, UniteRepository $uniteRepository,
TacheRepository $tacheRepository,
ActiviteRepository $activiteRepository,
ClientRepository $clientRepository,
Request $request,
EntityManagerInterface $em,
): Response
{
$user = $this->security->getUser();
if($user == null){
return $this->redirectToRoute('app_login');
}
$input_month = $request->query->get('month') ?: date('m');
$input_year = $request->query->get('year') ?: date('Y');
$start_month = date($input_year.'-'.$input_month.'-01 00:00:00');
$start_month_object = new \DateTime("$input_year-$input_month-01 00:00:00", new \DateTimeZone('Europe/Dublin'));
$selected_month = date('n', strtotime($start_month));
$next_month = ($selected_month % 12) + 1;
//$end_month = date($input_year.'-'.$next_month.'-01 00:00:00');
$end_month_object = clone $start_month_object;
$end_month_object->modify('first day of next month');
$from = $start_month_object;
$to = $end_month_object;
//$allTaches = $tacheRepository->findBy(array(), array('startedAt' => 'DESC'));
$allUsers = $userRepository->findBy(array(), array('nom' => 'ASC'));
$allActivites = $activiteRepository->findAll();
$allClients = $clientRepository->findAll();
$allUnites = $uniteRepository->findAll();
$user_id = $request->query->get('user_id');
$activite_id = $request->query->get('activite_id');
$client_id = $request->query->get('client_id');
$periode = $request->query->get('periode');
$unite_id = $request->query->get('unite_id');
$sort = 'd';
$input_from = null;
$input_to = null;
$user = $userRepository->findOneBy(['id' => $user_id]);
$activite = $activiteRepository->findOneBy(['id' => $activite_id]);
$client = $clientRepository->findOneBy(['id' => $client_id]);
$taches = null;
$taches_unite = null;
$selected_user = null;
$selected_client = null;
$selected_activite = null;
$tableau_client = [];
$arr_tot_chaque_unite_key_val = [];
$arr_tot_chaque_unite = [];
foreach($allUnites as $unite){
$arr_totaux[] = null;
$taches_unite = $unite->getTache();
$tot_unite_tache = 0;
$unite_code = $unite->getCode();
foreach($taches_unite as $tache){
$arr_totaux[]= $tache->getTotal();
// on groupe les tâches par client code
$code = $tache->getClient()->getCode();
$tableau_client[$unite_code][$code][] = $tache;
}
$arr_tot_chaque_unite_key_val[$unite->getCode()] = array_sum($arr_totaux);
$arr_tot_chaque_unite[]= array_sum($arr_totaux);
$arr_totaux = null;
// on groupe le total des heures de chaque unite par client dans $unite_clientCode_total
$unite_clientCode_total = null;
foreach($tableau_client as $unite_code => $arr_taches){
$tmp = null;
$tot = 0;
foreach($arr_taches as $code_client => $arr) {
foreach($arr as $tache) {
$arr_totaux[]= $tache->getTotal();
}
$tmp[$code_client] = array_sum($arr_totaux);
$tot += array_sum($arr_totaux);
$arr_totaux = null;
}
$tmp['tot'] = $tot;
$tot = 0;
$unite_clientCode_total[$unite_code] = $tmp;
$tmp = null;
}
}
//dd($arr_tot_chaque_unite_key_val);
// on prend le nombre d'heures le plus grand par unite
$max_value = max($arr_tot_chaque_unite_key_val);
// Code unite correspondant
$code_unite = array_search($max_value, $arr_tot_chaque_unite_key_val);
$max_unite = [$code_unite, $max_value];
$unite_id = 1;
$unite = $uniteRepository->findOneBy(['id' => $unite_id]);
$taches_unite_1 = $unite->getTache();
$taches_unite_same_client = [];
$tableau_client_ux = [];
$tableau_activite = [];
$code = null;
foreach($taches_unite_1 as $tache) {
$code = $tache->getClient()->getCode();
$tableau_client_ux[$code][] = $tache;
}
$arr_totaux = null;
$groups = null;
$group = null;
$tableau_client_code_total_uniteDonnee = null;
foreach($tableau_client_ux as $code => $arr_taches){
foreach($arr_taches as $tache) {
$arr_totaux[]= $tache->getTotal();
}
$tableau_client_code_total_uniteDonnee[$code] = array_sum($arr_totaux);
$arr_totaux = null;
}
foreach($taches_unite_1 as $tache) {
$code = $tache->getActivite()->getCode();
$tableau_activite[$code][]=$tache;
}
// on recupère les heures de chaque client
$arr_tot_chaque_client=[];
$arr_tot_chaque_client_key_val=[];
$color_client_code = null;
foreach($allClients as $client){
$arr_totaux[] = null;
$client_id = $client->getId();
//$client_id = 1;
$qb = $em->createQueryBuilder();
$qb ->select('t')
->from(Tache::class, 't')
->where('t.startedAt >= :from')
->andWhere('t.endedAt <= :to')
->andWhere('t.client = :cid')
->setParameter('from', $from)
->setParameter('to', $to)
->setParameter('cid', $client_id)
->orderBy('t.startedAt', 'DESC');
$taches = $qb->getQuery()->getResult();
//dd($taches);
foreach($taches as $tache) {
$arr_totaux[]= $tache->getTotal();
}
$arr_tot_chaque_client[]= array_sum($arr_totaux);
$arr_tot_chaque_client_key_val[$client->getCode()] = array_sum($arr_totaux);
//$color_client_code = $client->getCodeCouleur();
$arr_totaux = null;
}
// on prend le nombre d'heures le plus grand par commune
$max_value = max($arr_tot_chaque_client_key_val);
// Code client correspondant
$code_client = array_search($max_value, $arr_tot_chaque_client_key_val);
$max_client = [$code_client, $max_value];
// on recupère les heures de chaque activité
$arr_tot_chaque_activite=[];
$arr_tot_chaque_activite_key_val=[];
$color_activite_code = null;
foreach($allActivites as $activite){
$arr_totaux[] = null;
$activite_id = $activite->getId();
$qb = $em->createQueryBuilder();
$qb ->select('t')
->from(Tache::class, 't')
->where('t.startedAt >= :from')
->andWhere('t.endedAt <= :to')
->andWhere('t.activite = :aid')
->setParameter('from', $from)
->setParameter('to', $to)
->setParameter('aid', $activite_id)
->orderBy('t.startedAt', 'DESC');
$taches = $qb->getQuery()->getResult();
foreach($taches as $tache) {
$total = $tache->getTotal();
$users = $tache->getUser();
$total = $total * count($users);
$arr_totaux[]= $total;
}
$arr_tot_chaque_activite[]= array_sum($arr_totaux);
$arr_tot_chaque_activite_key_val[$activite->getCode()] = array_sum($arr_totaux);
$color_activite_code = $activite->getCodeCouleur();
$arr_totaux = null;
}
$total_des_heures = array_sum($arr_tot_chaque_activite);
// on prend le nombre d'heures le plus grand par activite
$max_value = max($arr_tot_chaque_activite_key_val);
// Code activite correspondant
$code_activite = array_search($max_value, $arr_tot_chaque_activite_key_val);
$max_activite = [$code_activite,$color_activite_code , $max_value];
$arr_tache_same_date = array();
$dateRef = null;
$totHour = 0;
for($i = 0; $i < count($taches); $i++) {
$started = $taches[$i]->getStartedAt();
$startAt = date_format($started,"Y-m-d");
if($dateRef != $startAt) {
$arr_tache_same_date[$startAt][] = $taches[$i];
$dateRef = $startAt;
} else {
$totHour = $taches[$i]->getTotal();
$arr_tache_same_date[$startAt][$i] = $taches[$i];
}
}
if($sort == 'd') {
krsort($arr_tache_same_date);
} else if($sort == 't') {
ksort($arr_tache_same_date);
}
// on trie le total des heures par date et par activité
// $key c'est la date !
$arr_Data = array();
foreach($arr_tache_same_date as $key => $arr_tache){
$total = 0;
foreach($arr_tache as $tache) {
$activite_code = $tache->getActivite()->getCode();
$total = $tache->getTotal();
$users = $tache->getUser();
$total = $total * count($users);
$arr_Data[$key][$activite_code][]= $total;
}
}
foreach($arr_Data as $key => $arr_value) {
foreach($arr_value as $code => $value) {
for($i = 0; $i <= count($value); $i++) {
if ($i > 0) {
$somme = array_sum($value);
}else{
$somme = $value[$i];
}
$arr_Data[$key][$code]['total'] = $somme;
}
}
}
// $arr_data contient le total des heures par date et par activité
$arr_total_vertical = array();
$keyRef = null;
$number_of_rows = 0;
foreach($arr_tache_same_date as $key => $arr_tache){
$total_jour = 0;
$number_of_rows += 1;
foreach($arr_tache as $tache) {
$total_jour += $tache->getTotal();
$arr_total_vertical[$key] = $total_jour;
}
}
$arr_activitesTotal = array();
foreach($allActivites as $activite){
$arr_activitesTotal[$activite->getCode()] = 0;
}
$ref_code = 0;
$tot = 0;
$arr_tot = array();
foreach($arr_tache_same_date as $arr_tache){
foreach($arr_tache as $tache) {
$activiteCode = $tache->getActivite()->getCode();
//$arr_user = $tache->getUser();
//$number_of_user = count($arr_user);
foreach($arr_activitesTotal as $key => $value){
if($key == $activiteCode) {
$total = $tache->getTotal();
$users = $tache->getUser();
$total = $total * count($users);
$value += $total;
$arr_activitesTotal[$key] = $value;
}
}
}
}
$somme = array_sum($arr_activitesTotal);
array_push($arr_activitesTotal, $somme);
$code_couleurs = [];
$code_clients = [];
$code_activites = [];
foreach($allClients as $item){
$code_clients[] = $item->getCode();
}
foreach($allActivites as $item){
$code_activites[] = $item->getCode();
$code_couleurs[] = $item->getCodeCouleur();
}
$arr_duree = [];
for($i = 1; $i < 31; $i++) {
$arr_duree[] = $i;
}
$heures_par_client = $arr_activitesTotal;
$last = array_pop($heures_par_client);
$arr_totaux = [];
for($i = 1; $i <= $last; $i++) {
$arr_totaux[]= $i;
}
return $this->render('home/home_time.html.twig', [
'arr_tache_same_date' => $arr_tache_same_date,
'client' => $client,
'user'=> null,
'allUsers' => $allUsers,
'allClients' => $allClients,
'allActivites' => $allActivites,
//'allTaches' => $allTaches,
'arr_activitesTotal' => $arr_activitesTotal,
'arr_Data' => $arr_Data,
'selected_user' => $selected_user,
'selected_client' => $selected_client,
'selected_activite' => $selected_activite,
'input_from' => $input_from,
'input_to'=> $input_to,
'periode' => $periode,
'arr_duree' => $arr_duree,
'heures_par_client' => $heures_par_client,
'total_heures' => $last,
'arr_totaux' => $arr_totaux,
'code_couleurs' => $code_couleurs,
'code_clients' => $code_clients,
'code_activites' => $code_activites,
'total_des_heures' => $total_des_heures,
'arr_tot_chaque_client' => $arr_tot_chaque_client,
'arr_tot_chaque_client_key_val' => $arr_tot_chaque_client_key_val,
'arr_tot_chaque_activite' => $arr_tot_chaque_activite,
'max_client' => $max_client,
'max_activite' => $max_activite,
'taches_unite' => $taches_unite_1,
'arr_tot_chaque_unite' => $arr_tot_chaque_unite,
'max_unite' => $max_unite,
'unite_clientCode_total' => $unite_clientCode_total,
'input_month' => $input_month,
'input_year' => $input_year
]);
}
#[Route('/home', name: 'app_home_test')]
public function homeTest(EntityManagerInterface $em,
TacheRepository $tacheRepository,
UserRepository $userRepository): Response
{
return $this->render('home/default.html.twig', [
'taches' => null,
'allTaches' => null,
'selected_user' => null
]);
}
}