src/EventSubscriber/MissionSubscriber.php line 251

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Mission;
  4. use App\Entity\SystemEmail;
  5. use App\Entity\User;
  6. use App\Enum\Role;
  7. use App\Enum\Trigger;
  8. use App\Event\Mission\ContactAddedEvent;
  9. use App\Event\Mission\MissionAcceptedEvent;
  10. use App\Event\Mission\MissionActivatedEvent;
  11. use App\Event\Mission\MissionArchivedEvent;
  12. use App\Event\Mission\MissionCancelledEvent;
  13. use App\Event\Mission\MissionDesiredDeliveryUpdatedAfterValidationEvent;
  14. use App\Event\Mission\MissionDesiredDeliveryUpdatedBeforeValidationEvent;
  15. use App\Event\Mission\MissionInitialTimeEvent;
  16. use App\Event\Mission\MissionRealTimeEvent;
  17. use App\Event\Mission\MissionRefusedEvent;
  18. use App\Event\Mission\MissionWithoutSubContractorCheckedEvent;
  19. use App\Event\Mission\MissionWithoutWorkflowEvent;
  20. use App\Repository\SystemEmailRepository;
  21. use App\Repository\UserRepository;
  22. use App\Service\NotificationService;
  23. use App\Service\TriggerService;
  24. use Symfony\Bridge\Twig\Mime\NotificationEmail;
  25. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  26. use Symfony\Component\Mailer\MailerInterface;
  27. use Symfony\Component\Mime\Address;
  28. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  29. use Symfony\Component\Routing\RouterInterface;
  30. use App\Event\Mission\MissionSendEvent;
  31. use App\Repository\MissionParticipantRepository;
  32. use App\Enum\AdminMail;
  33. use App\Enum\Manager;
  34. use App\Event\Admin\MissionNotificationActivatedEvent;
  35. use App\Event\Mission\MissionCanActivateEvent;
  36. use App\Event\Mission\MissionResendEvent;
  37. use App\Service\MissionService;
  38. use App\Service\StepsService;
  39. use App\Service\WorkflowStepService;
  40. use  App\Service\RelanceService;
  41. use Symfony\Component\Security\Core\Security;
  42. use App\Event\Mission\MissionClosedEvent;
  43. use Doctrine\ORM\EntityManagerInterface;
  44. use App\Service\DynamicHostService;
  45. class MissionSubscriber implements EventSubscriberInterface
  46. {
  47.     public function __construct(
  48.         private MailerInterface $mailer,
  49.         private UserRepository $userRepository,
  50.         private RouterInterface $router,
  51.         private TriggerService $triggerService,
  52.         private SystemEmailRepository $systemEmailRepository,
  53.         private NotificationService $notificationService,
  54.         private MissionParticipantRepository $missionParticipantRepository,
  55.         private Security $security,
  56.         private WorkflowStepService $workflowStepService,
  57.         private StepsService $stepsService,
  58.         private MissionService $missionService,
  59.         private RelanceService $relanceService,
  60.         private EntityManagerInterface $entityManagerInterface,
  61.         private DynamicHostService $dynamicHostService
  62.         
  63.     ){}
  64.     public static function getSubscribedEvents()
  65.     {
  66.         return [
  67.             MissionRefusedEvent::NAME => 'onMissionRefused',
  68.             MissionArchivedEvent::NAME => 'onMissionArchived',
  69.             MissionWithoutWorkflowEvent::NAME => 'onMissionWithoutWorkflow',
  70.             MissionCancelledEvent::NAME => 'onMissionCancelled',
  71.             MissionAcceptedEvent::NAME => 'onMissionAccepted',
  72.             MissionInitialTimeEvent::NAME => 'onMissionInitialTime',
  73.             MissionRealTimeEvent::NAME => 'onMissionRealTime',
  74.             MissionCanActivateEvent::NAME => 'onMissionCanActivateEvent',
  75.             ContactAddedEvent::NAME => 'onContactAdded',
  76.             MissionWithoutSubContractorCheckedEvent::NAME => 'onMissionWithoutSubContractorChecked',
  77.             MissionActivatedEvent::NAME => 'onMissionActivated',
  78.             MissionDesiredDeliveryUpdatedAfterValidationEvent::NAME => 'onMissionDesiredDeliveryUpdatedAfterValidation',
  79.             MissionDesiredDeliveryUpdatedBeforeValidationEvent::NAME => 'onMissionDesiredDeliveryUpdatedBeforeValidation',
  80.             MissionSendEvent::NAME => 'onMissionSend',
  81.             MissionResendEvent::NAME=>'onMissionResend',
  82.             MissionNotificationActivatedEvent::NAME=>'onMissionNotificationActivatedEvent',
  83.             MissionClosedEvent::NAME=>'onMissionClosedEvent'
  84.         ];
  85.     }
  86.     
  87.     public function onMissionClosedEvent(MissionClosedEvent $event){
  88.         $mission $event->getMission();
  89.         $participant $mission->getParticipants();
  90.         $listIdParticipant = [];
  91.         $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::MISSION_FINISH]);
  92.         foreach ($mission->getParticipants() as $participant) {
  93.             if ($participant->getRole() === Role::ROLE_SUBCONTRACTOR 
  94.                 and !in_array($participant->getId(), $listIdParticipant) and $participant->getUser()->getSalary() != 1
  95.                ) {
  96.                 $user $participant->getUser();
  97.                 $this->notificationService->create($email$user$usernull$mission->getWorkflow()?->getActiveStep(), $mission->getCampaign(),null,false,false,null,null,$mission);
  98.                 $listIdParticipant[] = $participant->getId();
  99.             }
  100.         }
  101.     }
  102.     public function onMissionCanActivateEvent(MissionCanActivateEvent $missionCanActivateEvent){
  103.         $mission$missionCanActivateEvent->getMission();
  104.         $marge$missionCanActivateEvent->getMarge();
  105.         $refPanier $mission->getReference();
  106.         $productName $mission->getProduct()->getName();
  107.         $content "Ce projet portant la référence $refPanier présente une marge ({$marge} %)insuffisante.
  108.          Nous vous invitons à modifier le panier ou à revoir la tarification des prestataires pour activer la mission (Produit: $productName )";
  109.         
  110.          $admins $this->dynamicHostService->getMailAdmin();
  111.         $addressFrom = new Address("caroline.b@my-flow.fr@my-flow.fr""Myflow");
  112.         foreach ($admins as $admin){
  113.             $addressTo = new Address($admin->value);
  114.             $this->notificationService->sendEmail($addressFrom,$addressTo"Projet avec marge insuffisante",$content);
  115.         }
  116.     }
  117.     public function onMissionNotificationActivatedEvent(MissionNotificationActivatedEvent $missionNotificationActivatedEvent)
  118.     {   // We commented this code because in line 263 NOTIFICATION_APRES_ACTIVATION is send to a subcontractor (onMissionNotificationActivatedEvent)
  119.         // $email = $this->systemEmailRepository->findOneBy(['code' => SystemEmail::NOTIFICATION_APRES_ACTIVATION]);
  120.         // foreach ($missionNotificationActivatedEvent->getMission()->getCampaign()->getCompany()->getUsers() as $user) {
  121.         //     if($user->isEnabled()){
  122.         //         $this->notificationService->create($email, $user, $user, $user->getCompany(),$missionNotificationActivatedEvent->getMission()->getWorkflow()?->getSteps()?->first());
  123.         //     }
  124.         // }
  125.     }
  126.     public function onMissionSend(MissionSendEvent $event){
  127.         $user $event->getUser();
  128.         $mission $event->getMission();
  129.         $company $mission->getCampaign()->getCompany();
  130.         if (!$mission instanceof Mission) {
  131.             return;
  132.         }
  133.         if (!$user instanceof User) {
  134.             return;
  135.         }
  136.         // if ((gettype($event->getRole()) != 'string' && trim($event->getRole()) == 'ROLE_VALIDATOR') ||   $event->getRole() == 'ROLE_VALIDATOR'){
  137.         //     $email = $this->systemEmailRepository->findOneBy(['code' => SystemEmail::AJOUT_VALIDATEUR]);
  138.         // }else{
  139.         //     $email = $this->systemEmailRepository->findOneBy(['code' => SystemEmail::AJOUT_OBSERVATEUR]);
  140.         // }
  141.         // if (null !== $email) {
  142.         //     $this->notificationService->create($email, $user, $user, $company, $mission->getWorkflow()?->getActiveStep(), $mission->getCampaign());
  143.         // }
  144.     }
  145.     public function onMissionRefused(MissionRefusedEvent $event)
  146.     {
  147.         $mission $event->getMission();
  148.         if (!$mission instanceof Mission) {
  149.             return;
  150.         }
  151.         $intervenant $event->getIntervenant();
  152.         $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::MISSION_REFUSEE_INTERVENANT]);
  153.         if (null !== $email) {
  154.             $this->notificationService->create($email$intervenant$intervenant$mission->getCampaign()->getOrderedBy()->getCompany(), null$mission->getCampaign(), nullfalsefalse,nullnull$mission);
  155.         }
  156.          $admins $this->dynamicHostService->getMailAdmin();
  157.         $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::MISSION_REFUSEE_ADMIN]);
  158.         if (null !== $email) {
  159.             foreach ($admins as $admin) {
  160.                 $this->notificationService->create($email$admin->value$intervenant$mission->getCampaign()->getOrderedBy()->getCompany(), null$mission->getCampaign(), nullfalsefalse,nullnull$mission);
  161.             }
  162.         }
  163.     }
  164.     public function onMissionArchived(MissionArchivedEvent $event)
  165.     {
  166.         $mission $event->getMission();
  167.         if (!$mission instanceof Mission) {
  168.             return;
  169.         }
  170.         foreach ($mission->getWorkflow()->getSteps()->last()->getActions() as $action) {
  171.             foreach ($action->getTriggers() as $trigger) {
  172.                 if ($trigger->getTriggerType() === Trigger::MISSION_ARCHIVED) {
  173.                     
  174.                     $this->triggerService->execute($trigger);
  175.                 }
  176.             }
  177.         }
  178.         $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::MISSION_ARCHIVED_CLIENT]);
  179.         if (null !== $email) {
  180.             $this->notificationService->create($email$mission->getCampaign()->getOrderedBy(), $mission->getCampaign()->getOrderedBy(), $mission->getCampaign()->getOrderedBy()->getCompany(), null$mission->getCampaign());
  181.         }
  182.         $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::MISSION_ARCHIVED_PRESTATAIRE]);
  183.         if (null !== $email) {
  184.             foreach ($mission->getParticipants() as $participant) {
  185.                 if ($participant->getRole() === Role::ROLE_SUBCONTRACTOR) {
  186.                     $this->notificationService->create($email$participant->getUser(), $participant->getUser(), $mission->getCampaign()->getOrderedBy()->getCompany(), null$mission->getCampaign());
  187.                 }
  188.             }
  189.         }
  190.          $admins $this->dynamicHostService->getMailAdmin();
  191.         $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::MISSION_ARCHIVED_ADMIN]);
  192.         if (null !== $email) {
  193.             foreach ($admins as $admin){
  194.                 $this->notificationService->create($email$admin->value$mission->getCampaign()->getOrderedBy(), $mission->getCampaign()->getOrderedBy()->getCompany(), null$mission->getCampaign());
  195.             }
  196.         }
  197.     }
  198.     public function onMissionWithoutWorkflow(MissionWithoutWorkflowEvent $event)
  199.     {
  200.         
  201.         $mission $event->getMission();
  202.         if (!$mission instanceof Mission) {
  203.             return;
  204.         }
  205.          $admins $this->dynamicHostService->getMailAdmin();
  206.         foreach ($admins as $admin) {
  207.             $notification = (new NotificationEmail())
  208.                 ->to(new Address($admin->value))
  209.                 ->subject('Une mission a été créée sans workflow')
  210.                 ->content('
  211.                 <p>Bonjour,</p>
  212.                 <p>La mission '$mission->getReference() .' contient un produit "'.$mission->getProduct()->getName().'" qui n\'a pas de Workflow associé.</p>
  213.                 <p>Merci d\'en créer un pour ce produit et d\'aller le relier à la mission.</p>
  214.             ')
  215.                 ->action('Modifier la mission'$this->router->generate('handle_mission_campaign', ['id' => $mission->getCampaign()->getId()], UrlGeneratorInterface::ABSOLUTE_URL))
  216.                 ->markAsPublic()
  217.             ;
  218.             $this->mailer->send($notification);
  219.         }
  220.     }
  221.     public function onMissionDesiredDeliveryUpdatedAfterValidation(MissionDesiredDeliveryUpdatedAfterValidationEvent $event)
  222.     {
  223.         $mission $event->getMission();
  224.         
  225.         if (!$mission instanceof Mission) {
  226.             return;
  227.         }
  228.         // $email = $this->systemEmailRepository->findOneBy(['code' => SystemEmail::MISSION_DESIRED_DELIVERY_UPDATED_AFTER_VALIDATION]);
  229.         // $emailParticipants = [];
  230.         // foreach ($mission->getParticipants() as $participant) {
  231.         //     $user= $participant->getUser();
  232.         //     $emailUser = $user->getEmail();
  233.         //     if(!in_array($emailUser, $emailParticipants) && ! $this->security->isGranted('ROLE_SUBCONTRACTOR')){
  234.         //         $this->notificationService->create($email, $user, $user, $mission->getCampaign()->getOrderedBy()->getCompany(), null, $mission->getCampaign());
  235.         //         $emailParticipants=[...$emailParticipants, $emailUser];
  236.         //     }
  237.         // }
  238.         
  239.         // if (null !== $email && [...$emailParticipants, $mission->getCampaign()->getOrderedBy()->getEmail()]) {
  240.         //     $this->notificationService->create($email, $mission->getCampaign()->getOrderedBy(), $mission->getCampaign()->getOrderedBy(), $mission->getCampaign()->getOrderedBy()->getCompany(), null, $mission->getCampaign());
  241.         // }
  242.     }
  243.     public function onMissionDesiredDeliveryUpdatedBeforeValidation(MissionDesiredDeliveryUpdatedBeforeValidationEvent $event)
  244.     {
  245.         $mission $event->getMission();
  246.         if (!$mission instanceof Mission) {
  247.             return;
  248.         }
  249.         $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::MISSION_DESIRED_DELIVERY_UPDATED_BEFORE_VALIDATION]);
  250.         if (null !== $email) {
  251.             $this->notificationService->create($email$mission->getCampaign()->getOrderedBy(), $mission->getCampaign()->getOrderedBy(), $mission->getCampaign()->getOrderedBy()->getCompany(), null$mission->getCampaign());
  252.         }
  253.     }
  254.     public function onMissionCancelled(MissionCancelledEvent $event)
  255.     {
  256.         $mission $event->getMission();
  257.         if (!$mission instanceof Mission) {
  258.             return;
  259.         }
  260.         $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::MISSION_CANCELLED]);
  261.         if (null !== $email) {
  262.             $this->notificationService->create($email$mission->getCampaign()->getOrderedBy(), $mission->getCampaign()->getOrderedBy(), $mission->getCampaign()->getOrderedBy()->getCompany(),null,null,null,false,false,null,null,$mission);
  263.             
  264.              $admins $this->dynamicHostService->getMailAdmin();
  265.             foreach ($admins as $admin) {
  266.                 $this->notificationService->create($email$admin->value$mission->getCampaign()->getOrderedBy(), $mission->getCampaign()->getOrderedBy()->getCompany(),null,null,null,false,false,null,null,$mission);
  267.             }
  268.         }
  269.     }
  270.     public function onMissionAccepted(MissionAcceptedEvent $event)
  271.     {
  272.         $mission $event->getMission();
  273.         if (!$mission instanceof Mission) {
  274.             return;
  275.         }
  276.         $intervenant $event->getIntervenant();
  277.         $emailClient $this->systemEmailRepository->findOneBy(['code' => SystemEmail::NOTIFICATION_APRES_ACTIVATION]);
  278.         if (null !== $emailClient) {
  279.             if(in_array("ROLE_SUBCONTRACTOR",$intervenant->getRoles())){
  280.                 $this->notificationService->create($emailClient$intervenant$intervenant$mission->getCampaign()->getOrderedBy()->getCompany(),null,$mission->getCampaign());
  281.             }
  282.         }
  283.         $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::MISSION_ACTIVER_CLIENT]);
  284.         if (null !== $email) {
  285.             $this->notificationService->create($email$mission->getCampaign()->getOrderedBy(), $mission->getCampaign()->getOrderedBy(), $mission->getCampaign()->getOrderedBy()->getCompany(),null,$mission->getCampaign());
  286.         }
  287.     }
  288.     public function onMissionInitialTime(MissionInitialTimeEvent $event)
  289.     {
  290.         $mission $event->getMission();
  291.         if (!$mission instanceof Mission) {
  292.             return;
  293.         }
  294.          $admins $this->dynamicHostService->getMailAdmin();
  295.         foreach ($admins as $admin) {
  296.             $notification = (new NotificationEmail())
  297.                 ->to(new Address($admin->value))
  298.                 ->subject('Un intervenant vient d\'ajouter le temps initial d\'une mission')
  299.                 ->content('
  300.                     <p>Bonjour,</p>
  301.                     <p>Le temps initial pour la mission '$mission->getCampaign()->getName() .' vient d\'être ajouté</p>
  302.                 ')
  303.                 ->action('Voir la mission'$this->router->generate('mission_edit', ['id' => $mission->getId()], UrlGeneratorInterface::ABSOLUTE_URL))
  304.                 ->markAsPublic()
  305.             ;
  306.             try {
  307.                 $this->mailer->send($notification);
  308.             } catch (\Exception $e) { /* TODO: logger ou afficher une alerte que l'email n'a pas été envoyé */ }
  309.         }
  310.     }
  311.     public function onMissionRealTime(MissionRealTimeEvent $event)
  312.     {
  313.         $mission $event->getMission();
  314.         if (!$mission instanceof Mission) {
  315.             return;
  316.         }
  317.         $emailSystem $this->systemEmailRepository->findOneBy(['code' => SystemEmail::RENSEIGNEZ_TEMPS_MISSION]);
  318.         if (null !== $emailSystem) {
  319.             foreach ($mission->getParticipants() as $participant) {
  320.                 if ($participant->getRole() === Role::ROLE_SUBCONTRACTOR) {
  321.                     $this->notificationService->create($emailSystem$participant->getUser(), $participant->getUser(), $mission->getCampaign()->getOrderedBy()->getCompany(), null$mission->getCampaign());
  322.                 }
  323.             }
  324.         }
  325.     }
  326.     public function onContactAdded(ContactAddedEvent $event)
  327.     {
  328.         $contact $event->getContact();
  329.         if (!$contact instanceof User || !$contact->isEnabled()) {
  330.             return;
  331.         }
  332.         
  333.             if ($event->getParticipant()->getRole() == Role::ROLE_VALIDATOR){
  334.                 $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::AJOUT_VALIDATEUR]);
  335.             }else{
  336.                 $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::AJOUT_OBSERVATEUR]);
  337.             }
  338.             if (null !== $email) {
  339.                 $this->notificationService->create($email$contact$contact$contact->getCompany(), false$event->getMission()->getCampaign());
  340.             }
  341.             $contact->setAutorizationDateAt(new \DateTimeImmutable());
  342.             $this->entityManagerInterface->flush();
  343.     }
  344.     public function onMissionActivated(MissionActivatedEvent $event)
  345.     {
  346.         $mission $event->getMission();
  347.         $client $event->getClient();
  348.         if (!$mission instanceof Mission) {
  349.             return;
  350.         }
  351.         $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::MISSION_ACTIVER_CLIENT]);
  352.         if (null !== $email) {
  353.             $this->notificationService->create($email$client$client$client->getCompany(), $mission->getWorkflow()?->getSteps()?->first());
  354.         }
  355.     }
  356.     public function onMissionWithoutSubContractorChecked(MissionWithoutSubContractorCheckedEvent $event)
  357.     {
  358.         $mission $event->getMission();
  359.         if (!$mission instanceof Mission) {
  360.             return;
  361.         }
  362.         $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::MISSION_SANS_PARTENAIRE]);
  363.         if (null !== $email) {
  364.            /* $this->notificationService->create($email, 'admin@my-flow.fr', $mission->getCampaign()->getOrderedBy(), $mission->getCampaign()->getOrderedBy()->getCompany());*/
  365.             if (null !== $email->getSmsContent()) {
  366.                  $admins $this->dynamicHostService->getMailAdmin();
  367.                 foreach ($admins as $admin) {
  368.                     $this->notificationService->create($email$admin->value$mission->getCampaign()->getOrderedBy(), $mission->getCampaign()->getOrderedBy()->getCompany(), nullnullnulltrue);
  369.                 }
  370.             }
  371.         }
  372.     }
  373.     public function onMissionResend(MissionResendEvent $missionResendEvent){
  374.          $this->relanceService->sendMailForResponsableStep(mission$missionResendEvent->getMission());
  375.     }
  376. }