src/MDS/AvexpressBundle/Controller/CalendarController.php line 142

Open in your IDE?
  1. <?php
  2. namespace App\MDS\AvexpressBundle\Controller;
  3. use App\Entity\User;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Psr\Log\LoggerInterface;
  9. use App\Form\WidgetNotesType;
  10. use App\Entity\WidgetNotes;
  11. use App\MDS\AvexpressBundle\Entity\AveFiles;
  12. use App\MDS\AvexpressBundle\Entity\AveProductFile;
  13. use App\MDS\GreenPatioBundle\Entity\ReservationLoungeSimple;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. use DateTime;
  16. class CalendarController extends AbstractController
  17. {
  18.     private $translator;
  19.     public function __construct(TranslatorInterface $translator) {
  20.         $this->translator $translator;
  21.     }
  22.     
  23.     /**
  24.      * @Route("/calendarav/",  name="ave_calendarav")
  25.      * Ver Calendario de Av. Se cargan expedientes Av, Proposal asociados con Av y eventos de Green Patio
  26.      */
  27.     public function calendarAvActionRequest $request)
  28.     {
  29.         $wnotes = new WidgetNotes();
  30.         $wnotes->setDateAt(new \DateTime("now"));
  31.         $form $this->createWidgetNotesCreateForm($wnotes);
  32.         return $this->render('MDS/AvexpressBundle/Avexpress/calendar.html.twig',
  33.             array(
  34.                 'form' => $form->createView(),
  35.             )
  36.         );
  37.     }
  38.     /**
  39.      * @Route("/widget/notes/calendar/create/", name="widget_notes_calendar_av_create")
  40.      */
  41.     public function addNotesAction(Request $requestLoggerInterface $logger)
  42.     {
  43.         $em $this->getDoctrine()->getManager();
  44.         $notes $em->getRepository(WidgetNotes::class)->findAll();
  45.         $wnotes = new WidgetNotes();
  46.         $form $this->createWidgetNotesCreateForm($wnotes);
  47.         $form->handleRequest($request);
  48.         $forAgent $form->get('forAgent')->getData();
  49.         if(!is_null($forAgent)){
  50.             $wnotes->setForAgent($forAgent->getId());
  51.         }
  52.         if($form->isValid())
  53.         {
  54.             /* Obtengo usuario logueado */
  55.             $user_logueado $this->get('security.token_storage')->getToken()->getUser();
  56.             $user_id $user_logueado->getId();
  57.             $wnotes->setCreatedId($user_id);
  58.             $wnotes->setUpdatedId($user_id);
  59.             /* Gestión de eventos en Log */
  60.             $user_lastname $user_logueado->getLastname();
  61.             $user_name $user_logueado->getName();
  62.             $user_email $user_logueado->getEmail();
  63.             $user_rol $user_logueado->getRoles();
  64.             $event_url $request->getPathInfo();
  65.             $event_complete $user_name.' '.$user_lastname.' - '.$user_email.' - '.$user_rol[0].' | '.$event_url;
  66.             try{
  67.                 $em->persist($wnotes);
  68.                 $em->flush();
  69.                 $event 'The Note has been created succesfully.';
  70.                 $successMessage $this->translator->trans($event);
  71.                 $this->addFlash('mensaje'$successMessage);
  72.                 $logger->info($event_complete.' | '.$event);
  73.             } catch (\Exception $e){
  74.                 $event 'An error occurred: '.$e->getMessage();
  75.                 /* Para el log */
  76.                 $logger->error($event_complete.' | '.$event);
  77.                 /* Para el usuario */
  78.                 $errorMessage $this->translator->trans($event);
  79.                 $this->addFlash('mensajeerror'$errorMessage);
  80.             }
  81.             /* Fin Gestión de eventos en Log */
  82.         }else{
  83.             $errorMessage $this->translator->trans('Error, some fields are empty');
  84.             $this->addFlash('mensajeerror'$errorMessage);
  85.         }
  86.         return $this->redirectToRoute('homepage');
  87.     }
  88.     /**
  89.      * @Route("/eventsav", name="get_eventsav")
  90.      * Eventos para el Calendario consultados desde el dashboard
  91.      */
  92.     public function eventsavAction(Request $request)
  93.     {
  94.         $em $this->getDoctrine()->getManager();
  95.         $datos = array();
  96.         $datosAV $this->buscarEventosAvFiles();
  97.         $datosGP $this->buscarEventosGreenPatio();
  98.         $datosIO $this->buscarEventosInOut();
  99.         $datos array_merge($datosAV$datosGP$datosIO);
  100.         $return = array(
  101.             'reservation' => $datos,
  102.         );
  103.         $response = new JsonResponse($return);
  104.         return $response;
  105.     }
  106.     /**
  107.      * @Route("/calendarav/avexpress/fileedit/{id}", name="get_calendar_redirection")
  108.      * Redireccion desde el calendario a los expedientes de AV
  109.      */
  110.     public function calendarRedirectionAction($idRequest $request)
  111.     {
  112.         return $this->redirectToRoute('ave_edit_file', array( 'id' => $id ));
  113.     }
  114.     private function buscarEventosAvFiles()
  115.     {
  116.         $em $this->getDoctrine()->getManager();
  117.         $todayOneYearAgo = new \Datetime('-365 days'); //Al dia actual se le resta un año
  118.         $datos = [];
  119. //
  120.         $parameters = array(
  121.             'desde' => $todayOneYearAgo,
  122.             'cancelado' => 'deleted',
  123.         );
  124. //
  125. //        $dql = 'SELECT p
  126. //                FROM App\MDS\AvexpressBundle\Entity\AveFiles p
  127. //                WHERE p.dateStart >= :desde AND p.status <> :cancelado
  128. //                ORDER BY p.dateStart ASC';
  129. //
  130. //        $query = $em->createQuery($dql)->setParameters($parameters);
  131. //        $filesAv = $query->getResult();
  132. //
  133. //        $colorAv = '#e86100';
  134. //
  135. //        foreach($filesAv as $fileAv){
  136. //            $id = $fileAv->getReservation()? 'AV'. ($fileAv->getReservation())->getId() : $fileAv->getId();
  137. //            $tooltip = 'Evento Av Express ID: '.$id.'<br>Fecha inicio: '.$fileAv->getDateStart()->format('d/m/Y').'<br>Fecha fin: '.$fileAv->getDateEnd()->format('d/m/Y');
  138. //
  139. //            $datos[] = array(
  140. //                "id" => $fileAv->getId(),
  141. //                "title" => $fileAv->getTitle(),
  142. //                "tooltip" => $tooltip,
  143. //                "date" => $fileAv->getDateStart(),
  144. //                "start" => $fileAv->getDateStart(),
  145. //                "end" => $fileAv->getDateEnd(),
  146. //                "color" => $colorAv,
  147. //                "url" => "avexpress/fileedit/".$fileAv->getId()
  148. //            );
  149. //        }
  150.         $dql 'SELECT 
  151.             p.id,
  152.             p.title,
  153.             p.dateStart,
  154.             p.dateEnd,
  155.             r.id AS reservationId
  156.         FROM App\MDS\AvexpressBundle\Entity\AveFiles p
  157.         LEFT JOIN p.reservation r
  158.         WHERE p.dateStart >= :desde AND p.status <> :cancelado
  159.         ORDER BY p.dateStart ASC';
  160.         $query $em->createQuery($dql)
  161.             ->setParameters($parameters);
  162.         $filesAv $query->getArrayResult();
  163.         $colorAv '#e86100';
  164.         $datos = [];
  165.         foreach($filesAv as $fileAv){
  166.             $id $fileAv['reservationId'] ? 'AV' $fileAv['reservationId'] : $fileAv['id'];
  167.             $tooltip 'Evento Av Express ID: ' $id '<br>Fecha inicio: ' . ($fileAv['dateStart'])->format('d/m/Y') . '<br>Fecha fin: ' . ($fileAv['dateEnd'])->format('d/m/Y');
  168.             $datos[] = [
  169.                 "id" => $fileAv['id'],
  170.                 "title" => $fileAv['title'],
  171.                 "tooltip" => $tooltip,
  172.                 "date" => $fileAv['dateStart'],
  173.                 "start" => $fileAv['dateStart'],
  174.                 "end" => $fileAv['dateEnd'],
  175.                 "color" => $colorAv,
  176.                 "url" => "avexpress/fileedit/" $fileAv['id']
  177.             ];
  178.         }
  179.         return $datos;
  180.     }
  181.     private function buscarEventosInOut()
  182.     {
  183.         $em $this->getDoctrine()->getManager();
  184.         $todayOneYearAgo = new \Datetime('-60 days'); //Al dia actual se le resta dos meses
  185.         $datos = array();
  186.         $parameters = array(
  187.             'desde' => $todayOneYearAgo,
  188.             'supAv' => 80,
  189.             'cancelado' => 'cancel',
  190.         );
  191.         $dql 'SELECT cl
  192.                 FROM App\MDS\EventsBundle\Entity\Proposal cl
  193.                 INNER JOIN EventsBundle:ProposalSupplierControl c WITH cl.id = c.proposalId
  194.                 WHERE cl.dateEventStarAt >= :desde AND c.supplierId =:supAv AND cl.status <> :cancelado';
  195.         $query $em->createQuery($dql)->setParameters($parameters);
  196.         $proposals $query->getResult();
  197. //        $colorInOut = '#12b8c4';
  198.         $colorInOut '#3ea83e';
  199.         foreach($proposals as $proposal){
  200.             // Solo agregamos los proposals que no estén asociados a un expediente de AV, asi evitamos repeticiones
  201.             $fileAv $em->getRepository(AveFiles::class)->findOneByIdProposal($proposal->getId());
  202.             if (empty($fileAv)){
  203.                 if (!empty($proposal->getAgentId())){
  204.                     $textoAgente '';
  205.                     $agent $em->getRepository(User::class)->findOneById($proposal->getAgentId());
  206.                     if (!empty($agent)){
  207.                         $textoAgente ''.'<br>Agente: '.$agent->getName().' '.$agent->getLastName();
  208.                     }
  209.                 }
  210.                 $tooltip $proposal->getTitle().', Proposal: '.$proposal->getId().'<br>Fecha inicio: '.$proposal->getDateEventStarAt()->format('d/m/Y').'<br>Fecha fin: '.$proposal->getDateEventEndAt()->format('d/m/Y').$textoAgente;
  211.                 $datos[] = array(
  212.                     "id" => $proposal->getId(),
  213.                     "title" => 'IN OUT - '.$proposal->getTitle(),
  214.                     "tooltip" => $tooltip,
  215.                     "date" => $proposal->getDateEventStarAt(),
  216.                     "start" => $proposal->getDateEventStarAt(),
  217.                     "end" => $proposal->getDateEventEndAt(),
  218.                     "color" => $colorInOut,
  219.                     "url" => ""
  220.                 );
  221.             }
  222.         }
  223.         return $datos;
  224.     }
  225.     private function buscarEventosGreenPatio()
  226.     {
  227.         $em $this->getDoctrine()->getManager();
  228.         $todayOneYearAgo = new \Datetime('-60 days'); //Al dia actual se le resta dos meses
  229.         $datos = array();
  230.         $parameters = array(
  231.             'desde' => $todayOneYearAgo,
  232.             'Confirmed' => 'Confirmed',
  233.             'Bloqueo' => 'Bloqueo',
  234.             'Invoiced' => 'Invoiced',
  235.         );
  236.         $dql 'SELECT p
  237.                 FROM App\MDS\GreenPatioBundle\Entity\Reservation p
  238.                 WHERE p.dateStart >= :desde 
  239.                 AND ((p.status = :Confirmed) OR (p.status = :Bloqueo) OR (p.status = :Invoiced)) 
  240.                 AND p.dateStart <= p.dateEnd  
  241.                 ORDER BY p.dateStart ASC';
  242.         $query $em->createQuery($dql)->setParameters($parameters);
  243.         $reservation $query->getResult();
  244.         foreach($reservation as $reserva){
  245.             $reservationLounges $em->getRepository(ReservationLoungeSimple::class)->findByIdReservation($reserva->getId());
  246.             $arrayNameLounges = array();
  247.             foreach ($reservationLounges as $sala) {
  248.                 if (($sala->getType() == 'Montaje') or ($sala->getType() == 'Desmontaje')) {
  249.                     $colorGreenPatio '#a5b8a2';
  250.                     $nameLounges $sala->getType() . ': ' $sala->getLoungeName();
  251.                     $datos[] = array(
  252.                         "id" => $reserva->getId(),
  253.                         "title" => '( '.$nameLounges.' ) -'.$reserva->getTitle(),
  254.                         "tooltip" => '<br>Inicio: '.$sala->getDateStart()->format('d/m/Y H:i').'<br>Fin: '.$sala->getDateEnd()->format('d/m/Y H:i').'<br>',
  255.                         "date" => $reserva->getDateStart(),
  256.                         "start" => $reserva->getDateStart(),
  257.                         "end" => $reserva->getDateEnd(),
  258.                         "color" => $colorGreenPatio,
  259.                         "url" => ""
  260.                     );
  261.                 } else {
  262.                     $arrayNameLounges[$sala->getIdLounge()] =  $sala->getLoungeName();
  263.                 }
  264.             }
  265.             $nameLounges implode(","$arrayNameLounges);
  266.             $tooltip $reserva->getTitle().', ID: '.$reserva->getId().'<br>Salas: '.$nameLounges.'<br>Fecha inicio: '.$reserva->getDateStart()->format('d/m/Y').'<br>Fecha fin: '.$reserva->getDateEnd()->format('d/m/Y').'<br>';
  267.             switch ($reserva->getStatus()){
  268.                 case 'Bloqueo'/* BLOQUEADO */ $colorGreenPatio '#facc61'$reserva->setTitle('BLOQUEO - '.$reserva->getTitle()); break;
  269.                 case 'Confirmed'/* CONFIRMADO */ $colorGreenPatio '#3ea83e'; break;
  270.                 case 'Invoiced'/* FACTURADO */ $colorGreenPatio '#3ea83e'; break;
  271.                 default: $colorGreenPatio '#056905'; break;
  272.             }
  273.             if (!empty($reservationLounges)){
  274.                 $datos[] = array(
  275.                     "id" => $reserva->getId(),
  276.                     "title" => '( '.$nameLounges.' ) -'.$reserva->getTitle(),
  277.                     "tooltip" => $tooltip,
  278.                     "date" => $reserva->getDateStart(),
  279.                     "start" => $reserva->getDateStart(),
  280.                     "end" => $reserva->getDateEnd(),
  281.                     "color" => $colorGreenPatio,
  282.                     "url" => ""
  283.                 );
  284.             }
  285.         }
  286.         return $datos;
  287.     }
  288.     private function createWidgetNotesCreateForm(WidgetNotes $entity)
  289.     {
  290.         $form $this->createForm(WidgetNotesType::class, $entity, array(
  291.             'action' => $this->generateUrl('widget_notes_calendar_av_create'),
  292.             'method' => 'POST'
  293.         ));
  294.         return $form;
  295.     }
  296. };