src/Controller/higotrigo/HtFileController.php line 86

Open in your IDE?
  1. <?php
  2. namespace App\Controller\higotrigo;
  3. use App\Entity\ClientContact;
  4. use App\Entity\HtFile;
  5. use App\MDS\AvexpressBundle\Entity\AveFiles;
  6. use App\Entity\HtInvoice;
  7. use App\Entity\HtProforma;
  8. use App\Form\HtFileType;
  9. use App\MDS\GreenPatioBundle\Entity\Reservation;
  10. use App\MDS\GreenPatioBundle\Entity\ReservationLoungeSimple;
  11. use App\Repository\HtFileRepository;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use App\Entity\Client;
  16. use App\Entity\SettingsCompany;
  17. use App\Entity\Regions;
  18. use App\Entity\Country;
  19. use App\Entity\Cities;
  20. use App\Entity\HtExtra;
  21. use App\Entity\HtItem;
  22. use App\Entity\User;
  23. use App\Service\HtService;
  24. use Doctrine\ORM\EntityManagerInterface;
  25. use Doctrine\Common\Collections\Collection;
  26. use Symfony\Component\HttpFoundation\JsonResponse;
  27. use Symfony\Component\Routing\Annotation\Route;
  28. /**
  29.  * @Route("/higotrigo/ht/file")
  30.  */
  31. class HtFileController extends AbstractController
  32. {
  33.     /**
  34.      * @Route("/", name="app_ht_file_index", methods={"GET"})
  35.      */
  36.     public function index(HtFileRepository $htFileRepository): Response
  37.     {
  38.         return $this->render('higotrigo/ht_file/index.html.twig', [
  39.             'ht_files' => $htFileRepository->findAll(),
  40.         ]);
  41.     }
  42.     /**
  43.      * @Route("/new", name="app_ht_file_new", methods={"GET", "POST"})
  44.      */
  45.     public function new(Request $requestHtFileRepository $htFileRepository): Response
  46.     {
  47.         $htFile = new HtFile();
  48.         $form $this->createForm(HtFileType::class, $htFile);
  49.         $form->handleRequest($request);
  50.         if ($form->isSubmitted() && $form->isValid()) {
  51.             $user $this->getUser();
  52.             $htFile->setCreatedId($user);
  53.             $htFile->setUpdatedId($user);
  54.             $htFile $this->_addUserToExtras($htFile$user);
  55.             $htFileRepository->add($htFiletrue);
  56.             return $this->redirectToRoute('app_ht_file_edit', ['id' => $htFile->getId()], Response::HTTP_SEE_OTHER);
  57.         }
  58.         return $this->renderForm('higotrigo/ht_file/new.html.twig', [
  59.             'ht_file' => $htFile,
  60.             'invoices' => null,
  61.             'form' => $form,
  62.         ]);
  63.     }
  64.     /**
  65.      * @Route("/{id}", name="app_ht_file_show", methods={"GET"})
  66.      */
  67.     public function show(HtFile $htFile): Response
  68.     {
  69.         return $this->render('higotrigo/ht_file/show.html.twig', [
  70.             'ht_file' => $htFile,
  71.         ]);
  72.     }
  73.     /**
  74.      * @Route("/{id}/edit", name="app_ht_file_edit", methods={"GET", "POST"})
  75.      */
  76.     public function edit(Request $requestHtFile $htFileHtFileRepository $htFileRepositoryHtService $htServiceEntityManagerInterface $em): Response
  77.     {
  78.         $form $this->createForm(HtFileType::class, $htFile);
  79.         $form->handleRequest($request);
  80.         if ($form->isSubmitted() && $form->isValid()) {
  81.             
  82.             $user $this->getUser();
  83.             $htFile->setUpdatedId($user);
  84.             
  85.             $htFile $this->_addUserToExtras($htFile$user);
  86.             $htFileRepository->add($htFiletrue);
  87.             // return $this->redirectToRoute('app_ht_file_index', [], Response::HTTP_SEE_OTHER);
  88.         }
  89.         $invoices $em->getRepository(HtInvoice::class)->findByHtFile($htFile);
  90.         $proformas $em->getRepository(HtProforma::class)->findByHtFile($htFile->getId());
  91.         /* Obtengo usuario logueado */
  92.         $user_logueado $this->get('security.token_storage')->getToken()->getUser();
  93.         $gpFile in_array($user_logueado->getUserrol(),[9,23,24,37,47,53]) ? $em->getRepository(Reservation::class)->findOneById($htFile->getReservation()) : null;
  94.         $aveFile = !(empty($gpFile)) ? (in_array($user_logueado->getUserrol(),[9,23,24,37,47,53]) ? $em->getRepository(AveFiles::class)->findOneByReservation($gpFile) : null) : null;
  95.         return $this->renderForm('higotrigo/ht_file/edit.html.twig', [
  96.             'ht_file' => $htFile,
  97.             'invoices' => $invoices,
  98.             'proformas' => $proformas,
  99.             'gpFile' => $gpFile,
  100.             'aveFile' => $aveFile,
  101.             'form' => $form,
  102.             'totalData' => $htService->CalcTotals($htFile),
  103.         ]);
  104.     }
  105.     /**
  106.      * @Route("/{id}/edit/ajax", name="app_ht_file_edit_ajax", methods={"POST"})
  107.      */
  108.     public function editAjax(Request $requestHtFile $htFileHtFileRepository $htFileRepository): JsonResponse
  109.     {
  110.         $form $this->createForm(HtFileType::class, $htFile);
  111.         $form->handleRequest($request);
  112.         $msg 'Datos guardados correctamente';
  113.         $data = [];
  114.         if ($form->isSubmitted() && $form->isValid()) {
  115.             try {
  116.                 $user $this->getUser();
  117.                 $htFile->setUpdatedId($user);
  118.                 
  119.                 $htFile $this->_addUserToExtras($htFile$user);
  120.                 $htFileRepository->add($htFiletrue);
  121.             } catch (\Throwable $th) {
  122.                 $msg 'Ha ocurrido un error, contacta con tu informatico de confianza';
  123.             }
  124.         }else{
  125.             $msg 'Error al enviar los datos';
  126.             $data $form->getErrors(truefalse);
  127.         }
  128.         return $this->json([
  129.             'msg' => $msg,
  130.             'data' => $data,
  131.         ], JsonResponse::HTTP_OK);
  132.     }
  133.     /**
  134.      * @Route("/{id}", name="app_ht_file_delete", methods={"POST"})
  135.      */
  136.     public function delete(Request $requestHtFile $htFileHtFileRepository $htFileRepository): Response
  137.     {
  138.         if ($this->isCsrfTokenValid('delete'.$htFile->getId(), $request->request->get('_token'))) {
  139.             $htFileRepository->remove($htFiletrue);
  140.         }
  141.         return $this->redirectToRoute('app_ht_file_index', [], Response::HTTP_SEE_OTHER);
  142.     }
  143.     /**
  144.      * @Route("/htexternal/{id}/summary", name="app_ht_file_summary", methods={"GET", "POST"})
  145.      */
  146.     public function summary(HtFile $htFileEntityManagerInterface $em): Response
  147.     {
  148.         $arrayElem = array();
  149.         $htItems $htFile->getHtItems();
  150.         if(!empty($htItems)) {
  151.             foreach ($htItems as $htItem) {
  152.                 $arrayEscandallos = [];
  153.                 $arrayMenus = [];
  154.                 $lounge = empty($htItem->getLoungeGp()) ? $htItem->getLoungeOther() : $htItem->getLoungeGp()->getName();
  155.                 $lounge = empty($lounge) ? 'No se ha indicado la sala' $lounge;
  156.                 if (!empty($htItem->getHtMenus())) {
  157.                     foreach (($htItem->getHtMenus()) as $htMenu){
  158.                         foreach (($htMenu->getEscandallo()) as $escan){
  159.                             $arrayEscandallos[] = array('escandallo' => $escan'nota' => $escan->getHtNotes()[0],);
  160.                         }
  161.                         $arrayMenus[] = array('htMenu' => $htMenu'arrayEscandallos' => $arrayEscandallos,);
  162.                     }
  163.                 }
  164.                 $arrayElem[$htItem->getDateStart()->format('YmdHi').str_pad($htItem->getId(), 6"0"STR_PAD_LEFT)] = array(
  165.                     'htItem'=>$htItem,
  166.                     'htMenus'=> $arrayMenus,
  167.                     'lounge'=>$lounge,
  168.                 );
  169.             }
  170.         }
  171.         ksort($arrayElem);
  172.         $reserva null$lngMont null$lngDesMont null;
  173.         $reservaIdNotLinked $htFile->getReservation();
  174.         if (!empty($reservaIdNotLinked)){ $reserva $em->getRepository(Reservation::class)->findOneById($reservaIdNotLinked->getId()); }
  175.         if (!empty($reserva)){
  176.             $lngMont $em->getRepository(ReservationLoungeSimple::class)->findOneBy( array( 'idReservation' => $reserva->getId(), 'type' => 'Montaje', ) );
  177.             $lngDesMont $em->getRepository(ReservationLoungeSimple::class)->findOneBy( array( 'idReservation' => $reserva->getId(), 'type' => 'Desmontaje', ) );
  178.         }
  179.         return $this->renderForm('higotrigo/ht_file/summary-htfile-pdf.html.twig', [
  180.             'ht_file' => $htFile,
  181.             'arrayElem' => $arrayElem,
  182.             'reserva' => $reserva,
  183.             'montaje' => $lngMont,
  184.             'desmontaje' => $lngDesMont,
  185.         ]);
  186.     }
  187.     /**
  188.      * Recorremos los extras para aƱadirle el usuario que los crea y edita
  189.      */
  190.     private function _addUserToExtras(HtFile $htFileUser $user) : HtFile {
  191.         $htItems $htFile->getHtItems();
  192.         foreach ($htItems as $htItem) {
  193.             $htFile->removeHtItem($htItem); // Borrar el item para volverlo a insertar al final con los cambios de extras
  194.             $htExtras $htItem->getHtExtras();
  195.             foreach ($htExtras as $htExtra) {
  196.                 $htItem->removeHtExtra($htExtra);
  197.                 if($htExtra->getCreatedId() == NULL){
  198.                     $htExtra->setCreatedId($user);
  199.                 }
  200.                 $htExtra->setUpdatedId($user);
  201.                 $htItem->addHtExtra($htExtra);
  202.             }
  203.             $htFile->addHtItem($htItem);
  204.         }
  205.         return $htFile;
  206.     }
  207.     /**
  208.      * Comprobar si el htFile tiene htItems y si estos tienen htExtras para aƱadirselos para que la vista funcione bien.
  209.      */
  210.     private function _addHtItemAndHtExtra(HtFile $htFile) : HtFile {
  211.         $htItems $htFile->getHtItems();
  212.         if($htItems){
  213.             foreach ($htItems as $htItem) {
  214.                 $htExtras $htItem->getHtExtras();
  215.                 if($htExtras === null || $htExtras->isEmpty()){
  216.                     $htFile->removeHtItem($htItem);
  217.                     $htExtra = new HtExtra();
  218.                     $htItem->addHtExtra($htExtra);
  219.                     $htFile->addHtItem($htItem);
  220.                 }
  221.             }
  222.         }
  223.         return $htFile;
  224.     }
  225. }