vendor/blh/blh-bundle/src/EventSubscriber/WorkflowLoggerSubscriber.php line 32

Open in your IDE?
  1. <?php
  2. namespace Blh\Bundle\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  5. use Symfony\Component\Workflow\Event\Event;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Blh\Bundle\Handler\MailHandler;
  8. use Blh\Bundle\Entity\Email;
  9. use Blh\Bundle\Entity\Employee;
  10. use Blh\Bundle\Repository\EmployeeRepository;
  11. use Blh\Bundle\Entity\Position;
  12. use Blh\Bundle\Entity\EntityContract;
  13. class WorkflowLoggerSubscriber implements EventSubscriberInterface
  14. {
  15.     private EntityManagerInterface $entityManager;
  16.     private MailHandler $mailHandler;
  17.     private ParameterBagInterface $parameters;    
  18.     public function __construct(
  19.         EntityManagerInterface $entityManager
  20.         MailHandler $mailHandler,
  21.         ParameterBagInterface $parameters
  22.     ){
  23.         $this->parameters $parameters;
  24.         $this->entityManager $entityManager;
  25.         $this->mailHandler $mailHandler;
  26.     }
  27.     public function onCompleted(Event $event): void
  28.     {
  29.         
  30.         $this->sendNotificationEmail($event);
  31.     }
  32.     public function onCompletedVacation(Event $event): void
  33.     {
  34.         $this->sendAbsenceNotificationEmail($event);
  35.     }    
  36.     private function sendNotificationEmail(Event $event): void
  37.     {
  38.         $emailToSend $this->entityManager->getRepository(Email::class)->findOneBy([
  39.             'name' => 'workflow-'.$event->getWorkflowName().'-'.$event->getSubject()->getWorkflowStep()
  40.         ]);
  41.         if(!isset($emailToSend)){
  42.             return;
  43.         }
  44.         $employee $event->getSubject()->getContract()->getEmployee();
  45.         $typeMotive $event->getSubject()->getprocessMotive()->getProcessType()->getId();
  46.         if(in_array($event->getSubject()->getContract()->getPosition()->getCode(), [Position::CODE_CMPosition::CODE_CC])){
  47.             $emailsForNotification = [ MailHandler::RECEIVER_TO => [ $employee->getImmediateBoss()->getCorporateEmail() ] ];
  48.         }else{
  49.             $emailsForNotification = [
  50.                 MailHandler::RECEIVER_TO => array_merge(
  51.                     $this->entityManager->getRepository(Employee::class)
  52.                         ->findEmployeePositionByEntity($event->getSubject()->getEntity(), Position::CODE_CM)
  53.                         ->map(fn($element) => $element->getContract()->getEmployee()->getCorporateEmail())
  54.                         ->toArray(),
  55.                     $this->entityManager->getRepository(Employee::class)
  56.                         ->findEmployeePositionByEntity($event->getSubject()->getEntity(), Position::CODE_CC)
  57.                         ->map(fn($element) => $element->getContract()->getEmployee()->getCorporateEmail())
  58.                         ->toArray()
  59.                 )
  60.             ];
  61.         }
  62.         // if($employee->getCorporateEmail() !== null) $emailsForNotification[MailHandler::RECEIVER_TO][] = $employee->getCorporateEmail();
  63.         $subject str_replace('{{employeeName}}'ucwords($employee->getFirstName().' '.$employee->getSurname()), $emailToSend->getSubject());
  64.         $dateProccessContract = ($typeMotive == )?$event->getSubject()->getContract()->getStartDate(): $event->getSubject()->getContract()->getEndDate();
  65.         $subject str_replace('{{date}}',$dateProccessContract->format('Y-m-d'), $subject );
  66.         $this->mailHandler->generateEmail(
  67.             //$emailToSend->setSubject(str_replace('{{employeeName}}', ucwords($employee->getFirstName().' '.$employee->getSurname()), $emailToSend->getSubject())), 
  68.             $emailToSend->setSubject($subject), 
  69.             $emailsForNotification
  70.             ['process' => $event->getSubject()]
  71.         );
  72.         $this->entityManager->detach($emailToSend);
  73.     }
  74.     public function sendAbsenceNotificationEmail($event)
  75.     {
  76.         
  77.         //Identificamos si la ausencia fue cancelada por RRHH para identificar
  78.         //Cual es el template que se utilizara
  79.         if (isset($event->getSubject()->cancelByHR)){
  80.             $templateName 'absence-canceled-ByHR';
  81.         }else{
  82.             $templateName 'absence-'.$event->getSubject()->getWorkflowStep();
  83.         }
  84.         
  85.         $emailToSend $this->entityManager->getRepository(Email::class)->findOneBy([
  86.             'name' => $templateName
  87.         ]);
  88.         if(!isset($emailToSend)){
  89.             return;
  90.         }        
  91.         $employee $event->getSubject()->getEmployee();
  92.         //dd("llego a esta opcion sendAbsenceNotificationEmail():", $emailToSend, $employee);
  93.         
  94.         //Este pedazo de codigo busca a los empelados que tendran el rol de contabilidad en el pais, de la persona a la que se
  95.         //le esta gestionando la ausencia.
  96.         $contract $this->entityManager->getRepository(Employee::class)->hasContractActive($event->getSubject()->getEmployee()->getId());
  97.         $entityContract $this->entityManager->getRepository(EntityContract::class)->findOneBy(['contract'=>$contract[0]->getId(), 'isPrincipal'=>1]);
  98.         $a3code $entityContract->getEntity()->getCountry()->getA3code();
  99.         $positionAccountant =$this->parameters->get('absence_position_notification_by_country')[$a3code];
  100.         $employeeAccountant $this->entityManager->getRepository(Employee::class)->getEmployeeByCountryAndPosition($entityContract->getEntity()->getCountry()->getId(), $positionAccountant);
  101.         $emailAccountant = [];
  102.         foreach ($employeeAccountant as $contract) {
  103.             $emailAccountant[] = $contract->getEmployee()->getCorporateEmail();
  104.         }
  105.         //Buscamos el CM y CC
  106.         $cmEmail $this->entityManager->getRepository(Employee::class)->findEmailsByPositionInCountry($employee'CM');
  107.         $ccEmail $this->entityManager->getRepository(Employee::class)->findEmailsByPositionInCountry($employee'CC');
  108.         $cmEmail array_merge($cmEmail,$ccEmail);
  109.         $cmEmail implode(",",$cmEmail);
  110.         
  111.         if($event->getSubject()->getWorkflowStep() == "Approved_by_Supervisor"){            
  112.             /*$cmEmail = $this->entityManager->getRepository(Employee::class)->findEmailsByPositionInCountry($employee, 'CM');
  113.             $ccEmail = $this->entityManager->getRepository(Employee::class)->findEmailsByPositionInCountry($employee, 'CC');
  114.             $cmEmail = array_merge($cmEmail,$ccEmail);
  115.             $cmEmail = implode(",",$cmEmail);*/
  116.             $to_cc = ($cmEmail !='' && $emailToSend->getSendCc() !='')? $emailToSend->getSendCc().",$cmEmail"$emailToSend->getSendCc().$cmEmail;            
  117.             $emailsForNotification 
  118.             [ 
  119.                 MailHandler::RECEIVER_TO => [$employee->getCorporateEmail()],
  120.                 MailHandler::RECEIVER_CC =>  explode(",",$to_cc.",".$employee->getImmediateBoss()->getCorporateEmail()),
  121.                 MailHandler::RECEIVER_BCC => explode(",",$emailToSend->getSendBcc()) 
  122.             ];           
  123.             $subject str_replace('{{employeeName}}'ucwords($employee->getFirstName().' '.$employee->getSurname()), $emailToSend->getSubject());
  124.                         
  125.             $this->mailHandler->sendEmailByApi(
  126.                 $emailToSend->setSubject($subject), 
  127.                 $emailsForNotification
  128.                 ['process' => $event->getSubject()]                
  129.             );
  130.             $this->entityManager->detach($emailToSend);
  131.             //Buscamos el peronsal de contabilidad asociado al pais donde el empleado tiene el contrato.            
  132.             $emailToSend $this->entityManager->getRepository(Email::class)->findOneBy([
  133.                 'name' => 'absence-notification-accountant'
  134.             ]); 
  135.             /*$contract = $this->entityManager->getRepository(Employee::class)->hasContractActive($event->getSubject()->getEmployee()->getId());
  136.             $entityContract = $this->entityManager->getRepository(EntityContract::class)->findOneBy(['contract'=>$contract[0]->getId(), 'isPrincipal'=>1]);
  137.             $a3code = $entityContract->getEntity()->getCountry()->getA3code();
  138.             $positionAccountant =$this->parameters->get('absence_position_notification_by_country')[$a3code];
  139.             $employeeAccountant = $this->entityManager->getRepository(Employee::class)->getEmployeeByCountryAndPosition($entityContract->getEntity()->getCountry()->getId(), $positionAccountant);
  140.             $emailAccountant = [];
  141.             foreach ($employeeAccountant as $contract) {
  142.                 $emailAccountant[] = $contract->getEmployee()->getCorporateEmail();
  143.             }*/   
  144.             $to_cc = ($cmEmail !='' && $emailToSend->getSendCc() !='')? $emailToSend->getSendCc().",$cmEmail"$emailToSend->getSendCc().$cmEmail
  145.             //Buscamos a la persona con ese rol para ese pais.    
  146.             $emailsForNotification = [ MailHandler::RECEIVER_TO => $emailAccountant,
  147.                                        MailHandler::RECEIVER_CC => explode(",",$to_cc.",".$employee->getImmediateBoss()->getCorporateEmail()), 
  148.                                        MailHandler::RECEIVER_BCC => explode(",",$emailToSend->getSendBcc() ) ];
  149.             $subject str_replace('{{employeeName}}'ucwords($employee->getFirstName().' '.$employee->getSurname()), $emailToSend->getSubject());
  150.             //dd($event->getSubject()->getWorkflowStep(), $emailToSend, $employee, $emailsForNotification, $subject, ['process' => $event->getSubject()]);
  151.             $this->mailHandler->sendEmailByApi(
  152.                 $emailToSend->setSubject($subject), 
  153.                 $emailsForNotification
  154.                 ['process' => $event->getSubject()]
  155.             );
  156.             $this->entityManager->detach($emailToSend);
  157.             
  158.         }
  159.         if($event->getSubject()->getWorkflowStep() == "canceled")
  160.         {
  161.             // Si la ausencia fue cancelada por RRHH
  162.             if (isset($event->getSubject()->cancelByHR)){
  163.                 $to_cc = ($cmEmail !='' && $emailToSend->getSendCc() !='')? $emailToSend->getSendCc().",$cmEmail"$emailToSend->getSendCc().$cmEmail
  164.                 //Buscamos a la persona con ese rol para ese pais.    
  165.                 $emailsForNotification = [ MailHandler::RECEIVER_TO => $emailAccountant,
  166.                                            MailHandler::RECEIVER_CC => explode(",",$to_cc.",".$employee->getImmediateBoss()->getCorporateEmail().",".$employee->getCorporateEmail()), 
  167.                                            MailHandler::RECEIVER_BCC => explode(",",$emailToSend->getSendBcc() ) ];
  168.     
  169.                 /*$contract = $this->entityManager->getRepository(Employee::class)->hasContractActive($event->getSubject()->getEmployee()->getId());
  170.                 $entityContract = $this->entityManager->getRepository(EntityContract::class)->findOneBy(['contract'=>$contract[0]->getId(), 'isPrincipal'=>1]);
  171.                 $a3code = $entityContract->getEntity()->getCountry()->getA3code();
  172.                 $positionAccountant =$this->parameters->get('absence_position_notification_by_country')[$a3code];
  173.                 $employeeAccountant = $this->entityManager->getRepository(Employee::class)->getEmployeeByCountryAndPosition($entityContract->getEntity()->getCountry()->getId(), $positionAccountant);
  174.                 $emailAccountant = [];
  175.                 foreach ($employeeAccountant as $contract) {
  176.                     $emailAccountant[] = $contract->getEmployee()->getCorporateEmail();
  177.                 }*/
  178.     
  179.             }else{
  180.                 $to_cc = ($emailToSend->getSendCc())? $emailToSend->getSendCc().",".$employee->getCorporateEmail(): $employee->getCorporateEmail();            
  181.                 $emailsForNotification = [ MailHandler::RECEIVER_TO => [$employee->getImmediateBoss()->getCorporateEmail() ],
  182.                                            MailHandler::RECEIVER_CC => explode(",",$to_cc),
  183.                                            MailHandler::RECEIVER_BCC => explode(","$emailToSend->getSendBcc()) ];
  184.             }
  185.             $subject str_replace('{{employeeName}}'ucwords($employee->getFirstName().' '.$employee->getSurname()), $emailToSend->getSubject());
  186.             $this->mailHandler->sendEmailByApi(
  187.                 $emailToSend->setSubject($subject), 
  188.                 $emailsForNotification
  189.                 ['process' => $event->getSubject()]                
  190.             );
  191.             $this->entityManager->detach($emailToSend);
  192.         }
  193.         if($event->getSubject()->getWorkflowStep() == "rejected")
  194.         {
  195.             $to_cc = ($emailToSend->getSendCc())? $emailToSend->getSendCc().",".$employee->getImmediateBoss()->getCorporateEmail(): $employee->getImmediateBoss()->getCorporateEmail();            
  196.             $emailsForNotification = [ MailHandler::RECEIVER_TO => [$employee->getCorporateEmail() ],
  197.                                        MailHandler::RECEIVER_CC => explode(",",$to_cc),
  198.                                        MailHandler::RECEIVER_BCC => explode(","$emailToSend->getSendBcc())];
  199.             $subject str_replace('{{employeeName}}'ucwords($employee->getFirstName().' '.$employee->getSurname()), $emailToSend->getSubject());
  200.             $this->mailHandler->sendEmailByApi(
  201.                 $emailToSend->setSubject($subject), 
  202.                 $emailsForNotification
  203.                 ['process' => $event->getSubject()]                
  204.             );
  205.         }
  206.         $this->entityManager->detach($emailToSend);                    
  207.     }
  208.     public static function getSubscribedEvents()
  209.     {
  210.         return [
  211.             
  212.             //'workflow.completed' => 'onCompleted', //Este aplica para todos los workflow
  213.              'workflow.onboarding.completed' => 'onCompleted',
  214.              'workflow.offboarding.completed' => 'onCompleted',
  215.              'workflow.absenceVacation.completed' => 'onCompletedVacation'
  216.              
  217.         ];
  218.     }
  219. }