src/Controller/DevisController.php line 355

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Campaign;
  4. use App\Entity\Company;
  5. use App\Entity\User;
  6. use App\Enum\CampaignTypeInitiation;
  7. use App\Enum\TypePack;
  8. use App\Event\Campaign\CampaignCreatedEvent;
  9. use App\Event\ClientUpdatedEvent;
  10. use App\Event\DevisCreatedWithNewClientEvent;
  11. use App\Form\DevisPublicType;
  12. use App\Service\DynamicHostService;
  13. use App\Repository\CompanyRepository;
  14. use App\Repository\UserRepository;
  15. use App\Service\DevisService;
  16. use App\Service\NotificationService;
  17. use App\Service\UserService;
  18. use Doctrine\ORM\EntityManagerInterface;
  19. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  20. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  21. use Symfony\Component\HttpFoundation\JsonResponse;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\HttpFoundation\Response;
  24. use Symfony\Component\Mime\Address;
  25. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  26. use Symfony\Component\Routing\Annotation\Route;
  27. use App\Entity\CreditHistory;
  28. use App\Repository\CreditHistoryRepository;
  29. use App\Service\PackService;
  30. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  31. use App\Service\MissionService;
  32. #[Route(''name'devis_')]
  33. class DevisController extends AbstractController
  34. {   
  35.     public function __construct(
  36.         private CompanyRepository $companyRepository
  37.         private UserRepository $userRepository
  38.         private EntityManagerInterface $entityManager
  39.         private EventDispatcherInterface $dispatcher
  40.         private UserService $userService
  41.         private  UserPasswordHasherInterface $hasher
  42.         private NotificationService $notificationService,
  43.         private DevisService $devisService,  
  44.         private MissionService $missionService,
  45.     ){
  46.     }
  47.     #[Route('/api/devis/'name'add'methods: ['POST''GET'])]
  48.     public function add(Request $requestParameterBagInterface $parameter,CreditHistoryRepository $creditHistoryRepository): JsonResponse|Response
  49.     {
  50.         
  51.         $campaign = new Campaign();
  52.        
  53.         $form $this->createForm(DevisPublicType::class, $campaign, ['csrf_protection' => false]);
  54.         $form->handleRequest($request);
  55.         if ($form->isSubmitted() && $form->isValid()) {
  56.             
  57.             $recaptcha_response =   $request->request->get('token'); 
  58.             $originPostDevice =   $request->query->get('device'); 
  59.             $recaptcha_url 'https://www.google.com/recaptcha/api/siteverify';
  60.             $recaptcha_secret =  $parameter->get('recaptcha_secret');
  61.             $recaptcha file_get_contents($recaptcha_url '?secret=' $recaptcha_secret '&response=' $recaptcha_response);
  62.             $recaptcha json_decode($recaptcha);
  63.             if ($originPostDevice != 'mobil' && !($recaptcha->success && $recaptcha->score >= 0.5)){
  64.                 return new JsonResponse([
  65.                     'status'=> 'ko'
  66.                     'stssatus'=> $originPostDevice
  67.                     'response'=> $recaptcha_response
  68.                     'recaptcha_secret'=>$recaptcha_secret,
  69.                     'recaptcha_status'=>$recaptcha->success,
  70.                     'recaptcha_score'=>  $recaptcha->score,
  71.                     'message'=> 'recaptchat invalid'
  72.                     'short_code'=> 'invalid_recaptchat'
  73.                 ],401); 
  74.             }
  75.             $campaignTypeInitiation CampaignTypeInitiation::CAMPAIGN_FROM_DEVIS_PUBLIC->value;
  76.             $emailOrderedBy $form->get('orderedBy')->getData(); 
  77.             $attachments =  $request->files->get('devis_public')!=null $request->files->get('devis_public')['attachments'] : []; 
  78.             
  79.             $companyName $form->get('company')->getData(); 
  80.             $userPersisted $this->userRepository->findOneBy(['email'=>$emailOrderedBy]); 
  81.             if($userPersisted != null){
  82.                  $userRoleToUbleCreateACampaign = !(in_array('ROLE_SUBCONTRACTOR'$userPersisted->getRoles()) OR in_array('ROLE_ADMIN'$userPersisted->getRoles()));
  83.                  if($userRoleToUbleCreateACampaign){
  84.                     $companyPersisted $userPersisted->getCompany(); 
  85.                     $company is_null($companyPersisted) ? $this->devisService->createCompany($userPersisted$companyName) : $companyPersisted 
  86.                     $campaign->setCompany($company)
  87.                                 ->setDefaultValidator([$userPersisted->getOriginalMailUser()])
  88.                                 ->setTypeInitiation($campaignTypeInitiation)
  89.                                 ->setOrderedBy($userPersisted);
  90.                     ;
  91.                     $creditHistory $creditHistoryRepository->findBy(['company'=>$company,'typePack' => '4']);
  92.                   
  93.                     if (empty($creditHistory)) {
  94.                     
  95.                         $creditHistory $creditHistoryRepository->findBy(['company'=>$company])[0];
  96.                     }else{
  97.                 
  98.                         $creditHistory $creditHistory[0];
  99.                     }
  100.         
  101.                     $campaign->setCreditHistory($creditHistory);
  102.                     $event = new CampaignCreatedEvent($campaign'CREATION_DEVIS_PUBLIC');
  103.                     $this->dispatcher->dispatch($eventCampaignCreatedEvent::NAME);
  104.                     $this->entityManager->persist($campaign); 
  105.                     $this->entityManager->flush(); 
  106.                     $this->devisService->addAttachements($campaign,$attachments);
  107.                     foreach ($campaign->getMissions() as $mission) {
  108.                          $this->missionService->addHistorique(mission$missionuser:$campaign->getOrderedBy() , action"devis_public");
  109.                     }
  110.                     return new JsonResponse([
  111.                         'status'=>'ok'
  112.                         'message'=> 'create with success'
  113.                         'is_new_user'=> false
  114.                         'short_code'=> 'create_with_success'
  115.                     ],201);
  116.                  }
  117.            
  118.                  return new JsonResponse([
  119.                     'status'=>'ko'
  120.                     'message'=> 'not allowed to create devis'
  121.                     'short_code'=> 'unable_to_create_devis'
  122.                 ],401); 
  123.             
  124.             }
  125.             else {
  126.                 $company =  $this->companyRepository->findOneBy(['name'=>$companyName]); 
  127.                 $password $this->userService->generatePassword();
  128.                 
  129.                 $user = new User(); 
  130.                 $hashedPassword $this->hasher->hashPassword($user$password);
  131.                 $user->setPassword($hashedPassword)
  132.                      ->setRoles(['ROLE_CLIENT'])
  133.                      ->setEmail($emailOrderedBy)
  134.                      ->setCellPhone($form->get('phoneNumber')->getData())
  135.                      ->setEnabled(false)
  136.                      ->setState('enabled')
  137.                      ;
  138.                 $this->entityManager->persist($user);
  139.                 $this->entityManager->flush();
  140.                 
  141.                 $event = new ClientUpdatedEvent($usertrue);
  142.                 $this->dispatcher->dispatch($eventClientUpdatedEvent::NAME);
  143.                 $company is_null($company) ? $this->devisService->createCompany($user$companyName) : $company 
  144.                 $campaign->setCompany($company)
  145.                          ->setDefaultValidator([$user->getOriginalMailUser()])
  146.                          ->setTypeInitiation($campaignTypeInitiation)
  147.                          ->setOrderedBy($user);
  148.                 $creditHistory $creditHistoryRepository->findBy(['company'=>$company,'typePack' => '4']);
  149.                     if (empty($creditHistory)) {
  150.                         $creditHistory $creditHistoryRepository->findBy(['company'=>$company])[0];
  151.                     }else{
  152.                         $creditHistory $creditHistory[0];
  153.                     }
  154.                     $campaign->setCreditHistory($creditHistory);
  155.                 $user->setCompany($company); 
  156.                 $this->entityManager->persist($campaign);
  157.                 $this->entityManager->flush(); 
  158.                 $linkAttachments$this->devisService->addAttachements($campaign,$attachments); 
  159.                 $event = new CampaignCreatedEvent($campaign'CREATION_DEVIS_PUBLIC', [], false,  $linkAttachmentsfalsetrue);
  160.                  $this->dispatcher->dispatch($eventCampaignCreatedEvent::NAME);
  161.                 // $event = new DevisCreatedWithNewClientEvent($campaign, $linkAttachments); 
  162.                 // $this->dispatcher->dispatch($event, DevisCreatedWithNewClientEvent::NAME);
  163.                 $user->setDeleted(false); 
  164.                 $this->entityManager->flush();
  165.                 foreach ($campaign->getMissions() as $mission) {
  166.                      $this->missionService->addHistorique(mission$missionuser:$campaign->getOrderedBy() , action"devis_public");
  167.                 }
  168.   
  169.         }
  170.         
  171.             return new JsonResponse([
  172.                 'status'=>'ok'
  173.                 'message'=> 'create with success'
  174.                 'is_new_user'=> true
  175.                 'short_code'=> 'create_with_success']); 
  176.         }
  177.         // return new JsonResponse([
  178.         //     'status'=>'ko', 
  179.         //     'message'=> 'not allowed to create devis', 
  180.         //     'short_code'=> 'unable_to_create_devis'
  181.         // ], 401); 
  182.         return $this->render('devis/index.html.twig',[
  183.             'form'=>$form->createView()
  184.         ]); 
  185.     }
  186.     #[Route('/creation/devis'name'create_devis_public_agence'methods: ['POST''GET'])]
  187.     public function create(Request $requestParameterBagInterface $parameter,CreditHistoryRepository $creditHistoryRepository,DynamicHostService $host): JsonResponse|Response
  188.     {
  189.         $companyAgency $host->getCompany();
  190.         //redirection si MYFlow
  191.         if ($companyAgency == null) {
  192.              return $this->redirectToRoute('app_login');
  193.         }
  194.         //fin redirection
  195.         $campaign = new Campaign();
  196.         $form $this->createForm(DevisPublicType::class, $campaign, ['csrf_protection' => false]);
  197.          $form->handleRequest($request);
  198.         if ($form->isSubmitted() && $form->isValid()) {
  199.             $campaignTypeInitiation CampaignTypeInitiation::CAMPAIGN_FROM_DEVIS_PUBLIC->value;
  200.             $emailOrderedBy $form->get('orderedBy')->getData(); 
  201.             $attachments =  $request->files->get('devis_public')!=null $request->files->get('devis_public')['attachments'] : []; 
  202.             $companyName $form->get('company')->getData(); 
  203.             $userPersisted $this->userRepository->getUserByCompanyForAgency($emailOrderedBy,$companyAgency);
  204.             $userMyFlow =  $this->userRepository->getUserMyFlow($emailOrderedBy);
  205.             if (is_null($userPersisted) and !is_null($userMyFlow)) {
  206.                 return $this->redirectToRoute('devis_failed_devis_public_agency', ['id' => $companyAgency->getId()]);
  207.             }
  208.             if($userPersisted != null){
  209.                  $userRoleToUbleCreateACampaign = !(in_array('ROLE_SUBCONTRACTOR'$userPersisted->getRoles()) OR in_array('ROLE_ADMIN'$userPersisted->getRoles()));
  210.                  if($userRoleToUbleCreateACampaign){
  211.                     $companyPersisted $userPersisted->getCompany(); 
  212.                     if (!is_null($companyName) and !empty($companyName) and is_null($userPersisted?->getCompany())) {
  213.                         $company $this->devisService->createCompanyAgency($userPersisted$companyName,$companyAgency) ; 
  214.                     }else{
  215.                         $company $userPersisted?->getCompany() ?? $companyAgency;
  216.                     }
  217.                     $campaign->setCompany($company)
  218.                                 ->setDefaultValidator([$userPersisted->getOriginalMailUser()])
  219.                                 ->setTypeInitiation($campaignTypeInitiation)
  220.                                 ->setOrderedBy($userPersisted);
  221.                     ;
  222.                     $creditHistory $creditHistoryRepository->findBy(['company'=>$company,'typePack' => '4']);
  223.                   
  224.                     if (empty($creditHistory)) {
  225.                     
  226.                         $creditHistory $creditHistoryRepository->findBy(['company'=>$company])[0];
  227.                     }else{
  228.                 
  229.                         $creditHistory $creditHistory[0];
  230.                     }
  231.         
  232.                     $campaign->setCreditHistory($creditHistory);
  233.                     $event = new CampaignCreatedEvent($campaign'CREATION_DEVIS_PUBLIC');
  234.                     $this->dispatcher->dispatch($eventCampaignCreatedEvent::NAME);
  235.                     $this->entityManager->persist($campaign); 
  236.                     $this->entityManager->flush(); 
  237.                     $this->devisService->addAttachements($campaign,$attachments);
  238.                     foreach ($campaign->getMissions() as $mission) {
  239.                          $this->missionService->addHistorique(mission$missionuser:$campaign->getOrderedBy() , action"devis_public");
  240.                     }
  241.                      return $this->redirectToRoute('devis_confirmation_devis_public_agency', ['id' => $companyAgency->getId()]);
  242.                  }
  243.                 return $this->redirectToRoute('devis_failed_devis_public_agency', ['id' => $companyAgency->getId()]);
  244.             }
  245.             else {
  246.                 $company =  $this->companyRepository->findOneBy(['name'=>$companyName]); 
  247.                 
  248.               
  249.                 $password $this->userService->generatePassword();
  250.                 $userMyFlow =  $this->userRepository->getUserMyFlow($emailOrderedBy);
  251.                 if ($userMyFlow != null) {
  252.                     return $this->redirectToRoute('devis_failed_devis_public_agency', ['id' => $companyAgency->getId()]);
  253.                 }
  254.                 $user = new User(); 
  255.                 $hashedPassword $this->hasher->hashPassword($user$password);
  256.                 $user->setPassword($hashedPassword)
  257.                      ->setRoles(['ROLE_CLIENT'])
  258.                      ->setEmail($emailOrderedBy)
  259.                      ->setCellPhone($form->get('phoneNumber')->getData())
  260.                      ->setEnabled(false)
  261.                      ->setIsNewClient(false)
  262.                      ->setState('enabled')
  263.                      ;
  264.                 $this->entityManager->persist($user);
  265.                 $this->entityManager->flush();
  266.                 
  267.                 $event = new ClientUpdatedEvent($usertrue);
  268.                 $this->dispatcher->dispatch($eventClientUpdatedEvent::NAME);
  269.                 if (!is_null($companyName) and !empty($companyName)) {
  270.                     //verification si company MyFlow,Alors faire la création
  271.                     if (!is_null($company) and $company?->getTypeCompany() == null and $company?->getParent() == null) {
  272.                         $company $this->devisService->createCompanyAgency($user$companyName,$companyAgency); 
  273.                     }else{
  274.                         $company is_null($company) ? $this->devisService->createCompanyAgency($user$companyName,$companyAgency) : $company 
  275.                     }
  276.                 }else{
  277.                     $company $companyAgency;
  278.                 }
  279.                 $campaign->setCompany($company)
  280.                          ->setDefaultValidator([$user->getOriginalMailUser()])
  281.                          ->setTypeInitiation($campaignTypeInitiation)
  282.                          ->setOrderedBy($user);
  283.                 $creditHistory $creditHistoryRepository->findBy(['company'=>$company,'typePack' => '4']);
  284.                     if (empty($creditHistory)) {
  285.                         $creditHistory $creditHistoryRepository->findBy(['company'=>$company])[0];
  286.                     }else{
  287.                         $creditHistory $creditHistory[0];
  288.                     }
  289.                     $campaign->setCreditHistory($creditHistory);
  290.                 $user->setCompany($company); 
  291.                 $this->entityManager->persist($campaign);
  292.                 $this->entityManager->flush(); 
  293.                 $linkAttachments$this->devisService->addAttachements($campaign,$attachments); 
  294.                 $event = new CampaignCreatedEvent($campaign'CREATION_DEVIS_PUBLIC', [], false,  $linkAttachmentsfalsetrue);
  295.                  $this->dispatcher->dispatch($eventCampaignCreatedEvent::NAME);
  296.                 $event = new DevisCreatedWithNewClientEvent($campaign$linkAttachments); 
  297.                 $this->dispatcher->dispatch($eventDevisCreatedWithNewClientEvent::NAME);
  298.                 $user->setDeleted(false); 
  299.                 $this->entityManager->flush();
  300.                 foreach ($campaign->getMissions() as $mission) {
  301.                      $this->missionService->addHistorique(mission$missionuser:$campaign->getOrderedBy() , action"devis_public");
  302.                 }
  303.                 return $this->redirectToRoute('devis_confirmation_devis_public_agency', ['id' => $companyAgency->getId()]);
  304.                
  305.             }
  306.         }
  307.         return $this->render('inscription/devis_public.html.twig',[
  308.             'form'=>$form->createView(),
  309.             'company' => $companyAgency
  310.         ]); 
  311.     }
  312.     #[Route('/confirmation/devis/{id}'name'confirmation_devis_public_agency'methods: ['POST''GET'])]
  313.     public function confirmation(Company $company):Response
  314.     {
  315.         return $this->render('inscription/confirmation_devis.html.twig', [
  316.                 'company' => $company
  317.         ]);
  318.     }
  319.     #[Route('/failed/devis/{id}'name'failed_devis_public_agency'methods: ['POST''GET'])]
  320.     public function failed(Company $company):Response
  321.     {
  322.         return $this->render('inscription/failed_devis.html.twig', [
  323.                 'company' => $company
  324.         ]);
  325.     }
  326. }