src/Controller/MainController.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Agent;
  4. use App\Entity\Gare;
  5. use App\Entity\GareCreneau;
  6. use App\Entity\Jour;
  7. use App\Entity\Periode;
  8. use App\Entity\PlanningTemporaire;
  9. use App\Service\ExcelService;
  10. use App\Service\SmsService;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Notifier\Notification\Notification;
  15. use Symfony\Component\Notifier\NotifierInterface;
  16. use Symfony\Component\Notifier\Recipient\Recipient;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Ovh\Api;
  19. use Symfony\Component\HttpKernel\KernelInterface;
  20. class MainController extends AbstractController
  21. {
  22.     public function __construct(private EntityManagerInterface $em, private KernelInterface $kernel)
  23.     {
  24.         $this->em $em;
  25.         $this->kernel $kernel;
  26.     }
  27.     #[Route('/'name'app_main')]
  28.     public function index(): Response
  29.     {
  30.         return $this->redirectToRoute('admin');
  31.     }
  32.     
  33.     #[Route('/sms/test')]
  34.     public function create(SmsService $sms_service)
  35.     {
  36.         //dd($sms_service->send("test numero", "+33672774793"));
  37.         $ints = [1,43,3,94,34,9];
  38.         $array = [];
  39.         $template $this->kernel->getProjectDir().'/xls_template/Template_test.xlsx';
  40.         $xls = new ExcelService;
  41.         $xls->createFromTemplate($template);
  42.         foreach($ints as $i){
  43.             array_push($array, [$i]);
  44.         }
  45.         $xls->setDatas($array);
  46.         $filename bin2hex(random_bytes(8));
  47.         return $xls->getExcel($filename);
  48.     }
  49.     #[Route('/test'name'app_main_test')]
  50.     public function test(){
  51.         //VERIFIER JOURS DE REPOS ET REMPLACER PAR DES JOURS DE RESERVE SI POSSIBLE
  52.         //CHECK TOUTES LES AGENTS?
  53.         //PUIS TOUTES LES GARES EN FONCTION DE CES AGENTS
  54.         //PUIS TOUS LES JOURS ET VOIR CEUX NON TRAVAILLES ET NON EN REPOS
  55.         $jours $this->em->getRepository(Jour::class)->findBy(array(), array('ordre_calcul' => 'ASC'));
  56.         $name "planning_A23_0417";
  57.         $array_gares = array();
  58.         $array_reserves_par_jours_gares = array();
  59.         $uniques_gare $this->em->getRepository(Gare::class)->getPlansAgents($name);
  60.         foreach($uniques_gare as $gare){
  61.             array_push($array_gares$gare);
  62.         }
  63.         foreach($jours as $jour){
  64.             $array_reserves_par_jours_gares[$jour->getName()] = [];
  65.             foreach($array_gares as $gare){
  66.                 //rechercher reserves du jour
  67.                 $reserves $this->em->getRepository(GareCreneau::class)->getCreneauxJourGare($gare$jour);
  68.                 if(!empty($reserves)){    
  69.                     $creneaux = [];
  70.                     foreach($reserves as $res){
  71.                         $date_debut \DateTime::createFromFormat('H:i'$res['heure_debut']);
  72.                         $date_fin \DateTime::createFromFormat('H:i'$res['heure_fin']);
  73.                         $interval $date_debut->diff($date_fin);
  74.                         $temps_total $interval->i+$interval->h*60;
  75.                         $key uniqid(10);
  76.                         array_push($creneaux, array(
  77.                             "key" => $key,
  78.                             "heure_debut" => $res['heure_debut'],
  79.                             "heure_fin" => $res['heure_fin'],
  80.                             "temps_travail" => $temps_total,
  81.                             "nombre_agents_demandes" => $res['nombre_agent'],
  82.                             "nombre_agents_trouves" => 0,
  83.                             "done" => false
  84.                         ));
  85.                     }                
  86.                     array_push($array_reserves_par_jours_gares[$jour->getName()], array(
  87.                         "gare" => $gare,
  88.                         "creneaux" => $creneaux
  89.                     ));
  90.                 }
  91.             }
  92.         }
  93.         //dd($array_reserves_par_jours_gares);
  94.         foreach($array_reserves_par_jours_gares as $jour => $reserves){
  95.             $i 0;
  96.             foreach($reserves as $gare_reserve){
  97.                 //gare / creneaux
  98.                 if(!empty($gare_reserve["creneaux"])){
  99.                     //Aller chercher les agents de cette gare
  100.                     //check qu'il n'y'a pas de planning temporaire de crée ce jour la
  101.                     //S'il n'y en a pas => placer le creneau de reserve
  102.                     $agents $this->em->getRepository(Agent::class)->getOrderByContrat($gare_reserve['gare']);
  103.                     foreach($agents as $agent){
  104.                         //Check jour repos
  105.                         $is_repos false;
  106.                         foreach($agent->getJoursReposPrivilegies() as $jour_repos){
  107.                             if($jour_repos->getName() === $jour){
  108.                                 $is_repos true;
  109.                                 break;
  110.                             }
  111.                         }
  112.                         if($is_repos == false){
  113.                             //check plannings deja fais
  114.                             $plannings_agent $this->em->getRepository(PlanningTemporaire::class)->findBy(array('name' => $name'agent' => $agent'jour' => $jour));
  115.                             if(empty($plannings_agent)){
  116.                                 //Créer l'affectation a un creneau vide
  117.                                 foreach($gare_reserve["creneaux"] as $creneau){
  118.                                     
  119.                                     $current_creneau array_search($creneau['key'], array_column($array_reserves_par_jours_gares[$jour][$i]['creneaux'], 'key'));
  120.                                     if($array_reserves_par_jours_gares[$jour][$i]['creneaux'][$current_creneau]["done"] == false
  121.                                         && $array_reserves_par_jours_gares[$jour][$i]['creneaux'][$current_creneau]['nombre_agents_trouves'] < 
  122.                                         $array_reserves_par_jours_gares[$jour][$i]['creneaux'][$current_creneau]['nombre_agents_demandes']
  123.                                     ){
  124.                                         $plan = new PlanningTemporaire;
  125.                                         $plan
  126.                                             ->setReserve(true)
  127.                                             ->setAgent($agent)
  128.                                             ->setGareReserve($gare_reserve['gare'])
  129.                                             ->setHeureDebutReserve($creneau['heure_debut'])
  130.                                             ->setHeureFinReserve($creneau['heure_fin'])
  131.                                             ->setTempsTravail($creneau['temps_travail'])
  132.                                         ;
  133.                                         $this->em->persist($plan);
  134.                                         $array_reserves_par_jours_gares[$jour][$i]['creneaux'][$current_creneau]["nombre_agents_trouves"]++;
  135.                                         if($array_reserves_par_jours_gares[$jour][$i]['creneaux'][$current_creneau]["nombre_agents_trouves"] == 
  136.                                         $array_reserves_par_jours_gares[$jour][$i]['creneaux'][$current_creneau]['nombre_agents_demandes']){
  137.                                             $array_reserves_par_jours_gares[$jour][$i]['creneaux'][$current_creneau]["done"] = true;
  138.                                         }
  139.                                         break;
  140.                                     }
  141.                                 }
  142.                             }
  143.                         }
  144.                     }
  145.                 }
  146.                 $i++;
  147.             }
  148.             $this->em->flush();
  149.         }
  150.         
  151.     }
  152. }