<?php
namespace App\Controller;
use App\Entity\Agent;
use App\Entity\Gare;
use App\Entity\GareCreneau;
use App\Entity\Jour;
use App\Entity\Periode;
use App\Entity\PlanningTemporaire;
use App\Service\ExcelService;
use App\Service\SmsService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\NotifierInterface;
use Symfony\Component\Notifier\Recipient\Recipient;
use Symfony\Component\Routing\Annotation\Route;
use Ovh\Api;
use Symfony\Component\HttpKernel\KernelInterface;
class MainController extends AbstractController
{
public function __construct(private EntityManagerInterface $em, private KernelInterface $kernel)
{
$this->em = $em;
$this->kernel = $kernel;
}
#[Route('/', name: 'app_main')]
public function index(): Response
{
return $this->redirectToRoute('admin');
}
#[Route('/sms/test')]
public function create(SmsService $sms_service)
{
//dd($sms_service->send("test numero", "+33672774793"));
$ints = [1,43,3,94,34,9];
$array = [];
$template = $this->kernel->getProjectDir().'/xls_template/Template_test.xlsx';
$xls = new ExcelService;
$xls->createFromTemplate($template);
foreach($ints as $i){
array_push($array, [$i]);
}
$xls->setDatas($array);
$filename = bin2hex(random_bytes(8));
return $xls->getExcel($filename);
}
#[Route('/test', name: 'app_main_test')]
public function test(){
//VERIFIER JOURS DE REPOS ET REMPLACER PAR DES JOURS DE RESERVE SI POSSIBLE
//CHECK TOUTES LES AGENTS?
//PUIS TOUTES LES GARES EN FONCTION DE CES AGENTS
//PUIS TOUS LES JOURS ET VOIR CEUX NON TRAVAILLES ET NON EN REPOS
$jours = $this->em->getRepository(Jour::class)->findBy(array(), array('ordre_calcul' => 'ASC'));
$name = "planning_A23_0417";
$array_gares = array();
$array_reserves_par_jours_gares = array();
$uniques_gare = $this->em->getRepository(Gare::class)->getPlansAgents($name);
foreach($uniques_gare as $gare){
array_push($array_gares, $gare);
}
foreach($jours as $jour){
$array_reserves_par_jours_gares[$jour->getName()] = [];
foreach($array_gares as $gare){
//rechercher reserves du jour
$reserves = $this->em->getRepository(GareCreneau::class)->getCreneauxJourGare($gare, $jour);
if(!empty($reserves)){
$creneaux = [];
foreach($reserves as $res){
$date_debut = \DateTime::createFromFormat('H:i', $res['heure_debut']);
$date_fin = \DateTime::createFromFormat('H:i', $res['heure_fin']);
$interval = $date_debut->diff($date_fin);
$temps_total = $interval->i+$interval->h*60;
$key = uniqid(10);
array_push($creneaux, array(
"key" => $key,
"heure_debut" => $res['heure_debut'],
"heure_fin" => $res['heure_fin'],
"temps_travail" => $temps_total,
"nombre_agents_demandes" => $res['nombre_agent'],
"nombre_agents_trouves" => 0,
"done" => false
));
}
array_push($array_reserves_par_jours_gares[$jour->getName()], array(
"gare" => $gare,
"creneaux" => $creneaux
));
}
}
}
//dd($array_reserves_par_jours_gares);
foreach($array_reserves_par_jours_gares as $jour => $reserves){
$i = 0;
foreach($reserves as $gare_reserve){
//gare / creneaux
if(!empty($gare_reserve["creneaux"])){
//Aller chercher les agents de cette gare
//check qu'il n'y'a pas de planning temporaire de crée ce jour la
//S'il n'y en a pas => placer le creneau de reserve
$agents = $this->em->getRepository(Agent::class)->getOrderByContrat($gare_reserve['gare']);
foreach($agents as $agent){
//Check jour repos
$is_repos = false;
foreach($agent->getJoursReposPrivilegies() as $jour_repos){
if($jour_repos->getName() === $jour){
$is_repos = true;
break;
}
}
if($is_repos == false){
//check plannings deja fais
$plannings_agent = $this->em->getRepository(PlanningTemporaire::class)->findBy(array('name' => $name, 'agent' => $agent, 'jour' => $jour));
if(empty($plannings_agent)){
//Créer l'affectation a un creneau vide
foreach($gare_reserve["creneaux"] as $creneau){
$current_creneau = array_search($creneau['key'], array_column($array_reserves_par_jours_gares[$jour][$i]['creneaux'], 'key'));
if($array_reserves_par_jours_gares[$jour][$i]['creneaux'][$current_creneau]["done"] == false
&& $array_reserves_par_jours_gares[$jour][$i]['creneaux'][$current_creneau]['nombre_agents_trouves'] <
$array_reserves_par_jours_gares[$jour][$i]['creneaux'][$current_creneau]['nombre_agents_demandes']
){
$plan = new PlanningTemporaire;
$plan
->setReserve(true)
->setAgent($agent)
->setGareReserve($gare_reserve['gare'])
->setHeureDebutReserve($creneau['heure_debut'])
->setHeureFinReserve($creneau['heure_fin'])
->setTempsTravail($creneau['temps_travail'])
;
$this->em->persist($plan);
$array_reserves_par_jours_gares[$jour][$i]['creneaux'][$current_creneau]["nombre_agents_trouves"]++;
if($array_reserves_par_jours_gares[$jour][$i]['creneaux'][$current_creneau]["nombre_agents_trouves"] ==
$array_reserves_par_jours_gares[$jour][$i]['creneaux'][$current_creneau]['nombre_agents_demandes']){
$array_reserves_par_jours_gares[$jour][$i]['creneaux'][$current_creneau]["done"] = true;
}
break;
}
}
}
}
}
}
$i++;
}
$this->em->flush();
}
}
}