src/Controller/ComDestinoNotificacionBienvenidaController.php line 155

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\ComDestinoNotificacionBienvenida;
  4. use App\Entity\ComGestionContactoComercial;
  5. use App\Entity\ComInformacionContacto;
  6. use App\Entity\ComOfertaComercial;
  7. use App\Entity\ComResultadoNegociacion;
  8. use App\Entity\TerEmpresaCliente;
  9. use App\Entity\TerPersona;
  10. use App\Form\ComDestinoNotificacionBienvenidaType;
  11. use App\Repository\ComDestinoNotificacionBienvenidaRepository;
  12. use App\Repository\ComGestionContactoComercialRepository;
  13. use App\Services\DocumentHandler;
  14. use App\Services\MailerCore;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use Doctrine\ORM\EntityRepository;
  17. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  18. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  19. use Symfony\Component\HttpFoundation\JsonResponse;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Symfony\Component\Mailer\MailerInterface;
  23. use Symfony\Component\Routing\Annotation\Route;
  24. #[Route('/com/destino/notificacion/bienvenida')]
  25. class ComDestinoNotificacionBienvenidaController extends AbstractController
  26. {
  27.     private $repository;
  28.     private $code 0;
  29.     private $msj "";
  30.     private $url "";
  31.     private $html "";
  32.     private $mailerCore;
  33.     /**
  34.      * @var MailerInterface
  35.      */
  36.     private $mailer;
  37.     private $documentHandler;
  38.     public function __construct(EntityManagerInterface $entityManagerComDestinoNotificacionBienvenidaRepository $comDestinoNotificacionBienvenidaRepositoryMailerCore $mailerCoreMailerInterface $mailer,
  39.                                 DocumentHandler  $documentHandler) {
  40.         $this->entityManager $entityManager;
  41.         $this->repository $comDestinoNotificacionBienvenidaRepository;
  42.         $this->mailerCore $mailerCore;
  43.         $this->documentHandler $documentHandler;
  44.         $this->mailer $mailer;
  45.     }
  46.     #[Route('/'name'app_com_destino_notificacion_bienvenida_index'methods: ['GET'])]
  47.     public function index(Request $request): Response
  48.     {
  49.         $idProspecto $request->get('id'); // ID prospecto
  50.         $notificacionesBienvenida $this->repository->getAll($idProspecto);
  51.         return $this->render('com_destino_notificacion_bienvenida/index.html.twig', [
  52.             'entity' => $notificacionesBienvenida,
  53.             'id' => $idProspecto
  54.         ]);
  55.     }
  56.     #[Route('/new'name'app_com_destino_notificacion_bienvenida_new'methods: ['GET''POST'])]
  57.     public function new(Request $requestEntityManagerInterface $entityManager): Response
  58.     {
  59.         $idProspecto $request->get('id'); // ID prospecto
  60.         $terEmpresaCliente $entityManager->getRepository(TerEmpresaCliente::class)->find($idProspecto);
  61.         // Formulario
  62.         $comDestinoBienvenida = new ComDestinoNotificacionBienvenida();
  63.         $form $this->createForm(ComDestinoNotificacionBienvenidaType::class, $comDestinoBienvenida,[
  64.             'method' => 'POST',
  65.             'action'=> $this->generateUrl('app_com_destino_notificacion_bienvenida_new',[
  66.                 'id'=>$idProspecto,
  67.             ]),
  68.             'idCliente'=>$idProspecto
  69.         ]);
  70.         $form->handleRequest($request);
  71.         if ($form->isSubmitted() && $form->isValid()) {
  72.             // Persistir los datos
  73.             $comDestinoBienvenida->setTerEmpresaCliente($terEmpresaCliente);
  74.             $entityManager->persist($comDestinoBienvenida);
  75.             $entityManager->flush();
  76.             $this->code 'success';
  77.             $this->msj "Se ha registrado la notificacion de bienvenida";
  78.             return new JsonResponse(['code' => $this->code'msj' => $this->msj,'url' => $this->url,'html' => $this->html]);
  79.         }
  80.         return $this->renderForm('com_destino_notificacion_bienvenida/new.html.twig', [
  81.             'prospecto' => $terEmpresaCliente,
  82.             'consecutivos'=>$this->consecutivosAprobados($idProspecto,$entityManager),
  83.             'form' => $form,
  84.         ]);
  85.     }
  86.     // Funcion para editar la notificacion
  87.     #[Route('/edit'name'app_com_destino_notificacion_bienvenida_edit'methods: ['GET''POST'])]
  88.     public function edit(Request $requestEntityManagerInterface $entityManager): Response{
  89.         $idNotificacion $request->get('id'); // ID notificacion
  90.         $notificaciones $entityManager->getRepository(ComDestinoNotificacionBienvenida::class)->find($idNotificacion);
  91.         $form $this->createForm(ComDestinoNotificacionBienvenidaType::class, $notificaciones,[
  92.             'method' => 'POST',
  93.             'action' => $this->generateUrl('app_com_destino_notificacion_bienvenida_edit',[
  94.                 'id'=>$idNotificacion,
  95.             ]),
  96.             'idCliente'=>$notificaciones->getTerEmpresaCliente()->getId()
  97.         ]);
  98.         $form->handleRequest($request);
  99.         if ($form->isSubmitted() && $form->isValid()) {
  100.             // Guardar datos de actualizacion
  101.             $entityManager->persist($notificaciones);
  102.             $entityManager->flush();
  103.             $this->code 'success';
  104.             $this->msj "Se ha actualizado el registro exitosamente";
  105.             return new JsonResponse(['code' => $this->code'msj' => $this->msj,
  106.                 'url' => $this->url,'html' => $this->html]);
  107.         }
  108.         return $this->renderForm('com_destino_notificacion_bienvenida/edit.html.twig', [
  109.             'prospecto' => $notificaciones->getTerEmpresaCliente(),
  110.             'consecutivos'=>$this->consecutivosAprobados($notificaciones->getTerEmpresaCliente()->getId(),$entityManager),
  111.             'form' => $form,
  112.         ]);
  113.     }
  114.     // Funcion de ver
  115.     #[Route('/show'name'app_com_destino_notificacion_bienvenida_show'methods: ['GET'])]
  116.     public function show(Request $requestEntityManagerInterface $entityManager): Response
  117.     {
  118.         $idNotificacion $request->get('id');
  119.         $notificaciones $entityManager->getRepository(ComDestinoNotificacionBienvenida::class)->find($idNotificacion);
  120.         $ofertasAprobadas $this->consecutivosAprobados($notificaciones->getTerEmpresaCliente()->getId(), $entityManager);
  121.         return $this->renderForm('com_destino_notificacion_bienvenida/show.html.twig', [
  122.             'notificaciones' => $notificaciones,
  123.             'consecutivos' => $ofertasAprobadas,
  124.         ]);
  125.     }
  126.     // Funcion para consecutivos APROBADOS
  127.     public function consecutivosAprobados($idProspecto nullEntityManagerInterface $entityManager){
  128.         $query =  $entityManager->getRepository(ComResultadoNegociacion::class)->createQueryBuilder('cc')
  129.             ->leftJoin('cc.comOfertaComercial','ofc')
  130.             ->leftJoin('ofc.comProspectoCliente','cpc')
  131.             ->leftJoin('cc.resultadoNegociacion','rne')
  132.             ->where('cpc.id = :idProspecto')
  133.             ->andWhere('rne.id = :estado')
  134.             // ID prospecto
  135.             ->setParameter('idProspecto'$idProspecto)
  136.             ->setParameter('estado'5// Estado APROBADO
  137.             ->orderBy('cc.id''DESC');
  138.         return $query->getQuery()->getResult();
  139.     }
  140.     // Consultar los datos por CONSECUTIVO
  141.     #[Route('/consultar_consecutivo'name'app_com_destino_notificacion_bienvenida_consultar_consecutivo'methods: ['GET''POST'])]
  142.     public function consultarConsecutivo(Request $requestEntityManagerInterface $entityManager): Response{
  143.         $idResultado $request->get('id');
  144.         $comResultado $entityManager->getRepository(ComResultadoNegociacion::class)->find($idResultado);
  145.         $comComercialConsecutivo $comResultado->getComOfertaComercial();
  146.         if(!$comComercialConsecutivo){
  147.             return new JsonResponse([
  148.                 'code'=>'error',
  149.             ]);
  150.         }
  151.         return new JsonResponse([
  152.             'code'=>'success',
  153.             'data'=>[
  154.                 'resultadoNegocio'=>$comResultado->getResultadoNegociacion()?->getNombre() ?? 'N/A',
  155.                 'fechaResultado'=>$comResultado->getFechaResultado()->format('Y-m-d'),
  156.                 'vigenciaOferta'=>$comComercialConsecutivo->getVigenciaOferta()->format('Y-m-d'),
  157.                 'ofPdf'=>$comComercialConsecutivo->getCargarOfertaPdf(),
  158.                 'ofExcel'=>$comComercialConsecutivo->getOfertaExcel(),
  159.                 'empresa'=>$comComercialConsecutivo->getTerEmpresa()->getNombre(),
  160.                 'tipoEmpresa'=>$comComercialConsecutivo->getTerEmpresa()->getAbreviatura()
  161.             ]
  162.         ]);
  163.     }
  164.     // Funcion para enviar notificación a los seleccionados por contactos
  165.     #[Route('/envio_notificacion'name'app_com_destino_notificacion_bienvenida_envio_notificacion'methods: ['GET''POST'])]
  166.     public function envioContactos(Request $requestEntityManagerInterface $entityManager): Response{
  167.         $idProspecto $request->get('idProspecto');
  168.         $contactosIds $request->get('contactos'); // Recibir los contactos
  169.         $correosContactos = [];
  170.         $repository $entityManager->getRepository(ComInformacionContacto::class)->findBy([
  171.             'id'=>$contactosIds
  172.         ]);
  173.         // Validar que no encontro resultados
  174.         if(!$repository){
  175.             return new JsonResponse([
  176.                 'code'=>'error',
  177.             ]);
  178.         }
  179.         // Llenar la lista de correos seleccionados
  180.         foreach ($repository as $correo){
  181.             $correosContactos[] = $correo->getCorreo();
  182.         }
  183.         // Agregar correo de ejecutiva comercial
  184.         $prospecto $entityManager->getRepository(TerEmpresaCliente::class)->find($idProspecto);
  185.         $correoEjecutiva $entityManager->getRepository(TerPersona::class)->findBy([
  186.             'nombres'=>$prospecto->getEjecutivaComercialComisora()
  187.         ]);
  188.         // Preguntar por las ofertas comerciales que estan APROBADAS
  189.         $ofertasAprobadas $this->consecutivosAprobados($prospecto->getId(),$entityManager);
  190.         $correoComisiona = [
  191.             'correoEjecutiva' => $correoEjecutiva[0]->getEmail() ?? 'N/A',
  192.             'telefono' => $prospecto->getTelefono() ?? 'N/A',
  193.             'nombre' => $prospecto->getEjecutivaComercialComisora() ?? 'N/A',
  194.             'ofertasAprobadas' => $ofertasAprobadas,
  195.         ];
  196.         $correosContactos[] = $correoComisiona['correoEjecutiva'];
  197.         // Enviar correo I14 H1
  198.         $mensajes $this->mailerCore->notificacionBievenidaCliente('bienvenidaCliente',[$prospecto],$correosContactos,$correoComisiona);
  199.         return new JsonResponse([
  200.             'code'=>'success',
  201.             'data'=>[
  202.                 'contactos'=>$correosContactos,
  203.                 'email'=>$correosContactos,
  204.                 'mensajes'=>$mensajes,
  205.             ]
  206.         ]);
  207.     }
  208. }