src/Controller/HomeController.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use App\Repository\TacheRepository;
  8. use App\Repository\UserRepository;
  9. use App\Repository\ClientRepository;
  10. use App\Repository\AdresseRepository;
  11. use App\Repository\ActiviteRepository;
  12. use App\Repository\UniteRepository;
  13. use Symfony\Component\Form\Extension\Core\Type\TextType;
  14. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  15. use App\Entity\Unite;
  16. use App\Entity\Client;
  17. use App\Entity\Tache;
  18. use App\Entity\Adresse;
  19. use Symfony\Component\Validator\Constraints\NotBlank;
  20. use Symfony\Component\Form\FormEvents;
  21. use Doctrine\ORM\EntityManagerInterface;
  22. use Symfony\Component\Security\Core\Security
  23. class HomeController extends AbstractController
  24. {
  25.     
  26.     public function __construct(Security $security) {
  27.         $this->security $security;
  28.     }
  29.    
  30.     #[Route('/'name'app_default')]
  31.     public function home(EntityManagerInterface $em,
  32.                         TacheRepository $tacheRepository,
  33.                         UserRepository $userRepository): Response         
  34.     {
  35.         $user $this->security->getUser();
  36.         if($user == null){ 
  37.             return $this->redirectToRoute('app_login');
  38.         }
  39.         
  40.         return $this->render('home/global_default.html.twig');
  41.     }
  42.     #[Route('/time'name'app_home_time')]
  43.     public function homeTime(EntityManagerInterface $em,
  44.                         TacheRepository $tacheRepository,
  45.                         UserRepository $userRepository): Response         
  46.     {
  47.         $user $this->security->getUser();
  48.         if($user == null){
  49.             return $this->redirectToRoute('app_login');
  50.         }
  51.        
  52.         $today date("Y-m-d");   
  53.         $oneMonthAgo date('Y-m-d 00:00:00'strtotime('-1 months'strtotime($today))); // Calcul de la date il y a 1 mois        
  54.         $formated_from date("Y-m-01 00:00:00"strtotime($oneMonthAgo)); // Début du mois
  55.         $currentYear date("Y");   
  56.         $input_year $currentYear;
  57.         $valueOfMontAgo date('m'strtotime('-1 months'strtotime($today)));
  58.         $in_date date($currentYear.'-'.$valueOfMontAgo);
  59.         
  60.         $last_day date('t'strtotime($in_date));
  61.         if (date('m'strtotime($in_date)) == 02) {
  62.             // Vérifier si l'année est bissextile
  63.             if (date('L'strtotime($in_date))) {
  64.                 $last_day 29;
  65.             } else {
  66.                 $last_day 28;
  67.             }
  68.         }
  69.         $formated_to date($currentYear."-".$valueOfMontAgo."-".$last_day." 23:59:59"strtotime($in_date)); // fin du mois  
  70.         $from = new \DateTime(".$formated_from.", new \DateTimeZone('Europe/Dublin'));
  71.         $to = new \DateTime(".$formated_to.", new \DateTimeZone('Europe/Dublin'));   
  72.         $input_month $valueOfMontAgo;
  73.             
  74.         $user_id $user->getId(); 
  75.         $arr_roles $user->getRoles();
  76.         
  77.         //$taches = $tacheRepository->findByAll($from, $to);
  78.         //$taches = $tacheRepository->findByUser($from, $to, $user_id);
  79.         
  80.         foreach($arr_roles as $role) {
  81.             if($role == 'ROLE_ADMIN' || $role == 'ROLE_SUPER_ADMIN') {
  82.                 // renvoyer toutes les dernières taches 
  83.                
  84.                     
  85.             } else {
  86.                 $taches $tacheRepository->findByUser($from$to$user_id);
  87.             }
  88.         } 
  89.         
  90.         $allTaches = [];
  91.         for($i 0$i count($taches); $i++) {
  92.             $auteurId $taches[$i]->getAuteurId();
  93.             $auteur $userRepository->findOneBy(['id' => $auteurId ]);
  94.             $allTaches[$i][] = [
  95.                 'taches' => $taches[$i],
  96.                 'auteur' => $auteur
  97.             ];
  98.         }
  99.         
  100.         return $this->redirectToRoute('app_home_time_index_chart');
  101.       
  102.     }   
  103.     #[Route('/home/time'name'app_home_time_index_chart'methods: ['POST''GET'])]
  104.     public function getAllTaches(UserRepository $userRepositoryUniteRepository $uniteRepository,
  105.                                    TacheRepository $tacheRepository
  106.                                    ActiviteRepository $activiteRepository,
  107.                                    ClientRepository $clientRepository,
  108.                                    Request $request,
  109.                                    EntityManagerInterface $em,
  110.                                    ): Response
  111.     {  
  112.     
  113.         $user $this->security->getUser();
  114.         if($user == null){
  115.             return $this->redirectToRoute('app_login');
  116.         }
  117.          
  118.         $input_month $request->query->get('month') ?: date('m');
  119.         $input_year $request->query->get('year') ?: date('Y');
  120.         
  121.         $start_month date($input_year.'-'.$input_month.'-01 00:00:00');
  122.         $start_month_object = new \DateTime("$input_year-$input_month-01 00:00:00", new \DateTimeZone('Europe/Dublin'));
  123.         $selected_month date('n'strtotime($start_month));
  124.         
  125.         $next_month = ($selected_month 12) + 1;
  126.         //$end_month = date($input_year.'-'.$next_month.'-01 00:00:00');     
  127.         $end_month_object = clone $start_month_object;
  128.         $end_month_object->modify('first day of next month');
  129.         $from $start_month_object;
  130.         $to $end_month_object;        
  131.         //$allTaches = $tacheRepository->findBy(array(), array('startedAt' => 'DESC'));
  132.         $allUsers $userRepository->findBy(array(), array('nom' => 'ASC'));
  133.         $allActivites $activiteRepository->findAll();
  134.         $allClients $clientRepository->findAll();
  135.         $allUnites $uniteRepository->findAll();
  136.         $user_id $request->query->get('user_id'); 
  137.         $activite_id $request->query->get('activite_id');
  138.         $client_id $request->query->get('client_id');
  139.         $periode $request->query->get('periode');
  140.         $unite_id $request->query->get('unite_id');
  141.         $sort 'd';
  142.         $input_from null;
  143.         $input_to null;
  144.          
  145.         $user $userRepository->findOneBy(['id' => $user_id]);
  146.         $activite $activiteRepository->findOneBy(['id' => $activite_id]);
  147.         $client $clientRepository->findOneBy(['id' => $client_id]);
  148.         $taches null;
  149.         $taches_unite null;
  150.         $selected_user null
  151.         $selected_client null;
  152.         $selected_activite null;
  153.         $tableau_client = [];
  154.         $arr_tot_chaque_unite_key_val = [];
  155.         $arr_tot_chaque_unite = [];
  156.         foreach($allUnites as $unite){
  157.             $arr_totaux[] = null;
  158.             $taches_unite $unite->getTache();
  159.             $tot_unite_tache 0;
  160.             $unite_code $unite->getCode();
  161.             foreach($taches_unite as $tache){
  162.                 $arr_totaux[]= $tache->getTotal();
  163.                 // on groupe les tâches par client code
  164.                 $code $tache->getClient()->getCode(); 
  165.                 $tableau_client[$unite_code][$code][] = $tache;
  166.             }
  167.             $arr_tot_chaque_unite_key_val[$unite->getCode()] = array_sum($arr_totaux);
  168.             $arr_tot_chaque_unite[]= array_sum($arr_totaux);
  169.             $arr_totaux null;
  170.             // on groupe le total des heures de chaque unite par client dans $unite_clientCode_total
  171.             $unite_clientCode_total null;
  172.             foreach($tableau_client as $unite_code => $arr_taches){
  173.                 $tmp null;
  174.                 $tot 0;
  175.                 foreach($arr_taches as $code_client => $arr) {
  176.                     foreach($arr as $tache) {
  177.                         $arr_totaux[]= $tache->getTotal(); 
  178.                     } 
  179.                     $tmp[$code_client] = array_sum($arr_totaux); 
  180.                     $tot += array_sum($arr_totaux);
  181.                     $arr_totaux null;  
  182.                 } 
  183.                 $tmp['tot'] = $tot
  184.                 $tot 0;   
  185.                 $unite_clientCode_total[$unite_code] = $tmp;
  186.                 $tmp null;
  187.                 
  188.             }
  189.         }
  190.     //dd($arr_tot_chaque_unite_key_val);        
  191.         // on prend le nombre d'heures le plus grand par unite
  192.         $max_value max($arr_tot_chaque_unite_key_val);
  193.         // Code unite correspondant
  194.         $code_unite array_search($max_value$arr_tot_chaque_unite_key_val);
  195.         $max_unite = [$code_unite$max_value]; 
  196.         
  197.         $unite_id 1;
  198.         $unite $uniteRepository->findOneBy(['id' => $unite_id]);
  199.         $taches_unite_1 $unite->getTache();
  200.         $taches_unite_same_client = [];
  201.          
  202.         $tableau_client_ux = [];
  203.         $tableau_activite = [];
  204.          
  205.         $code null;
  206.         foreach($taches_unite_1 as $tache) {
  207.             $code $tache->getClient()->getCode(); 
  208.             $tableau_client_ux[$code][] = $tache;
  209.         }
  210.         $arr_totaux null;
  211.         $groups null;
  212.         $group null;
  213.         $tableau_client_code_total_uniteDonnee null;
  214.         
  215.         foreach($tableau_client_ux as $code => $arr_taches){
  216.             foreach($arr_taches as $tache) {
  217.                 $arr_totaux[]= $tache->getTotal(); 
  218.             }     
  219.             $tableau_client_code_total_uniteDonnee[$code] = array_sum($arr_totaux);
  220.             $arr_totaux null;
  221.         }
  222.         foreach($taches_unite_1 as $tache) {
  223.             $code $tache->getActivite()->getCode();
  224.             $tableau_activite[$code][]=$tache;
  225.         }
  226.         // on recupère les heures de chaque client 
  227.         $arr_tot_chaque_client=[];
  228.         $arr_tot_chaque_client_key_val=[];
  229.         $color_client_code null;
  230.         foreach($allClients as $client){ 
  231.             $arr_totaux[] = null;
  232.             $client_id $client->getId(); 
  233.             //$client_id = 1;
  234.             $qb $em->createQueryBuilder();
  235.             $qb ->select('t')
  236.                 ->from(Tache::class, 't')
  237.                 ->where('t.startedAt >= :from')
  238.                 ->andWhere('t.endedAt <= :to')                
  239.                 ->andWhere('t.client = :cid')
  240.                 ->setParameter('from'$from)
  241.                 ->setParameter('to'$to)                
  242.                 ->setParameter('cid'$client_id)
  243.                 ->orderBy('t.startedAt''DESC');
  244.                 $taches $qb->getQuery()->getResult(); 
  245.                 //dd($taches);
  246.                 foreach($taches as $tache) {
  247.                     $arr_totaux[]= $tache->getTotal(); 
  248.                 }
  249.                 $arr_tot_chaque_client[]= array_sum($arr_totaux);
  250.                 $arr_tot_chaque_client_key_val[$client->getCode()] = array_sum($arr_totaux);
  251.                 //$color_client_code = $client->getCodeCouleur();
  252.                 $arr_totaux null;  
  253.         }    
  254.         // on prend le nombre d'heures le plus grand par commune
  255.         $max_value max($arr_tot_chaque_client_key_val);
  256.         // Code client correspondant
  257.         $code_client array_search($max_value$arr_tot_chaque_client_key_val);
  258.         $max_client = [$code_client$max_value];
  259.         // on recupère les heures de chaque activité 
  260.         $arr_tot_chaque_activite=[];
  261.         $arr_tot_chaque_activite_key_val=[];
  262.         $color_activite_code null;
  263.         foreach($allActivites as $activite){ 
  264.             $arr_totaux[] = null;
  265.             $activite_id $activite->getId(); 
  266.             $qb $em->createQueryBuilder();
  267.             $qb ->select('t')
  268.                 ->from(Tache::class, 't')
  269.                 ->where('t.startedAt >= :from')
  270.                 ->andWhere('t.endedAt <= :to')                
  271.                 ->andWhere('t.activite = :aid')
  272.                 ->setParameter('from'$from)
  273.                 ->setParameter('to'$to)                
  274.                 ->setParameter('aid'$activite_id)
  275.                 ->orderBy('t.startedAt''DESC');
  276.                 $taches $qb->getQuery()->getResult(); 
  277.             foreach($taches as $tache) {
  278.                 $total $tache->getTotal();
  279.                 $users $tache->getUser();    
  280.                 $total $total count($users);
  281.                 $arr_totaux[]= $total
  282.             }
  283.             
  284.             $arr_tot_chaque_activite[]= array_sum($arr_totaux);
  285.             $arr_tot_chaque_activite_key_val[$activite->getCode()] = array_sum($arr_totaux);
  286.             $color_activite_code $activite->getCodeCouleur();
  287.             $arr_totaux null;
  288.         }
  289.         $total_des_heures array_sum($arr_tot_chaque_activite);
  290.          
  291.         // on prend le nombre d'heures le plus grand par activite
  292.         $max_value max($arr_tot_chaque_activite_key_val);
  293.         // Code activite correspondant
  294.         $code_activite array_search($max_value$arr_tot_chaque_activite_key_val);
  295.         $max_activite = [$code_activite,$color_activite_code $max_value];    
  296.   
  297.         $arr_tache_same_date = array();
  298.         $dateRef null;
  299.         $totHour 0;
  300.         for($i 0$i count($taches); $i++) {
  301.             
  302.             $started $taches[$i]->getStartedAt();
  303.             $startAt date_format($started,"Y-m-d");
  304.             
  305.             if($dateRef != $startAt) {
  306.                 
  307.                 $arr_tache_same_date[$startAt][] = $taches[$i];
  308.                 $dateRef $startAt;
  309.             } else {
  310.                 $totHour $taches[$i]->getTotal();
  311.                 $arr_tache_same_date[$startAt][$i] = $taches[$i]; 
  312.             }
  313.         }
  314.         if($sort == 'd') {
  315.             krsort($arr_tache_same_date);
  316.         } else if($sort == 't') {
  317.             ksort($arr_tache_same_date);
  318.         }
  319.         
  320.         // on trie le total des heures par date et par activité
  321.         // $key c'est la date !
  322.         $arr_Data = array();
  323.         foreach($arr_tache_same_date as $key => $arr_tache){   
  324.             $total 0;
  325.             foreach($arr_tache as $tache) {
  326.                 $activite_code $tache->getActivite()->getCode();
  327.                 $total $tache->getTotal();
  328.                 $users $tache->getUser();    
  329.                 $total $total count($users);
  330.                 $arr_Data[$key][$activite_code][]= $total;
  331.             }
  332.         }    
  333.         foreach($arr_Data as $key => $arr_value) {
  334.             foreach($arr_value as $code => $value) { 
  335.                 for($i 0$i <= count($value); $i++) {
  336.                     if ($i 0) {
  337.                         $somme array_sum($value);
  338.                     }else{
  339.                         $somme $value[$i];
  340.                     }
  341.                     $arr_Data[$key][$code]['total'] = $somme;
  342.                 }
  343.             }
  344.         }
  345.         // $arr_data contient le total des heures par date et par activité
  346.         $arr_total_vertical = array();
  347.         $keyRef null;
  348.         $number_of_rows 0;
  349.         foreach($arr_tache_same_date as $key => $arr_tache){
  350.                 $total_jour 0;
  351.                 $number_of_rows += 1;
  352.                 foreach($arr_tache as $tache) {
  353.                     $total_jour += $tache->getTotal();
  354.                     $arr_total_vertical[$key] = $total_jour;
  355.                 }
  356.         }        
  357.       
  358.       
  359.         $arr_activitesTotal = array();
  360.         foreach($allActivites as $activite){
  361.             $arr_activitesTotal[$activite->getCode()] = 0;
  362.         }
  363.         $ref_code 0;
  364.         $tot 0;
  365.         $arr_tot = array();
  366.         
  367.         foreach($arr_tache_same_date as $arr_tache){
  368.             foreach($arr_tache as $tache) {
  369.                 $activiteCode $tache->getActivite()->getCode();
  370.                 
  371.                 //$arr_user = $tache->getUser();
  372.                 //$number_of_user = count($arr_user);
  373.                 
  374.                 foreach($arr_activitesTotal as $key => $value){
  375.                     if($key == $activiteCode) {         
  376.                         
  377.                         $total $tache->getTotal();
  378.                         $users $tache->getUser();    
  379.                         $total $total count($users);
  380.                         $value += $total;
  381.                         $arr_activitesTotal[$key] = $value;
  382.                     }
  383.                 }
  384.             }            
  385.         }
  386.         $somme array_sum($arr_activitesTotal);
  387.         array_push($arr_activitesTotal$somme);
  388.      
  389.        
  390.         $code_couleurs = [];
  391.         $code_clients = [];
  392.         $code_activites = [];
  393.         foreach($allClients as $item){
  394.             $code_clients[] = $item->getCode();
  395.         }
  396.         foreach($allActivites as $item){
  397.             $code_activites[] = $item->getCode();
  398.             $code_couleurs[] = $item->getCodeCouleur();
  399.         }
  400.         $arr_duree = [];
  401.         for($i 1$i 31$i++) {
  402.             $arr_duree[] = $i;
  403.         }
  404.         $heures_par_client $arr_activitesTotal;
  405.         $last array_pop($heures_par_client);
  406.         $arr_totaux = [];
  407.         for($i 1$i <= $last$i++) {
  408.             $arr_totaux[]= $i;
  409.         }
  410.         return $this->render('home/home_time.html.twig', [
  411.             'arr_tache_same_date' => $arr_tache_same_date,
  412.             'client' => $client,
  413.             'user'=> null,
  414.             'allUsers' => $allUsers,
  415.             'allClients' => $allClients,
  416.             'allActivites' => $allActivites,
  417.             //'allTaches' => $allTaches, 
  418.             'arr_activitesTotal' => $arr_activitesTotal,
  419.             'arr_Data' => $arr_Data,
  420.             'selected_user' => $selected_user,
  421.             'selected_client' => $selected_client,
  422.             'selected_activite' => $selected_activite,
  423.             'input_from' => $input_from,
  424.             'input_to'=> $input_to,
  425.             'periode' => $periode,
  426.             'arr_duree' => $arr_duree,
  427.             'heures_par_client' => $heures_par_client,
  428.             'total_heures' => $last,
  429.             'arr_totaux' => $arr_totaux,
  430.             
  431.             'code_couleurs' => $code_couleurs,
  432.             'code_clients' => $code_clients,
  433.             'code_activites' => $code_activites,
  434.             'total_des_heures' => $total_des_heures,
  435.             'arr_tot_chaque_client' => $arr_tot_chaque_client,
  436.             'arr_tot_chaque_client_key_val' => $arr_tot_chaque_client_key_val,
  437.             'arr_tot_chaque_activite' => $arr_tot_chaque_activite,
  438.             'max_client' => $max_client,
  439.             'max_activite' => $max_activite,
  440.             'taches_unite' => $taches_unite_1,
  441.             'arr_tot_chaque_unite' => $arr_tot_chaque_unite,
  442.             'max_unite' => $max_unite,
  443.             'unite_clientCode_total' => $unite_clientCode_total,
  444.             'input_month' => $input_month,
  445.             'input_year' => $input_year
  446.         ]);
  447.     }
  448.  
  449.    
  450.     #[Route('/home'name'app_home_test')]
  451.     public function homeTest(EntityManagerInterface $em,
  452.                         TacheRepository $tacheRepository,
  453.                         UserRepository $userRepository): Response         
  454.     {
  455.         return $this->render('home/default.html.twig', [
  456.             'taches' => null,
  457.             'allTaches' => null,
  458.             'selected_user' => null
  459.         ]);
  460.     }
  461. }