src/Controller/Publico/PedidosPublicController.php line 120

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Publico;
  3. use App\Entity\Ajustes\HistComisiones;
  4. use App\Entity\Ajustes\Pedido;
  5. use App\Form\Privado\Filtros\Catalogo\PedidoFilterType;
  6. use App\Form\Publico\Pedido\PedidoPaso2Type;
  7. use App\Form\Publico\Pedido\PedidoPaso3Type;
  8. use App\Form\Publico\Pedido\PedidoUsuarioType;
  9. use App\Repository\Ajustes\ConfigRepository;
  10. use App\Repository\Ajustes\PedidoRepository;
  11. use App\Service\EmailService;
  12. use App\Service\PDFService;
  13. use App\Utils\FilterSession;
  14. use App\Utils\HelperUtil;
  15. use DateTime;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use Exception;
  18. use Knp\Component\Pager\PaginatorInterface;
  19. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  20. use Sonata\SeoBundle\Seo\SeoPageInterface;
  21. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  22. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  23. use Symfony\Component\HttpFoundation\RedirectResponse;
  24. use Symfony\Component\HttpFoundation\Request;
  25. use Symfony\Component\HttpFoundation\Response;
  26. use Symfony\Component\Routing\Annotation\Route;
  27. use Symfony\Component\Security\Core\Security;
  28. use Symfony\Contracts\Translation\TranslatorInterface;
  29. use WhiteOctober\BreadcrumbsBundle\Model\Breadcrumbs;
  30. use Sermepa\Tpv\Tpv;
  31. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  32. /**
  33.  * @Route("/carrito", name="usu_pedidos_")
  34.  */
  35. class PedidosPublicController extends AbstractController
  36. {
  37.     public const BASEDIR Pedido::BASEDIRUSER;
  38.     public const ICON Pedido::ICON;
  39.     protected $em;
  40.     protected $bd;
  41.     protected $bag;
  42.     protected $pag;
  43.     protected $hlp;
  44.     protected $repo;
  45.     protected $trans;
  46.     protected $seo;
  47.     protected $sec;
  48.     protected $email;
  49.     protected $repoConf;
  50.     protected $pdf;
  51.     public function __construct(EntityManagerInterface $emBreadcrumbs $bdPaginatorInterface $pgPedidoRepository $repoHelperUtil $hlpParameterBagInterface $pbTranslatorInterface $transSeoPageInterface $seoSecurity $secEmailService $emailConfigRepository $repoConfPDFService $pdf)
  52.     {
  53.         $this->em $em;
  54.         $this->bd $bd;
  55.         $this->bag $pb;
  56.         $this->pag $pg;
  57.         $this->repo $repo;
  58.         $this->hlp $hlp;
  59.         $this->trans $trans;
  60.         $this->seo $seo;
  61.         $this->sec $sec;
  62.         $this->SendMail $email;
  63.         $this->repoConf $repoConf;
  64.         $this->pdf $pdf;
  65.     }
  66.     /**
  67.      * @Route("/historico_pedidos", name="index", options={"expose"=true})
  68.      * @IsGranted("ROLE_PEDIDOS_LIST")
  69.      */
  70.     public function indexAction(Request $request): Response
  71.     {
  72.         // Filter data
  73.         $default_values = ['sort' => 'pedido.id''order' => 'desc''page' => 1'limit' => 12];
  74.         $filtro = new FilterSession($request$default_values'pedido_filter'$this->bag);
  75.         $data_Sesio $filtro->checkFiltroForm();
  76.         $form_filtro $this->createForm(PedidoFilterType::class, $data_Sesio);
  77.         $data $form_filtro->handleRequest($request)->getData();
  78.         if ($form_filtro->isSubmitted() && $form_filtro->isValid()) {
  79.             $filtro->aƱadirFiltrosForm($data);
  80.         }
  81.         // Pagination values
  82.         $resultBD $this->repo->filter(array_merge($data, ['user' => $this->getUser()]), ['getQuery' => true]);
  83.         $pagination $this->pag->paginate(
  84.             $resultBD// query NOT result
  85.              $filtro->getPage(), // page number
  86.              $filtro->getLimit(), // limit per page
  87.              $filtro->getSortKnp()
  88.         );
  89.         //SEO
  90.         $titulo =$this->trans->trans('pedido.usuario.index.seo.title', [], 'pedido');
  91.         $descripcion =$this->trans->trans('pedido.usuario.index.seo.desc', [], 'pedido');
  92.         $this->seo 
  93.             ->setTitle($titulo)
  94.             ->addMeta('name''description'$descripcion)
  95.             ->addMeta('property''og:title'$titulo)
  96.             ->addMeta('property''og:description'$descripcion)
  97.         ;
  98.         return $this->render(self::BASEDIR.'/ped_index.html.twig', [
  99.             'BaseDir' => self::BASEDIR,
  100.             'ICON' => self::ICON,
  101.             'pagination' => $pagination,
  102.             'form_filtro' => $form_filtro->createView(),
  103.             'p_filter' => $filtro->getData(),
  104.         ]);
  105.     }
  106.     /**
  107.      * @Route("/crear", name="new", methods = {"GET", "POST"}, options={"expose"=true})
  108.      * @IsGranted("ROLE_PEDIDO_CREATE")
  109.      */
  110.     public function new(Request $request): Response
  111.     {
  112.         //Averiguar si el usuario ya tiene un Pedido en Pendiente, es decir sin verificar
  113.         //Si es el caso, reedirigir al carrito
  114.         $pedidosUser $this->repo->findPedidoCarrito();
  115.         if ($pedidosUser){
  116.             return $this->redirectToRoute('usu_pedidos_finish');
  117.         }
  118.         //Comprovar el rol del client per a ficar la pantalla fullwidth perque sino no cap
  119.         if($this->sec->isGranted('ROLE_PEDIDOS_CREATE_PROFESIONAL')){
  120.             $profesional true;
  121.         }else{
  122.             $profesional false;
  123.         }
  124.         //Pasos del pedido
  125.         $stepper=[
  126.             '1' => "current",
  127.             '2' => "",
  128.             '3' => "",
  129.             '4' => "",
  130.             "5" => "",
  131.         ];
  132.         $entity = new Pedido();
  133.         $form $this->createForm(PedidoUsuarioType::class, $entity);
  134.         $form->handleRequest($request);
  135.         if ($form->isSubmitted()) {
  136.             if ($form->isValid()) {
  137.                 if(count($entity->getPedGradeadasLineas()) == && count($entity->getPedProductoLineas()) == 0){
  138.                     $this->addFlash('danger'$this->trans->trans('pedido.new.error1', [], 'pedido'));
  139.                 }else{
  140.                     try {
  141.                         if($entity->getPedUsuario() == null){
  142.                             $entity->setPedUsuario($this->getUser());
  143.                         }
  144.                         //if($entity->getUsuFullName() == null || $entity->getUsuFullName() == null)
  145.                         $entity->setPedEstado(Pedido::ESTADO_PENDIENTE);
  146.                         $this->em->persist($entity);
  147.                         $this->em->flush();
  148.                         if ($form->getClickedButton() && 'guardarvolver' === $form->getClickedButton()->getName()) {
  149.                             return $this->redirectToRoute('usu_pedidos_finish');
  150.                         }
  151.                         return $this->redirectToRoute('usu_pedidos_paso2');
  152.                     } catch (\Exception $e) {
  153.                         $this->hlp->error($e);
  154.                         $this->addFlash('danger'$this->trans->trans('pedido.new.error2', [], 'pedido') );
  155.                         if ($this->isGranted('ROLE_SYSTEM_INFO')) {
  156.                             $this->addFlash('danger'$e->getMessage());
  157.                         }
  158.                     }
  159.                 }
  160.                 
  161.             } else {
  162.                 $this->addFlash('danger'$this->trans->trans('pedido.new.error3', [], 'pedido'));
  163.             }
  164.         }
  165.         //SEO
  166.         $titulo =$this->trans->trans('pedido.new.seo.title', [], 'pedido');
  167.         $descripcion =$this->trans->trans('pedido.new.seo.desc', [], 'pedido');
  168.         $this->seo 
  169.             ->setTitle($titulo)
  170.             ->addMeta('name''description'$descripcion)
  171.             ->addMeta('property''og:title'$titulo)
  172.             ->addMeta('property''og:description'$descripcion)
  173.         ;
  174.         return $this->render(self::BASEDIR.'/ped_new.html.twig', [
  175.             'profesional' => $profesional,
  176.             'stepper' => $stepper,
  177.             'pedido' => $entity,
  178.             'form' => $form->createView(),
  179.             'BaseDir' => self::BASEDIR,
  180.             'ICON' => self::ICON,
  181.         ]);
  182.     }
  183.     /**
  184.      * @Route("/finalizar", name = "finish", methods = {"GET", "POST"}, options={"expose"=true})
  185.      * @IsGranted("ROLE_PEDIDO_CREATE")
  186.      */
  187.     public function finish(Request $request): Response
  188.     {
  189.         $entity $this->repo->findPedidoCarrito();
  190.         if($entity == null){
  191.             return $this->redirectToRoute('usu_pedidos_new');
  192.         }
  193.         
  194.         //Comprovar el rol del client per a ficar la pantalla fullwidth perque sino no cap
  195.         if($this->sec->isGranted('ROLE_PEDIDOS_CREATE_PROFESIONAL')){
  196.             $profesional true;
  197.         }else{
  198.             $profesional false;
  199.         }
  200.         $stepper=[
  201.             '1' => "current",
  202.             '2' => "",
  203.             '3' => "",
  204.             '4' => "",
  205.             "5" => "",
  206.         ];
  207.         $form $this->createForm(PedidoUsuarioType::class, $entity);
  208.         $form->handleRequest($request);
  209.         if ($form->isSubmitted()) {
  210.             if ($form->isValid()) {
  211.                 if(count($entity->getPedGradeadasLineas()) == && count($entity->getPedProductoLineas()) == 0){
  212.                     $this->addFlash('danger'$this->trans->trans('pedido.new.error1', [], 'pedido'));
  213.                 }else{
  214.                     try {
  215.                         foreach($entity->getPedGradeadasLineas() as $Card){
  216.                             $entity->addPedGradeadasLinea($Card);
  217.                         }
  218.                         //Per asegurarmos que entra al listener
  219.                         $entity->setUpdatedAt(new DateTime());
  220.                         $this->em->flush();
  221.                         if ($form->getClickedButton() && 'guardarvolver' === $form->getClickedButton()->getName()) {
  222.                             return $this->redirectToRoute('usu_pedidos_finish');
  223.                         }
  224.     
  225.                         return $this->redirectToRoute('usu_pedidos_paso2');
  226.                     } catch (\Exception $e) {
  227.                         $this->hlp->error($e);
  228.                         $this->addFlash('danger'$this->trans->trans('pedido.new.error2', [], 'pedido') );
  229.                         if ($this->isGranted('ROLE_SYSTEM_INFO')) {
  230.                             $this->addFlash('danger'$e->getMessage());
  231.                         }
  232.                     }
  233.                 }
  234.                 
  235.             } else {
  236.                 $this->addFlash('danger'$this->trans->trans('pedido.new.error3', [], 'pedido'));
  237.             }
  238.         }
  239.         //SEO
  240.         $titulo =$this->trans->trans('pedido.usuario.carrito.seo.title', [], 'pedido');
  241.         $descripcion =$this->trans->trans('pedido.usuario.carrito.seo.desc', [], 'pedido');
  242.         $this->seo 
  243.             ->setTitle($titulo)
  244.             ->addMeta('name''description'$descripcion)
  245.             ->addMeta('property''og:title'$titulo)
  246.             ->addMeta('property''og:description'$descripcion)
  247.         ;
  248.         return $this->render(self::BASEDIR.'/ped_new.html.twig', [
  249.             'profesional' => $profesional,
  250.             'stepper' => $stepper,
  251.             'pedido' => $entity,
  252.             'form' => $form->createView(),
  253.             'BaseDir' => self::BASEDIR,
  254.             'ICON' => self::ICON,
  255.         ]);
  256.     }
  257.     /**
  258.      * @Route("/paso/2", name = "paso2", methods = {"GET", "POST"})
  259.      * @IsGranted("ROLE_PEDIDO_CREATE")
  260.      */
  261.     public function paso2(Request $request): Response
  262.     {
  263.         $entity $this->repo->findPedidoCarrito();
  264.         if($entity == null){
  265.             return $this->redirectToRoute('usu_pedidos_new');
  266.         }
  267.         if(count($entity->getPedGradeadasLineas()) == && count($entity->getPedProductoLineas()) == 0){
  268.             return $this->redirectToRoute('usu_pedidos_finish');
  269.         }
  270.         $stepper=[
  271.             '1' => "completed",
  272.             '2' => "current",
  273.             '3' => "",
  274.             '4' => "",
  275.             "5" => "",
  276.         ];
  277.         $form $this->createForm(PedidoPaso2Type::class, $entity);
  278.         $form->handleRequest($request);
  279.         if ($form->isSubmitted()) {
  280.             if ($form->isValid()) {
  281.                 try {
  282.                     if($entity->getPedDireccionFacturacion() == '' || $entity->getPedDireccionFacturacion() == null){
  283.                         $entity->setPedDireccionFacturacion($entity->getUsuAddress());
  284.                     }
  285.                     //Per asegurarmos que entra al listener
  286.                     $entity->setUpdatedAt(new DateTime());
  287.                     $this->em->flush();
  288.                     if ($form->getClickedButton() && 'guardarvolver' === $form->getClickedButton()->getName()) {
  289.                         return $this->redirectToRoute('usu_pedidos_finish');
  290.                     }
  291.                     return $this->redirectToRoute('usu_pedidos_paso3');
  292.                 } catch (\Exception $e) {
  293.                     $this->hlp->error($e);
  294.                     $this->addFlash('danger'$this->trans->trans('pedido.new.error2', [], 'pedido') );
  295.                     if ($this->isGranted('ROLE_SYSTEM_INFO')) {
  296.                         $this->addFlash('danger'$e->getMessage());
  297.                     }
  298.                 }   
  299.             } else {
  300.                 $this->addFlash('danger'$this->trans->trans('pedido.new.error3', [], 'pedido'));
  301.             }
  302.         }
  303.         //SEO
  304.         $titulo =$this->trans->trans('pedido.usuario.carrito.seo.title', [], 'pedido');
  305.         $descripcion =$this->trans->trans('pedido.usuario.carrito.seo.desc', [], 'pedido');
  306.         $this->seo 
  307.             ->setTitle($titulo)
  308.             ->addMeta('name''description'$descripcion)
  309.             ->addMeta('property''og:title'$titulo)
  310.             ->addMeta('property''og:description'$descripcion)
  311.         ;
  312.         return $this->render(self::BASEDIR.'/ped_new.html.twig', [
  313.             'stepper' => $stepper,
  314.             'pedido' => $entity,
  315.             'form' => $form->createView(),
  316.             'BaseDir' => self::BASEDIR,
  317.             'ICON' => self::ICON,
  318.         ]);
  319.     }
  320.     /**
  321.      * @Route("/paso/3", name = "paso3", methods = {"GET", "POST"}, options={"expose"=true})
  322.      * @IsGranted("ROLE_PEDIDO_CREATE")
  323.      */
  324.     public function paso3(Request $request): Response
  325.     {
  326.         $entity $this->repo->findPedidoCarrito();
  327.         if($entity == null){
  328.             return $this->redirectToRoute('usu_pedidos_new');
  329.         }
  330.         if(count($entity->getPedGradeadasLineas()) == && count($entity->getPedProductoLineas()) == 0){
  331.             return $this->redirectToRoute('usu_pedidos_finish');
  332.         }
  333.         if($entity->getUsuAddress() == null || ($entity->getPedPayMethod() == null && $this->sec->isGranted('ROLE_PEDIDOS_CREATE_PROFESIONAL') == false) || $entity->getPedEnvioMetodo() == null ){
  334.             $this->addFlash('danger'$this->trans->trans('pedido.new.error3', [], 'pedido'));
  335.             return $this->redirectToRoute('usu_pedidos_paso2');
  336.         }
  337.         
  338.         //Comprovar el rol del client per a ficar la pantalla fullwidth perque sino no cap
  339.         //Quan estiga el pass de pagar modificar esta funcio
  340.         if($this->sec->isGranted('ROLE_PEDIDOS_CREATE_PROFESIONAL')){
  341.             $profesional true;
  342.             if($this->sec->getUser()->getUsuComentarioDescuento() != null){
  343.                 $this->addFlash('info'$this->sec->getUser()->getUsuComentarioDescuento());
  344.             }
  345.         }else{
  346.             $profesional false;
  347.         }
  348.         $stepper=[
  349.             '1' => "completed",
  350.             '2' => "completed",
  351.             '3' => "current",
  352.             '4' => "",
  353.             "5" => "",
  354.         ];
  355.         $form $this->createForm(PedidoPaso3Type::class, $entity);
  356.         $form->handleRequest($request);
  357.         if ($form->isSubmitted()) {
  358.             if ($form->isValid()) {        
  359.                 try {
  360.                     
  361.                     if ($form->getClickedButton() && 'guardarvolver' === $form->getClickedButton()->getName()) {
  362.                         return $this->redirectToRoute('usu_pedidos_paso2');
  363.                     }
  364.                 
  365.                     $entity->setPedVerificado(true);
  366.                     $this->em->flush();
  367.                     if($profesional){
  368.                         return $this->redirectToRoute('usu_pedidos_paso5');
  369.                     }else{
  370.                         return $this->redirectToRoute('usu_pedidos_paso4');
  371.                     }
  372.                 } catch (\Exception $e) {
  373.                     $this->hlp->error($e);
  374.                     $this->addFlash('danger'$this->trans->trans('pedido.new.error2', [], 'pedido') );
  375.                     if ($this->isGranted('ROLE_SYSTEM_INFO')) {
  376.                         $this->addFlash('danger'$e->getMessage());
  377.                     }
  378.                 }   
  379.             } else {
  380.                 $this->addFlash('danger'$this->trans->trans('pedido.new.error3', [], 'pedido'));
  381.             }
  382.         }
  383.         //SEO
  384.         $titulo =$this->trans->trans('pedido.usuario.carrito.seo.title', [], 'pedido');
  385.         $descripcion =$this->trans->trans('pedido.usuario.carrito.seo.desc', [], 'pedido');
  386.         $this->seo 
  387.             ->setTitle($titulo)
  388.             ->addMeta('name''description'$descripcion)
  389.             ->addMeta('property''og:title'$titulo)
  390.             ->addMeta('property''og:description'$descripcion)
  391.         ;
  392.         if($profesional){
  393.             //cargar vista profesional, masa diferencies per a fer-ho tot en una
  394.             return $this->render(self::BASEDIR.'/ped_new_paso3_pro.html.twig', [
  395.                 'stepper' => $stepper,
  396.                 'pedido' => $entity,
  397.                 'form' => $form->createView(),
  398.                 'BaseDir' => self::BASEDIR,
  399.                 'ICON' => self::ICON,
  400.             ]);
  401.         }else{
  402.             return $this->render(self::BASEDIR.'/ped_new_paso3.html.twig', [
  403.                 'stepper' => $stepper,
  404.                 'pedido' => $entity,
  405.                 'form' => $form->createView(),
  406.                 'BaseDir' => self::BASEDIR,
  407.                 'ICON' => self::ICON,
  408.             ]);
  409.         } 
  410.     }
  411.     /**
  412.      * @Route("/paso/4", name = "paso4", methods = {"GET", "POST"})
  413.      * @IsGranted("ROLE_PEDIDO_CREATE")
  414.      */
  415.     public function paso4(Request $request): Response
  416.     {
  417.         $entity $this->repo->findPedidoCarrito();
  418.         if($entity == null){
  419.             return $this->redirectToRoute('usu_pedidos_new');
  420.         }
  421.         if(count($entity->getPedGradeadasLineas()) == && count($entity->getPedProductoLineas()) == 0){
  422.             return $this->redirectToRoute('usu_pedidos_finish');
  423.         }
  424.         if($entity->getUsuAddress() == null || ($entity->getPedPayMethod() == null && $this->sec->isGranted('ROLE_PEDIDOS_CREATE_PROFESIONAL') == false) || $entity->getPedEnvioMetodo() == null ){
  425.             $this->addFlash('danger'$this->trans->trans('pedido.new.error3', [], 'pedido'));
  426.             return $this->redirectToRoute('usu_pedidos_paso2');
  427.         }
  428.         if($entity->getPedVerificado() == false){
  429.             $this->addFlash('danger'$this->trans->trans('pedido.new.error3', [], 'pedido'));
  430.             return $this->redirectToRoute('usu_pedidos_paso3');
  431.         }
  432.         
  433.         $stepper=[
  434.             '1' => "completed",
  435.             '2' => "completed",
  436.             '3' => "completed",
  437.             '4' => "current",
  438.             "5" => "",
  439.         ];
  440.         //SEO
  441.         $titulo =$this->trans->trans('pedido.usuario.carrito.seo.title', [], 'pedido');
  442.         $descripcion =$this->trans->trans('pedido.usuario.carrito.seo.desc', [], 'pedido');
  443.         $this->seo 
  444.             ->setTitle($titulo)
  445.             ->addMeta('name''description'$descripcion)
  446.             ->addMeta('property''og:title'$titulo)
  447.             ->addMeta('property''og:description'$descripcion)
  448.         ;
  449.         return $this->render(self::BASEDIR.'/ped_new_paso4.html.twig', [
  450.             'stepper' => $stepper,
  451.             'pedido' => $entity,
  452.             'BaseDir' => self::BASEDIR,
  453.             'ICON' => self::ICON,
  454.         ]);
  455.     }
  456.     /**
  457.      * @Route("/paso/5", name = "paso5", methods = {"GET", "POST"})
  458.      * @IsGranted("ROLE_PEDIDO_CREATE")
  459.      */
  460.     public function paso5(Request $request): Response
  461.     {
  462.        
  463.         $entity $this->repo->findPedidoCarrito();
  464.         if($entity == null){
  465.             return $this->redirectToRoute('usu_pedidos_new');
  466.         }
  467.         if(count($entity->getPedGradeadasLineas()) == && count($entity->getPedProductoLineas()) == 0){
  468.             return $this->redirectToRoute('usu_pedidos_finish');
  469.         }
  470.         if($entity->getUsuAddress() == null || ($entity->getPedPayMethod() == null && $this->sec->isGranted('ROLE_PEDIDOS_CREATE_PROFESIONAL') == false) || $entity->getPedEnvioMetodo() == null ){
  471.             $this->addFlash('danger'$this->trans->trans('pedido.new.error3', [], 'pedido'));
  472.             return $this->redirectToRoute('usu_pedidos_paso2');
  473.         }
  474.         if($entity->getPedVerificado() == false){
  475.             $this->addFlash('danger'$this->trans->trans('pedido.new.error3', [], 'pedido'));
  476.             return $this->redirectToRoute('usu_pedidos_paso3');
  477.         }
  478.         if($entity->getPedVerificado() == false){
  479.             $this->addFlash('danger'$this->trans->trans('pedido.new.error3', [], 'pedido'));
  480.             return $this->redirectToRoute('usu_pedidos_paso3');
  481.         }
  482.         if($entity->getPedFechaPago() == null && $this->sec->isGranted('ROLE_PEDIDOS_CREATE_PROFESIONAL') == false){
  483.             $this->addFlash('danger'$this->trans->trans('pedido.new.error3', [], 'pedido'));
  484.             return $this->redirectToRoute('usu_pedidos_paso4');
  485.         }
  486.         $stepper=[
  487.             '1' => "completed",
  488.             '2' => "completed",
  489.             '3' => "completed",
  490.             '4' => "completed",
  491.             "5" => "current",
  492.         ];
  493.         if($this->sec->isGranted('ROLE_PEDIDOS_CREATE_PROFESIONAL')){
  494.             $entity->setPedEstado(Pedido::ESTADO_PAGO_PENDIENTE);
  495.         }else{
  496.             $entity->setPedEstado(Pedido::ESTADO_PAGADO);
  497.         }
  498.         $this->em->flush();
  499.         //Calcular historico
  500.         $historico = new HistComisiones();
  501.         $historico->setHistComPedido($entity);              
  502.         $this->em->persist($historico);
  503.         $this->em->flush();
  504.         $this->addFlash('success'$this->trans->trans('pedido.new.success2', [], 'pedido'));
  505.         //Enviar correu de confirmació al client
  506.         if($this->sec->isGranted('ROLE_PEDIDOS_CREATE_PROFESIONAL')){
  507.             $this->enviarEmailPedidoPro($entity);
  508.         }else{
  509.             $this->enviarEmailPedido($entity);
  510.         }
  511.         //SEO
  512.         $titulo =$this->trans->trans('pedido.usuario.carrito.seo.title', [], 'pedido');
  513.         $descripcion =$this->trans->trans('pedido.usuario.carrito.seo.desc', [], 'pedido');
  514.         $this->seo 
  515.             ->setTitle($titulo)
  516.             ->addMeta('name''description'$descripcion)
  517.             ->addMeta('property''og:title'$titulo)
  518.             ->addMeta('property''og:description'$descripcion)
  519.         ;
  520.         return $this->render(self::BASEDIR.'/ped_new_paso5.html.twig', [
  521.             'stepper' => $stepper,
  522.             'pedido' => $entity,
  523.             'BaseDir' => self::BASEDIR,
  524.             'ICON' => self::ICON,
  525.         ]);
  526.     }
  527.     /**
  528.      * @Route("/paso/pago_tpv", name = "pago_tpv", methods = {"GET", "POST"})
  529.      * @IsGranted("ROLE_PEDIDO_CREATE")
  530.      */
  531.     public function pagoTpv(Request $request): Response
  532.     {
  533.         $entity $this->repo->findPedidoCarrito();
  534.         if($entity == null){
  535.             return $this->redirectToRoute('usu_pedidos_new');
  536.         }
  537.         if(count($entity->getPedGradeadasLineas()) == && count($entity->getPedProductoLineas()) == 0){
  538.             return $this->redirectToRoute('usu_pedidos_finish');
  539.         }
  540.         if($entity->getUsuAddress() == null || ($entity->getPedPayMethod() == null && $this->sec->isGranted('ROLE_PEDIDOS_CREATE_PROFESIONAL') == false) || $entity->getPedEnvioMetodo() == null ){
  541.             $this->addFlash('danger'$this->trans->trans('pedido.new.error3', [], 'pedido'));
  542.             return $this->redirectToRoute('usu_pedidos_paso2');
  543.         }
  544.         if($entity->getPedVerificado() == false){
  545.             $this->addFlash('danger'$this->trans->trans('pedido.new.error3', [], 'pedido'));
  546.             return $this->redirectToRoute('usu_pedidos_paso3');
  547.         }
  548.         try{
  549.             $key $this->repoConf->getValue('TPV_KEY', ['type' => 'string''default' => 'sq7HjrUOBfKmC576ILgskD5srU870gJ7']);
  550.             $redsys = new Tpv();
  551.             $redsys->setAmount($entity->getPedPrecioTotal());
  552.             $redsys->setOrder(time());
  553.             $redsys->setMerchantcode($this->repoConf->getValue('TPV_MERCHANT_CODE', ['type' => 'string''default' => '356860510']));
  554.             $redsys->setCurrency($this->repoConf->getValue('TPV_MERCHANT_CURRENCY', ['type' => 'string''default' => '978']));
  555.             $redsys->setTransactiontype($this->repoConf->getValue('TPV_MERCHANT_TRANSACTION', ['type' => 'string''default' => '0']));
  556.             $redsys->setTerminal($this->repoConf->getValue('TPV_MERCHANT_TERMINAL', ['type' => 'string''default' => '001']));
  557.             $redsys->setMethod('C');
  558.             $redsys->setNotification($this->generateUrl('usu_pedidos_verificar_tpv', array(), UrlGeneratorInterface::ABSOLUTE_URL)); //Url de notificacion
  559.             $redsys->setUrlOk($this->generateUrl('usu_pedidos_verificar_tpv', array(), UrlGeneratorInterface::ABSOLUTE_URL)); //Url OK
  560.             $redsys->setUrlKo($this->generateUrl('usu_pedidos_verificar_tpv', array(), UrlGeneratorInterface::ABSOLUTE_URL)); //Url KO
  561.             $redsys->setVersion('HMAC_SHA256_V1');
  562.             $redsys->setTradeName('European Grading S.L');
  563.             $redsys->setTitular('European Grading S.L');
  564.             $redsys->setProductDescription('Pedido nĀŗ:'.$entity->getId());
  565.             $redsys->setEnvironment('test'); //Entorno test
  566.             $signature $redsys->generateMerchantSignature($key);
  567.             $redsys->setMerchantSignature($signature);
  568.             $redsys->executeRedirection();
  569.         } catch (\Sermepa\Tpv\TpvException $e) {
  570.             $this->hlp->error($e);
  571.         }
  572.     }
  573.      /**
  574.      * @Route("/paso/verificar_tpv", name = "verificar_tpv", methods = {"GET", "POST"})
  575.      * @IsGranted("ROLE_PEDIDO_CREATE")
  576.      */
  577.     public function verificarTpv(Request $request): Response
  578.     {
  579.         $entity $this->repo->findPedidoCarrito();
  580.         if($entity == null){
  581.             return $this->redirectToRoute('usu_pedidos_new');
  582.         }
  583.         if(count($entity->getPedGradeadasLineas()) == && count($entity->getPedProductoLineas()) == 0){
  584.             return $this->redirectToRoute('usu_pedidos_finish');
  585.         }
  586.         if($entity->getUsuAddress() == null || ($entity->getPedPayMethod() == null && $this->sec->isGranted('ROLE_PEDIDOS_CREATE_PROFESIONAL') == false) || $entity->getPedEnvioMetodo() == null ){
  587.             $this->addFlash('danger'$this->trans->trans('pedido.new.error3', [], 'pedido'));
  588.             return $this->redirectToRoute('usu_pedidos_paso2');
  589.         }
  590.         if($entity->getPedVerificado() == false){
  591.             $this->addFlash('danger'$this->trans->trans('pedido.new.error3', [], 'pedido'));
  592.             return $this->redirectToRoute('usu_pedidos_paso3');
  593.         }
  594.         try{
  595.             $redsys = new Tpv();
  596.             $key $this->repoConf->getValue('TPV_KEY', ['type' => 'string''default' => 'sq7HjrUOBfKmC576ILgskD5srU870gJ7']);
  597.         
  598.             $parameters $redsys->getMerchantParameters($request->query->get('Ds_MerchantParameters'));
  599.             $DsResponse $parameters["Ds_Response"];
  600.             $DsResponse += 0;
  601.             if ($redsys->check($key$request->query->all()) && $DsResponse <= 99) {
  602.                 //acciones a realizar si es correcto, por ejemplo validar una reserva, mandar un mail de OK, guardar en bbdd o contactar con mensajerĆ­a para preparar un pedido
  603.                 $entity->setPedFechaPago(new \DateTime());
  604.                 $this->em->flush();
  605.                 return $this->redirectToRoute('usu_pedidos_paso5');
  606.             } else {
  607.                 //acciones a realizar si ha sido erroneo
  608.                 $this->addFlash('danger'$this->trans->trans('pedido.new.error4', [], 'pedido'));
  609.                 return $this->redirectToRoute('usu_pedidos_paso4');
  610.             }
  611.         } catch (\Sermepa\Tpv\TpvException $e) {
  612.             $this->hlp->error($e);
  613.             $this->addFlash('danger'$this->trans->trans('pedido.new.error4', [], 'pedido'));
  614.             if ($this->isGranted('ROLE_SYSTEM_INFO')) {
  615.                 $this->addFlash('danger'$e->getMessage());
  616.             }
  617.         }
  618.     }
  619.         /**
  620.      * @Route("/paso/pago_paypal", name = "pago_paypal", methods = {"GET", "POST"})
  621.      * @IsGranted("ROLE_PEDIDO_CREATE")
  622.      */
  623.     public function pagoPaypal(Request $request): Response
  624.     {
  625.         $entity $this->repo->findPedidoCarrito();
  626.         if($entity == null){
  627.             return $this->redirectToRoute('usu_pedidos_new');
  628.         }
  629.         if(count($entity->getPedGradeadasLineas()) == && count($entity->getPedProductoLineas()) == 0){
  630.             return $this->redirectToRoute('usu_pedidos_finish');
  631.         }
  632.         if($entity->getUsuAddress() == null || ($entity->getPedPayMethod() == null && $this->sec->isGranted('ROLE_PEDIDOS_CREATE_PROFESIONAL') == false) || $entity->getPedEnvioMetodo() == null ){
  633.             $this->addFlash('danger'$this->trans->trans('pedido.new.error3', [], 'pedido'));
  634.             return $this->redirectToRoute('usu_pedidos_paso2');
  635.         }
  636.         if($entity->getPedVerificado() == false){
  637.             $this->addFlash('danger'$this->trans->trans('pedido.new.error3', [], 'pedido'));
  638.             return $this->redirectToRoute('usu_pedidos_paso3');
  639.         }
  640.         try{
  641.             //Inicializar datos
  642.             $user $this->repoConf->getValue('PAYPAL_USER', ['type' => 'string''default' => 'info_api1.europeangrading.com']);
  643.             $pass $this->repoConf->getValue('PAYPAL_PASS', ['type' => 'string''default' => '8ZWFV777J3XR5CK7']);
  644.             $signature $this->repoConf->getValue('PAYPAL_SIGNATURE', ['type' => 'string''default' => 'ABtIJdd337osVy9aV9FPXbFKhbaDAuT1JQwgQT3JV9R4tU0BEUiQxkeL']);
  645.             $version $this->repoConf->getValue('PAYPAL_VERSION', ['type' => 'string''default' => '95.0']);
  646.             $method $this->repoConf->getValue('PAYPAL_METHOD', ['type' => 'string''default' => 'SetExpressCheckout']);
  647.             $urlAPI $this->repoConf->getValue('PAYPAL_API_URL', ['type' => 'string''default' => 'https://api-3t.paypal.com/nvp']);
  648.             $venta "Sale";
  649.             $total $entity->getPedPrecioTotal();
  650.             $currency "EUR";
  651.             $orderName 'Pedido nĀŗ:'.$entity->getId();
  652.             $orderNumber time();
  653.             $qty 1;
  654.             $returnURL $cancelURL $this->generateUrl('usu_pedidos_verificar_paypal', array(), UrlGeneratorInterface::ABSOLUTE_URL);
  655.             $clientEmail $entity->getUsuEmail();
  656.             //Crear string para la peticion
  657.             $datosString 
  658.                 "USER=$user&PWD=$pass&SIGNATURE=$signature&VERSION=$version&PAYMENTREQUEST_0_PAYMENTACTION=$venta&L_PAYMENTREQUEST_0_NAME0=$orderName&"
  659.                 "L_PAYMENTREQUEST_0_NUMBER0=$orderNumber&L_PAYMENTREQUEST_0_DESC0=$orderName&L_PAYMENTREQUEST_0_AMT0=$total&"
  660.                 "L_PAYMENTREQUEST_0_QTY0=$qty&PAYMENTREQUEST_0_ITEMAMT=$total&PAYMENTREQUEST_0_AMT=$total&"
  661.                 "PAYMENTREQUEST_0_CURRENCYCODE=$currency&RETURNURL=$returnURL&CANCELURL=$cancelURL&EMAIL=$clientEmail&METHOD=$method"
  662.             ;
  663.             $datos explode("&"$datosString);
  664.             //Peticion curl
  665.             $curl curl_init();
  666.             curl_setopt($curlCURLOPT_URL$urlAPI);
  667.             curl_setopt($curlCURLOPT_POSTcount($datos));
  668.             curl_setopt($curlCURLOPT_POSTFIELDS$datosString);
  669.             curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  670.             $remote_server_output curl_exec ($curl);
  671.             $remote_server_output urldecode($remote_server_output);
  672.             curl_close ($curl);
  673.             if($remote_server_output != ""){
  674.                 parse_str($remote_server_output$result);
  675.                 if(isset($result['ACK'])){
  676.                     if(strtolower($result['ACK']) == "success"){
  677.                         $token urldecode($result['TOKEN']);
  678.                         if($token != null){
  679.                             header("Location: https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=" $token );
  680.                             exit();
  681.                         }else{
  682.                             $this->addFlash('danger'$this->trans->trans('pedido.new.error4', [], 'pedido'));
  683.                             return $this->redirectToRoute('usu_pedidos_paso4');
  684.                         }
  685.                     }else{
  686.                         if ($this->isGranted('ROLE_SYSTEM_INFO')) {
  687.                             $this->addFlash('danger'$result);
  688.                         }
  689.                         $this->addFlash('danger'$this->trans->trans('pedido.new.error4', [], 'pedido'));
  690.                         return $this->redirectToRoute('usu_pedidos_paso4');
  691.                     }
  692.                 }
  693.             }else{
  694.                 $this->addFlash('danger'$this->trans->trans('pedido.new.error4', [], 'pedido'));
  695.                 return $this->redirectToRoute('usu_pedidos_paso4');
  696.             }
  697.           
  698.         } catch (Exception $e) {
  699.             $this->hlp->error($e);
  700.             $this->addFlash('danger'$this->trans->trans('pedido.new.error4', [], 'pedido'));
  701.             if ($this->isGranted('ROLE_SYSTEM_INFO')) {
  702.                 $this->addFlash('danger'$e->getMessage());
  703.             }
  704.             return $this->redirectToRoute('usu_pedidos_paso4');
  705.         }
  706.         $this->addFlash('danger'$this->trans->trans('pedido.new.error4', [], 'pedido'));
  707.         return $this->redirectToRoute('usu_pedidos_paso4');
  708.     }
  709.          /**
  710.      * @Route("/paso/verificar_paypal", name = "verificar_paypal", methods = {"GET", "POST"})
  711.      * @IsGranted("ROLE_PEDIDO_CREATE")
  712.      */
  713.     public function verificarPaypal(Request $request): Response
  714.     {
  715.         $entity $this->repo->findPedidoCarrito();
  716.         if($entity == null){
  717.             return $this->redirectToRoute('usu_pedidos_new');
  718.         }
  719.         if(count($entity->getPedGradeadasLineas()) == && count($entity->getPedProductoLineas()) == 0){
  720.             return $this->redirectToRoute('usu_pedidos_finish');
  721.         }
  722.         if($entity->getUsuAddress() == null || ($entity->getPedPayMethod() == null && $this->sec->isGranted('ROLE_PEDIDOS_CREATE_PROFESIONAL') == false) || $entity->getPedEnvioMetodo() == null ){
  723.             $this->addFlash('danger'$this->trans->trans('pedido.new.error3', [], 'pedido'));
  724.             return $this->redirectToRoute('usu_pedidos_paso2');
  725.         }
  726.         if($entity->getPedVerificado() == false){
  727.             $this->addFlash('danger'$this->trans->trans('pedido.new.error3', [], 'pedido'));
  728.             return $this->redirectToRoute('usu_pedidos_paso3');
  729.         }
  730.         try{
  731.             $token $request->query->get('token');
  732.             $payerID $request->query->get('PayerID');
  733.             if(null != $token && null != $payerID){
  734.                 //PAGO CORRECTO
  735.                 $entity->setPedFechaPago(new \DateTime());
  736.                 $this->em->flush();
  737.                 return $this->redirectToRoute('usu_pedidos_paso5');
  738.             }else{
  739.                 //Pago cancelado
  740.                 $this->addFlash('danger'$this->trans->trans('pedido.new.error4', [], 'pedido'));
  741.                 return $this->redirectToRoute('usu_pedidos_paso4');
  742.             }
  743.         } catch (Exception $e) {
  744.             $this->hlp->error($e);
  745.             $this->addFlash('danger'$this->trans->trans('pedido.new.error4', [], 'pedido'));
  746.             if ($this->isGranted('ROLE_SYSTEM_INFO')) {
  747.                 $this->addFlash('danger'$e->getMessage());
  748.             }
  749.         }
  750.     }
  751.     /**
  752.      * @Route("/{id}/ver", name="view", methods = {"GET"}, requirements = {"id"="\d+"})
  753.      * @IsGranted("ROLE_PEDIDO_VIEW")
  754.      */
  755.     public function view(Pedido $entity): Response
  756.     {
  757.         if($entity->getPedUsuario() != $this->getUser()){
  758.             throw $this->createAccessDeniedException();
  759.         }
  760.         if($this->sec->isGranted('ROLE_PEDIDOS_CREATE_PROFESIONAL')){
  761.             $profesional true;
  762.         }else{
  763.             $profesional false;
  764.         }
  765.         //SEO
  766.         $titulo =$this->trans->trans('pedido.usuario.view.seo.title', [], 'pedido');
  767.         $descripcion =$this->trans->trans('pedido.usuario.view.seo.desc', [], 'pedido');
  768.         $this->seo 
  769.             ->setTitle($titulo)
  770.             ->addMeta('name''description'$descripcion)
  771.             ->addMeta('property''og:title'$titulo)
  772.             ->addMeta('property''og:description'$descripcion)
  773.         ;
  774.         if($profesional){
  775.             //cargar vista profesional, masa diferencies per a fer-ho tot en una                 
  776.             return $this->render(self::BASEDIR.'/ped_view_pro.html.twig', [
  777.                 'pedido' => $entity,
  778.                 'BaseDir' => self::BASEDIR,
  779.                 'ICON' => self::ICON,
  780.             ]);
  781.         }else{      
  782.             return $this->render(self::BASEDIR.'/ped_view.html.twig', [
  783.                 'pedido' => $entity,
  784.                 'BaseDir' => self::BASEDIR,
  785.                 'ICON' => self::ICON,
  786.             ]);
  787.         }
  788.     }
  789.     public function enviarEmailPedido(Pedido $entity)
  790.     {
  791.         $emails $this->sec->getUser()->getEmail();
  792.         $emails explode(','$emails);
  793.         $params = [
  794.             'asunto' => $this->trans->trans('pedido.usuario.email.asunto', [], 'pedido'),
  795.             'to' => $emails,
  796.             'plantilla' => '/emails/email_pedido.html.twig',
  797.             'plantilla_params' => [
  798.                 'pedido' => $entity,
  799.                 'raw' => true,
  800.             ],
  801.             'importance' => null,
  802.             'adjuntos_base_pdf' => [
  803.                 [
  804.                     'nombre' => 'pedido_albaran.pdf',
  805.                     'mime' => 'application/pdf',
  806.                     'base_64' => $this->pdf->generateAlbaran($entitytrue),
  807.                 ],
  808.             ],
  809.         ];
  810.         if ($this->SendMail->envioBasico($params)) {
  811.             return true;
  812.         }
  813.         return false;
  814.     }
  815.     public function enviarEmailPedidoPro(Pedido $entity)
  816.     {
  817.         $emails $this->sec->getUser()->getEmail();
  818.         $emails explode(','$emails);
  819.         $params = [
  820.             'asunto' => $this->trans->trans('pedido.usuario.email.asunto', [], 'pedido'),
  821.             'to' => $emails,
  822.             'plantilla' => '/emails/email_pedido_pro.html.twig',
  823.             'plantilla_params' => [
  824.                 'pedido' => $entity,
  825.                 'raw' => true,
  826.             ],
  827.             'importance' => null,
  828.             'adjuntos_base_pdf' => [
  829.                 [
  830.                     'nombre' => 'pedido_albaran.pdf',
  831.                     'mime' => 'application/pdf',
  832.                     'base_64' => $this->pdf->generateAlbaran($entitytrue),
  833.                 ],
  834.             ],
  835.         ];
  836.         if ($this->SendMail->envioBasico($params)) {
  837.             return true;
  838.         }
  839.         return false;
  840.     }
  841.     /**
  842.      * @Route("/{id}/pdf_albaran", name="albaran", methods={"GET"}, requirements = {"id" = "\d+"})
  843.      * @IsGranted("ROLE_PEDIDOS_CREATE_ALBARAN_CLIENTE")
  844.      */
  845.     public function callAlbaran(Pedido $entity)
  846.     {
  847.         if($entity->getPedUsuario() != $this->getUser()){
  848.             throw $this->createAccessDeniedException();
  849.         }
  850.        return $this->pdf->generateAlbaran($entity);
  851.     }
  852.     /**
  853.      * @Route("/{id}/pdf_factura", name="factura", methods={"GET"}, requirements = {"id" = "\d+"})
  854.      * @IsGranted("ROLE_PEDIDOS_CREATE_FACTURA_CLIENTE")
  855.      */
  856.     public function callFactura(Pedido $entity)
  857.     {
  858.         if($entity->getPedUsuario() != $this->getUser()){
  859.             throw $this->createAccessDeniedException();
  860.         }
  861.        return $this->pdf->pdfFactura($entity);
  862.     }
  863. }