src/Application/Internit/LeadBundle/Controller/ProductFrontController.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\Application\Internit\LeadBundle\Controller;
  3. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  9. use App\Application\Internit\LeadBundle\Entity\Lead;
  10. use App\Application\Internit\LeadBundle\Entity\ChatbotLead;
  11. use Symfony\Component\HttpKernel\Exception\HttpException;
  12. /**
  13.  * Product Front controller.
  14.  *
  15.  * @Route("/api/leads")
  16.  */
  17. class ProductFrontController extends Controller
  18. {
  19.     public function getBundleName()
  20.     {
  21.         return 'ApplicationInternitLeadBundle';
  22.     }
  23.     public function getEntityName()
  24.     {
  25.         return 'Product';
  26.     }
  27.     public function getFormType()
  28.     {
  29.         return 'App\Application\Internit\LeadBundle\Form\ProductType';
  30.     }
  31.     /**
  32.      * @Route("/send", name="front_product_lead_send")
  33.      * @Method("POST")
  34.      */
  35.     public function send(Request $request)
  36.     {
  37.         $data json_decode($request->getContent());
  38.         $statusCode=400;
  39.         $result=[];
  40.         if(!$data)
  41.             return $this->sendStatus(400,"New Lead is not valid");
  42.         if(isset($data->token)){
  43.             $product $this->getDoctrine()->getRepository('ApplicationInternitLeadBundle:Product')->findOneByToken($data->token);
  44.             if(!$product)
  45.                 return $this->sendStatus(404,"Product not found");
  46.             if(!empty($data->name) &&
  47.                 !empty($data->origem) &&
  48.                 !empty($data->group)
  49.             ){
  50.                 // form de whatsapp não traz e-mail
  51.                 $emailLead = isset($data->email) ? $data->email '';
  52.                 $extraInfo = array();
  53.                 $to $this->get('tupi.caster.service')->next($product$emailLead);
  54.                 //dd($to);
  55.                 // dump($to);exit;
  56.                 $newLead = new Lead();
  57.                 //Obrigatórios
  58.                 $newLead->setName($data->name);
  59.                 $newLead->setGroup($data->group);
  60.                 $newLead->setOrigem($data->origem);
  61.                 $newLead->setProduct($product);
  62.                 $newLead->setEmail($emailLead);
  63.                 $newLead->setPhone(isset($data->phone) ? $data->phone '');
  64.                 //Opcionais
  65.                 if(isset($data->fixedPhone)) {
  66.                     $newLead->setFixedPhone(($data->fixedPhone $data->fixedPhone ''));
  67.                 }
  68.                 if(isset($data->empreendimento))
  69.                     $newLead->setEmpreendimento(($data->empreendimento $data->empreendimento ''));
  70.                 if(isset($data->utm_source))
  71.                     $newLead->setUtm_source(($data->utm_source $data->utm_source ''));
  72.                 if(isset($data->utm_medium))
  73.                     $newLead->setUtm_medium(($data->utm_medium $data->utm_medium ''));
  74.                 if(isset($data->utm_campaign))
  75.                     $newLead->setUtm_campaign(($data->utm_campaign $data->utm_campaign ''));
  76.                 if(isset($data->message))
  77.                     $newLead->setMessage(($data->message $data->message ''));
  78.                 // e-mail que a construtora configura para receber os leads deste produto.
  79.                 $productEmail $product->getEmail();
  80.                 if(!empty($productEmail) || !is_null($to)){
  81.                     // obter e-mails de lead da imobiliária da vez
  82.                     $realEstateLeadEmails $this->getDoctrine()
  83.                         ->getRepository('ApplicationInternitLeadBundle:RealEstateEmail')
  84.                         ->findRealEstateLeadEmails($to['imob']);
  85.                     $realEstate $this->getDoctrine()
  86.                         ->getRepository('ApplicationInternitRealEstateBundle:RealEstate')
  87.                         ->findOneBy(['name' => $to['imob']]);
  88.                     //Chamar service de disparo para corretor
  89.                     $email1 $this->get('admin.mail.service');
  90.                     //$email2 = $this->get('admin.mail.service');
  91.                     // EMAILS COPY
  92.                     $emailCopySend = [];
  93.                     if($realEstate){
  94.                         if($realEstate->getReceiveLead()){
  95.                             $emailCopySend[] = $realEstate->getEmail();
  96.                             //$email->setMessage()->setBCC($realEstate->getEmail());
  97.                         }
  98.                     }
  99.                     if ( ! empty($productEmail) ) {
  100.                         $emailsSend explode(','str_replace(' '''$productEmail));
  101.                         foreach ($emailsSend as $sendMail){
  102.                             $emailCopySend[] = $sendMail;
  103.                             //$email->setMessage()->setBCC($sendMail);
  104.                         }
  105.                     }
  106.                     // se houver o e-mail do corretor, ele é o destinatário principal e
  107.                     // o e-mail padrão da campanha é cópia oculta
  108.                     if ( !empty($to['email']) ) {
  109.                         $email1->setMessage()->setTo($to['email']);
  110.                         //dd($realEstate);
  111.                         if(!empty($emailCopySend)){
  112.                             $email1->setMessage()->setCC($emailCopySend);
  113.                         }
  114.                     }
  115.                     // se não houver nenhum corretor para receber e-mail, o e-mail padrão
  116.                     // da campanha é o destinatário principal
  117.                     else if (! empty($productEmail)) {
  118.                         $emailsSend explode(','str_replace(' '''$productEmail));
  119.                         foreach ($emailsSend as $sendMail){
  120.                             $emailCopySend[] = $sendMail;
  121.                             //$email->setMessage()->setBCC($sendMail);
  122.                         }
  123.                         $email1->setMessage()->setTo($emailsSend);
  124.                         if(!empty($emailCopySend)){
  125.                             $email1->setMessage()->setCC($emailCopySend);
  126.                         }
  127.                     }
  128. //                        if (!empty($realEstateLeadEmails)) {
  129. //                            $email->setMessage()->setCC($realEstateLeadEmails);
  130. //                        }
  131.                     $newLead->setBrokerEmail( ! empty($to['email']) ? $to['email'] : $productEmail ); //Seta email do corretor que recebeu o lead
  132.                     $newLead->setBrokerRealState($to['imob']); //Seta Imobiliaria do corretor que recebeu o lead
  133.                     $extraInfo['broker_name'] = $to['name'];
  134.                     //if($to['phone'])
  135.                     //    $extraInfo['broker_phone'] = preg_replace('/[^0-9]/', '', $to['phone']);
  136.                     $extraInfo['realstate_name'] = $to['imob']; //strtoupper()
  137.                     $entityManager $this->getDoctrine()->getManager();
  138.                     $entityManager->persist($newLead);
  139.                     $entityManager->flush();
  140.                     $email1
  141.                         ->setMessage()
  142.                         ->setSubject("[leadcalper] ".$product->getName() . ' - ' $newLead->getGroup() . ' - ' $to['name'])
  143.                         ->setBody($this->renderView("@ApplicationInternit/SettingBundle/Resources/views/Mail/emailLead.html.twig", array(
  144.                             'data' => $newLead,
  145.                             'corretor' => $to['name'],
  146.                         )), 'text/html');
  147.                     $email1->send();
  148.                  /*   $email2
  149.                         ->setMessage()
  150.                         ->setSubject("[Lead] ".$product->getName() . ' - ' . $newLead->getGroup())
  151.                         ->setBody($this->renderView("@ApplicationInternit/SettingBundle/Resources/views/Mail/emailLead.html.twig", array(
  152.                             'data' => $newLead,
  153.                         )), 'text/html');
  154.                     $email2->send();*/
  155.                 }
  156.                 // dump($newLead);
  157.                 return $this->sendStatus(200,"Lead created"$extraInfo);
  158.             }
  159.             else{
  160.                 return $this->sendStatus(400,"Invalid content");
  161.             }
  162.         }
  163.         return $this->sendStatus(400,"Token is required");
  164.     }
  165.     public function sendLast(Request $request)
  166.     {
  167.         
  168.         $data json_decode($request->getContent());
  169.         $statusCode=400;
  170.         $result=[];
  171.         if(!$data)
  172.             return $this->sendStatus(400,"New Lead is not valid");
  173.         if(isset($data->token)){
  174.             $product $this->getDoctrine()->getRepository('ApplicationInternitLeadBundle:Product')->findOneByToken($data->token);        
  175.             if(!$product)
  176.                 return $this->sendStatus(404,"Product not found");
  177.             
  178.             if(!empty($data->name) &&
  179.                 !empty($data->origem) &&
  180.                 !empty($data->group)
  181.                 ){
  182.                     // form de whatsapp não traz e-mail
  183.                     $emailLead = isset($data->email) ? $data->email '';
  184.                     $extraInfo = array();
  185.                     $to $this->get('tupi.caster.service')->next($product$emailLead);
  186.                     //dd($to);
  187.                     // dump($to);exit;
  188.                     $newLead = new Lead();
  189.                     //Obrigatórios
  190.                     $newLead->setName($data->name);
  191.                     $newLead->setGroup($data->group);
  192.                     $newLead->setOrigem($data->origem);
  193.                     $newLead->setProduct($product);
  194.                     $newLead->setEmail($emailLead);
  195.                     $newLead->setPhone(isset($data->phone) ? $data->phone '');
  196.                     
  197.                     //Opcionais
  198.                     if(isset($data->fixedPhone)) {
  199.                         $newLead->setFixedPhone(($data->fixedPhone $data->fixedPhone ''));
  200.                     }
  201.                     if(isset($data->empreendimento))
  202.                         $newLead->setEmpreendimento(($data->empreendimento $data->empreendimento ''));
  203.                     if(isset($data->utm_source))
  204.                         $newLead->setUtm_source(($data->utm_source $data->utm_source ''));
  205.                     if(isset($data->utm_medium))
  206.                         $newLead->setUtm_medium(($data->utm_medium $data->utm_medium ''));
  207.                     if(isset($data->utm_campaign))
  208.                         $newLead->setUtm_campaign(($data->utm_campaign $data->utm_campaign ''));
  209.                     if(isset($data->message))
  210.                         $newLead->setMessage(($data->message $data->message ''));
  211.                     // e-mail que a construtora configura para receber os leads deste produto.
  212.                     $productEmail $product->getEmail();
  213.                     if(!empty($productEmail) || !is_null($to)){
  214.                         // obter e-mails de lead da imobiliária da vez
  215.                         $realEstateLeadEmails $this->getDoctrine()
  216.                             ->getRepository('ApplicationInternitLeadBundle:RealEstateEmail')
  217.                             ->findRealEstateLeadEmails($to['imob']);
  218.                         $realEstate $this->getDoctrine()
  219.                             ->getRepository('ApplicationInternitRealEstateBundle:RealEstate')
  220.                             ->findOneBy(['name' => $to['imob']]);
  221.                         //Chamar service de disparo para corretor
  222.                         $email1 $this->get('admin.mail.service');
  223.                         $email2 $this->get('admin.mail.service');
  224.                         // EMAILS COPY
  225.                         $emailCopySend = [];
  226.                         if($realEstate){
  227.                             if($realEstate->getReceiveLead()){
  228.                                 $emailCopySend[] = $realEstate->getEmail();
  229.                                 //$email->setMessage()->setBCC($realEstate->getEmail());
  230.                             }
  231.                         }
  232.                         if ( ! empty($productEmail) ) {
  233.                             $emailsSend explode(','str_replace(' '''$productEmail));
  234.                             foreach ($emailsSend as $sendMail){
  235.                                 $emailCopySend[] = $sendMail;
  236.                                 //$email->setMessage()->setBCC($sendMail);
  237.                             }
  238.                         }
  239.                         // se houver o e-mail do corretor, ele é o destinatário principal e
  240.                         // o e-mail padrão da campanha é cópia oculta
  241.                         if ( !empty($to['email']) ) {
  242.                             
  243.                             $email1->setMessage()->setTo($to['email']);
  244.                             //dd($realEstate);
  245.                             if(!empty($emailCopySend)){
  246.                                 $email2->setMessage()->setTo($emailCopySend);
  247.                             }
  248.                         }
  249.                         // se não houver nenhum corretor para receber e-mail, o e-mail padrão
  250.                         // da campanha é o destinatário principal
  251.                         else if (! empty($productEmail)) {
  252.                             $emailsSend explode(','str_replace(' '''$productEmail));
  253.                             foreach ($emailsSend as $sendMail){
  254.                                 $emailCopySend[] = $sendMail;
  255.                                 //$email->setMessage()->setBCC($sendMail);
  256.                             }
  257.                             $email1->setMessage()->setTo($emailsSend);
  258.                             if(!empty($emailCopySend)){
  259.                                 $email2->setMessage()->setTo($emailCopySend);
  260.                             }
  261.                         }
  262. //                        if (!empty($realEstateLeadEmails)) {
  263. //                            $email->setMessage()->setCC($realEstateLeadEmails);
  264. //                        }
  265.                         $newLead->setBrokerEmail( ! empty($to['email']) ? $to['email'] : $productEmail ); //Seta email do corretor que recebeu o lead
  266.                         $newLead->setBrokerRealState($to['imob']); //Seta Imobiliaria do corretor que recebeu o lead
  267.                         $extraInfo['broker_name'] = $to['name'];
  268.                         if($to['phone'])
  269.                             $extraInfo['broker_phone'] = preg_replace('/[^0-9]/'''$to['phone']);
  270.                         $entityManager $this->getDoctrine()->getManager();
  271.                         $entityManager->persist($newLead);
  272.                         $entityManager->flush();
  273.                         $email1
  274.                             ->setMessage()
  275.                             ->setSubject("[Lead] ".$product->getName() . ' - ' $newLead->getGroup())
  276.                             ->setBody($this->renderView("@ApplicationInternit/SettingBundle/Resources/views/Mail/emailLead.html.twig", array(
  277.                                 'data' => $newLead,
  278.                             )), 'text/html');
  279.                         $email1->send();
  280.                         $email2
  281.                             ->setMessage()
  282.                             ->setSubject("[Lead] ".$product->getName() . ' - ' $newLead->getGroup())
  283.                             ->setBody($this->renderView("@ApplicationInternit/SettingBundle/Resources/views/Mail/emailLead.html.twig", array(
  284.                                 'data' => $newLead,
  285.                             )), 'text/html');
  286.                         $email2->send();
  287.                     }
  288.                     // dump($newLead);
  289.                     return $this->sendStatus(200,"Lead created"$extraInfo);
  290.                 }
  291.                 else{
  292.                     return $this->sendStatus(400,"Invalid content");
  293.                 }
  294.         }
  295.         return $this->sendStatus(400,"Token is required");
  296.     }
  297.     private function sendStatus($code 401$message "Requisição não autorizada",$extra = array()){
  298.         $array = array('message' => $message);
  299.         $array array_merge($array$extra);
  300.         $response = new Response(json_encode($array), $code);
  301.         $response->headers->set('Content-Type''application/json');
  302.         return $response;
  303.     }
  304. }