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 Psr\Log\LoggerInterface;
  27. use Symfony\Component\HttpFoundation\JsonResponse;
  28. use Symfony\Component\Routing\Annotation\Route;
  29. /**
  30.  * @Route("/higotrigo/ht/file")
  31.  */
  32. class HtFileController extends AbstractController
  33. {
  34.     private LoggerInterface $htLogger;
  35.     public function __construct(LoggerInterface $htLogger) {
  36.         $this->htLogger $htLogger;
  37.     }
  38.     /**
  39.      * @Route("/", name="app_ht_file_index", methods={"GET"})
  40.      */
  41.     public function index(HtFileRepository $htFileRepository): Response
  42.     {
  43.         return $this->render('higotrigo/ht_file/index.html.twig', [
  44.             'ht_files' => $htFileRepository->findAll(),
  45.         ]);
  46.     }
  47.     /**
  48.      * @Route("/new", name="app_ht_file_new", methods={"GET", "POST"})
  49.      */
  50.     public function new(Request $requestHtFileRepository $htFileRepository): Response
  51.     {
  52.         $htFile = new HtFile();
  53.         $form $this->createForm(HtFileType::class, $htFile);
  54.         $form->handleRequest($request);
  55.         if ($form->isSubmitted() && $form->isValid()) {
  56.             $user $this->getUser();
  57.             $htFile->setCreatedId($user);
  58.             $htFile->setUpdatedId($user);
  59.             $htFile $this->_addUserToExtras($htFile$user);
  60.             $htFileRepository->add($htFiletrue);
  61.             return $this->redirectToRoute('app_ht_file_edit', ['id' => $htFile->getId()], Response::HTTP_SEE_OTHER);
  62.         }
  63.         return $this->renderForm('higotrigo/ht_file/new.html.twig', [
  64.             'ht_file' => $htFile,
  65.             'invoices' => null,
  66.             'form' => $form,
  67.         ]);
  68.     }
  69.     /**
  70.      * @Route("/{id}", name="app_ht_file_show", methods={"GET"})
  71.      */
  72.     public function show(HtFile $htFile): Response
  73.     {
  74.         return $this->render('higotrigo/ht_file/show.html.twig', [
  75.             'ht_file' => $htFile,
  76.         ]);
  77.     }
  78.     /**
  79.      * @Route("/{id}/edit", name="app_ht_file_edit", methods={"GET", "POST"})
  80.      */
  81.     public function edit(Request $requestHtFile $htFileHtFileRepository $htFileRepositoryHtService $htServiceEntityManagerInterface $em): Response
  82.     {
  83.         $originalStatus $htFile->getStatus();
  84.         $originalClient $htFile->getClient();
  85.         $form $this->createForm(HtFileType::class, $htFile);
  86.         $form->handleRequest($request);
  87.         if ($form->isSubmitted() && $form->isValid()) {
  88.             if ($originalStatus !== $htFile->getStatus()) {
  89.                 $this->htLogger->info(sprintf(
  90.                     'Expediente %d: status cambiado de "%s" a "%s"',
  91.                     $htFile->getId(),
  92.                     $originalStatus,
  93.                     $htFile->getStatus()
  94.                 ));
  95.             }
  96.             if ($originalClient !== $htFile->getClient()) {
  97.                 $this->htLogger->info(sprintf(
  98.                     'Expediente %d: cliente cambiado de "%s" a "%s". El usuario que realiza el cambio es %s',
  99.                     $htFile->getId(),
  100.                     $originalClient->getName(),
  101.                     $htFile->getClient()->getName(),
  102.                     $this->getUser()->getEmail()
  103.                 ));
  104.             }
  105.             $user $this->getUser();
  106.             $htFile->setUpdatedId($user);
  107.             
  108.             $htFile $this->_addUserToExtras($htFile$user);
  109.             $htFileRepository->add($htFiletrue);
  110.             // return $this->redirectToRoute('app_ht_file_index', [], Response::HTTP_SEE_OTHER);
  111.         }
  112.         $invoices $em->getRepository(HtInvoice::class)->findByHtFile($htFile);
  113.         $proformas $em->getRepository(HtProforma::class)->findByHtFile($htFile->getId());
  114.         /* Obtengo usuario logueado */
  115.         $user_logueado $this->get('security.token_storage')->getToken()->getUser();
  116.         $gpFile in_array($user_logueado->getUserrol(),[9,23,24,37,47,53]) ? $em->getRepository(Reservation::class)->findOneById($htFile->getReservation()) : null;
  117.         $aveFile = !(empty($gpFile)) ? (in_array($user_logueado->getUserrol(),[9,23,24,37,47,53]) ? $em->getRepository(AveFiles::class)->findOneByReservation($gpFile) : null) : null;
  118.         return $this->renderForm('higotrigo/ht_file/edit.html.twig', [
  119.             'ht_file' => $htFile,
  120.             'invoices' => $invoices,
  121.             'proformas' => $proformas,
  122.             'gpFile' => $gpFile,
  123.             'aveFile' => $aveFile,
  124.             'form' => $form,
  125.             'totalData' => $htService->CalcTotals($htFile),
  126.         ]);
  127.     }
  128.     /**
  129.      * @Route("/{id}/edit/ajax", name="app_ht_file_edit_ajax", methods={"POST"})
  130.      */
  131.     public function editAjax(Request $requestHtFile $htFileHtFileRepository $htFileRepository): JsonResponse
  132.     {
  133.         $originalStatus $htFile->getStatus();
  134.         $originalClient $htFile->getClient();
  135.         $form $this->createForm(HtFileType::class, $htFile);
  136.         $form->handleRequest($request);
  137.         $msg 'Datos guardados correctamente';
  138.         $data = [];
  139.         if ($form->isSubmitted() && $form->isValid()) {
  140.             try {
  141.                 if ($originalStatus !== $htFile->getStatus()) {
  142.                     $this->htLogger->info(sprintf(
  143.                         'Expediente %d: status cambiado de "%s" a "%s"',
  144.                         $htFile->getId(),
  145.                         $originalStatus,
  146.                         $htFile->getStatus()
  147.                     ));
  148.                 }
  149.                 if ($originalClient !== $htFile->getClient()) {
  150.                     $this->htLogger->info(sprintf(
  151.                         'Expediente %d: cliente cambiado de "%s" a "%s". El usuario que realiza el cambio es %s',
  152.                         $htFile->getId(),
  153.                         $originalClient->getName(),
  154.                         $htFile->getClient()->getName(),
  155.                         $this->getUser()->getEmail()
  156.                     ));
  157.                 }
  158.                 $user $this->getUser();
  159.                 $htFile->setUpdatedId($user);
  160.                 
  161.                 $htFile $this->_addUserToExtras($htFile$user);
  162.                 $htFileRepository->add($htFiletrue);
  163.             } catch (\Throwable $th) {
  164.                 $msg 'Ha ocurrido un error, contacta con tu informatico de confianza';
  165.                 
  166.                 $this->htLogger->error(sprintf(
  167.                     'Expediente %d: %s',
  168.                     $htFile->getId(),
  169.                     $th->getMessage()
  170.                 ));
  171.             }
  172.         }else{
  173.             $msg 'Error al enviar los datos';
  174.             $data $form->getErrors(truefalse);
  175.         }
  176.         return $this->json([
  177.             'msg' => $msg,
  178.             'data' => $data,
  179.         ], JsonResponse::HTTP_OK);
  180.     }
  181.     /**
  182.      * @Route("/{id}", name="app_ht_file_delete", methods={"POST"})
  183.      */
  184.     public function delete(Request $requestHtFile $htFileHtFileRepository $htFileRepository): Response
  185.     {
  186.         if ($this->isCsrfTokenValid('delete'.$htFile->getId(), $request->request->get('_token'))) {
  187.             $htFileRepository->remove($htFiletrue);
  188.         }
  189.         return $this->redirectToRoute('app_ht_file_index', [], Response::HTTP_SEE_OTHER);
  190.     }
  191.     /**
  192.      * @Route("/htexternal/{id}/summary", name="app_ht_file_summary", methods={"GET", "POST"})
  193.      */
  194.     public function summary(HtFile $htFileEntityManagerInterface $em): Response
  195.     {
  196.         $arrayElem = array();
  197.         $htItems $htFile->getHtItems();
  198.         if(!empty($htItems)) {
  199.             foreach ($htItems as $htItem) {
  200.                 $arrayEscandallos = [];
  201.                 $arrayMenus = [];
  202.                 $lounge = empty($htItem->getLoungeGp()) ? $htItem->getLoungeOther() : $htItem->getLoungeGp()->getName();
  203.                 $lounge = empty($lounge) ? 'No se ha indicado la sala' $lounge;
  204.                 if (!empty($htItem->getHtMenus())) {
  205.                     foreach (($htItem->getHtMenus()) as $htMenu){
  206.                         foreach (($htMenu->getEscandallo()) as $escan){
  207.                             $arrayEscandallos[] = array('escandallo' => $escan'nota' => $escan->getHtNotes()[0],);
  208.                         }
  209.                         $arrayMenus[] = array('htMenu' => $htMenu'arrayEscandallos' => $arrayEscandallos,);
  210.                     }
  211.                 }
  212.                 $arrayElem[$htItem->getDateStart()->format('YmdHi').str_pad($htItem->getId(), 6"0"STR_PAD_LEFT)] = array(
  213.                     'htItem'=>$htItem,
  214.                     'htMenus'=> $arrayMenus,
  215.                     'lounge'=>$lounge,
  216.                 );
  217.             }
  218.         }
  219.         ksort($arrayElem);
  220.         $reserva null$lngMont null$lngDesMont null;
  221.         $reservaIdNotLinked $htFile->getReservation();
  222.         if (!empty($reservaIdNotLinked)){ $reserva $em->getRepository(Reservation::class)->findOneById($reservaIdNotLinked->getId()); }
  223.         if (!empty($reserva)){
  224.             $lngMont $em->getRepository(ReservationLoungeSimple::class)->findOneBy( array( 'idReservation' => $reserva->getId(), 'type' => 'Montaje', ) );
  225.             $lngDesMont $em->getRepository(ReservationLoungeSimple::class)->findOneBy( array( 'idReservation' => $reserva->getId(), 'type' => 'Desmontaje', ) );
  226.         }
  227.         return $this->renderForm('higotrigo/ht_file/summary-htfile-pdf.html.twig', [
  228.             'ht_file' => $htFile,
  229.             'arrayElem' => $arrayElem,
  230.             'reserva' => $reserva,
  231.             'montaje' => $lngMont,
  232.             'desmontaje' => $lngDesMont,
  233.         ]);
  234.     }
  235.     /**
  236.      * Recorremos los extras para aƱadirle el usuario que los crea y edita
  237.      */
  238.     private function _addUserToExtras(HtFile $htFileUser $user) : HtFile {
  239.         $htItems $htFile->getHtItems();
  240.         foreach ($htItems as $htItem) {
  241.             $htFile->removeHtItem($htItem); // Borrar el item para volverlo a insertar al final con los cambios de extras
  242.             $htExtras $htItem->getHtExtras();
  243.             foreach ($htExtras as $htExtra) {
  244.                 $htItem->removeHtExtra($htExtra);
  245.                 if($htExtra->getCreatedId() == NULL){
  246.                     $htExtra->setCreatedId($user);
  247.                 }
  248.                 $htExtra->setUpdatedId($user);
  249.                 $htItem->addHtExtra($htExtra);
  250.             }
  251.             $htFile->addHtItem($htItem);
  252.         }
  253.         return $htFile;
  254.     }
  255.     /**
  256.      * Comprobar si el htFile tiene htItems y si estos tienen htExtras para aƱadirselos para que la vista funcione bien.
  257.      */
  258.     private function _addHtItemAndHtExtra(HtFile $htFile) : HtFile {
  259.         $htItems $htFile->getHtItems();
  260.         if($htItems){
  261.             foreach ($htItems as $htItem) {
  262.                 $htExtras $htItem->getHtExtras();
  263.                 if($htExtras === null || $htExtras->isEmpty()){
  264.                     $htFile->removeHtItem($htItem);
  265.                     $htExtra = new HtExtra();
  266.                     $htItem->addHtExtra($htExtra);
  267.                     $htFile->addHtItem($htItem);
  268.                 }
  269.             }
  270.         }
  271.         return $htFile;
  272.     }
  273. }