src/Controller/Front/QuotationController.php line 253

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Classes\FormRequestSparePart;
  4. use App\Controller\Core\BaseFrontController;
  5. use App\Entity\FormQuotationServices;
  6. use App\Entity\FormQuotationTrucks;
  7. use App\Entity\TruckBrand;
  8. use App\Entity\TruckModel;
  9. use App\Entity\TruckTonnage;
  10. use App\Entity\UserClient;
  11. use App\Form\Front\FormQuotationServicesType;
  12. use App\Form\Front\FormQuotationTrucksType;
  13. use App\Form\Front\QuotationRegisterType;
  14. use App\Manager\AdviserUserManager;
  15. use App\Manager\EstablishmentManager;
  16. use App\Manager\FormQuotationRequestManager;
  17. use App\Manager\FormQuotationServicesManager;
  18. use App\Manager\FormQuotationTrucksManager;
  19. use App\Manager\QuotationRequestManager;
  20. use App\Manager\QuotationServicesManager;
  21. use App\Manager\QuotationTrucksManager;
  22. use App\Manager\RequestServiceManager;
  23. use App\Manager\RequestSparePartManager;
  24. use App\Manager\ServiceManager;
  25. use App\Manager\TruckBrandManager;
  26. use App\Manager\TruckModelManager;
  27. use App\Manager\TruckTonnageManager;
  28. use App\Manager\UserClientManager;
  29. use App\Security\ClientQuotationAuthenticator;
  30. use App\Service\CRM\ApiCRM;
  31. use App\Service\CRM\Lead;
  32. use App\Service\Mail\QuotationRequestMail;
  33. use App\Service\Mail\QuotationServicesMail;
  34. use App\Service\Mail\QuotationTrucksMail;
  35. use App\Service\Mail\RequestServiceAdviserMail;
  36. use App\Service\Mail\RequestServiceClientMail;
  37. use App\Service\Mail\RequestSparePartsAdviserMail;
  38. use App\Service\Mail\RequestSparePartsClientMail;
  39. use App\Traits\FormFileTrait;
  40. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  41. use Symfony\Component\HttpFoundation\File\UploadedFile;
  42. use Symfony\Component\HttpFoundation\JsonResponse;
  43. use Symfony\Component\HttpFoundation\RedirectResponse;
  44. use Symfony\Component\HttpFoundation\Request;
  45. use Symfony\Component\HttpFoundation\Response;
  46. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  47. use Symfony\Component\Routing\Annotation\Route;
  48. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  49. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  50. class QuotationController extends BaseFrontController
  51. {
  52.     use FormFileTrait;
  53.     const FORM_QUOTATION_TRUCKS 'sd_form_quotation_trucks_entity';
  54.     const FORM_QUOTATION_SERVICES 'sd_form_quotation_services_entity';
  55.     const FORM_QUOTATION_REGISTER 'sd_form_quotation_register';
  56.     const FORM_QUOTATION_REGISTER_ERROR 'sd_form_quotation_register_error';
  57.     /**
  58.      * @Route("/cotizar-camiones", name="quotation_trucks", methods={"GET"})
  59.      * @Template("front/quotation/trucks.html.twig")
  60.      */
  61.     public function quotationTrucks(QuotationTrucksManager $quotationTrucksManager): array
  62.     {
  63.         $entity $this->getFormSession(self::FORM_QUOTATION_TRUCKS, new FormQuotationTrucks());
  64.         $form $this->getFormView(FormQuotationTrucksType::class, $entity'quotation_trucks_action');
  65.         $this->locals['sd'] = $quotationTrucksManager->find(1);
  66.         $this->locals['form'] = $form;
  67.         return $this->locals;
  68.     }
  69.     /**
  70.      * @Route("/cotizar-camiones", name="quotation_trucks_action", methods={"POST"})
  71.      */
  72.     public function quotationTrucksAction(Request $requestFormQuotationTrucksManager $managerQuotationTrucksMail $mailApiCRM $apiCRM): RedirectResponse
  73.     {
  74.         $entity = new FormQuotationTrucks();
  75.         $form $this->getForm(FormQuotationTrucksType::class, $entity'quotation_trucks_action');
  76.         $form->handleRequest($request);
  77.         if ($form->isValid() && $this->captchaValidate()) {
  78.             $manager->save($entity);
  79.             $mail->load($entity)->send();
  80.             $result $apiCRM->send(Lead::fromEntity($entity));
  81.             $entity->setResultCrm($result);
  82.             $manager->save($entity);
  83.             return $this->redirectToRoute('thanks_slug', ['slug' => 'cotizar-camiones']);
  84.         }
  85.         $this->saveFormSession(self::FORM_QUOTATION_TRUCKS$entity);
  86.         return $this->redirectToRoute('quotation_trucks');
  87.     }
  88.     /**
  89.      * @Route("/cotizar-servicios", name="quotation_services", methods={"GET"})
  90.      * @Template("front/quotation/services.html.twig")
  91.      */
  92.     public function quotationServices(QuotationServicesManager $quotationServicesManagerServiceManager $serviceManagerAdviserUserManager $adviserUserManagerEstablishmentManager $establishmentManager): array
  93.     {
  94.         $entity $this->getFormSession(self::FORM_QUOTATION_SERVICES, new FormQuotationServices());
  95.         $form $this->getFormView(FormQuotationServicesType::class, $entity'quotation_services_action');
  96.         $this->locals['sd'] = $quotationServicesManager->find(1);
  97.         $this->locals['services'] = $serviceManager->valid();
  98.         $this->locals['advisers'] = $adviserUserManager->serviceAdviser();
  99.         $this->locals['establishments'] = $establishmentManager->inFormQuotationServices();
  100.         $this->locals['form'] = $form;
  101.         return $this->locals;
  102.     }
  103.     /**
  104.      * @Route("/cotizar-servicios", name="quotation_services_action", methods={"POST"})
  105.      */
  106.     public function quotationServicesAction(Request $requestUserClientManager $userClientManagerRequestServiceManager $requestServiceManagerAdviserUserManager $adviserUserManagerFormQuotationServicesManager $managerQuotationServicesMail $mailRequestServiceAdviserMail $requestServiceAdviserMailRequestServiceClientMail $requestServiceClientMail): RedirectResponse
  107.     {
  108.         $entity = new FormQuotationServices();
  109.         $form $this->getForm(FormQuotationServicesType::class, $entity'quotation_services_action');
  110.         $form->handleRequest($request);
  111.         $adviserId $request->request->getInt('adviser_id');
  112.         $adviser $adviserUserManager->byId($adviserId);
  113.         if (is_null($adviser)) {
  114.             $adviser $adviserUserManager->serviceRandom();
  115.         }
  116.         $entity->setAdviser($adviser);
  117.         if ($form->isValid() && $this->captchaValidate()) {
  118.             $entity $manager->save($entity);
  119.             $user $userClientManager->byDocument($entity->getDocumentNumber());
  120.             if (is_null($user)) {
  121.                 $user $userClientManager->byEmail($entity->getEmail());
  122.                 if (is_null($user)) {
  123.                     $user $userClientManager->generate($entity->getEmail(), $entity->getName(), $entity->getDocumentNumber(), $entity->getPhone());
  124.                 }
  125.             }
  126.             $requestService $requestServiceManager->fromQuotation($user$adviser$entity);
  127.             $mail->load($entity)->send();
  128.             $requestServiceAdviserMail->load($requestService)->send();
  129.             $requestServiceClientMail->load($requestService)->send();
  130.             return $this->redirectToRoute('thanks_slug', ['slug' => 'cotizar-servicios']);
  131.         }
  132.         $this->saveFormSession(self::FORM_QUOTATION_SERVICES$entity);
  133.         return $this->redirectToRoute('quotation_services');
  134.     }
  135.     /**
  136.      * @Route("/cotizar", name="quotation_request", methods={"GET"})
  137.      * @Template("front/quotation/quotation_request.html.twig")
  138.      */
  139.     public function quotationRequest(QuotationRequestManager $quotationRequestManagerAdviserUserManager $adviserUserManagerTruckBrandManager $truckBrandManager): array
  140.     {
  141.         $this->locals['advisers'] = $adviserUserManager->sparePartsAdviser();
  142.         $this->locals['brands'] = $truckBrandManager->valid();
  143.         $this->locals['sd'] = $quotationRequestManager->find(1);
  144.         return $this->locals;
  145.     }
  146.     /**
  147.      * @Route("/cotizar", name="quotation_request_action", methods={"POST"})
  148.      */
  149.     public function quotationRequestAction(Request $requestRequestSparePartManager $requestSparePartManagerAdviserUserManager $adviserUserManagerFormQuotationRequestManager $managerQuotationRequestMail $mailRequestSparePartsAdviserMail $requestSparePartsAdviserMailRequestSparePartsClientMail $requestSparePartsClientMail): RedirectResponse
  150.     {
  151.         $brands $request->request->get('form_brand', []);
  152.         $models $request->request->get('form_model', []);
  153.         $vimPlates $request->request->get('form_vim_plate', []);
  154.         $numberCodes $request->request->get('form_number_code', []);
  155.         $quantities $request->request->get('form_quantity', []);
  156.         $discounts $request->request->get('form_discount', []);
  157.         $descriptions $request->request->get('form_description', []);
  158.         $images $request->files->get('form_images', []);
  159.         $spareParts = [];
  160.         foreach ($brands as $index => $brand) {
  161.             $model $this->getDataFromRequestArray($models$index);
  162.             $vimPlate $this->getDataFromRequestArray($vimPlates$index);
  163.             $numberCode $this->getDataFromRequestArray($numberCodes$index);
  164.             $quantity $this->getDataFromRequestArray($quantities$index);
  165.             $discount $this->getDataFromRequestArray($discounts$index);
  166.             $description $this->getDataFromRequestArray($descriptions$index);
  167.             $image null;
  168.             $imageObject array_key_exists($indexis_array($images) ? $images : []) ? $images[$index] : null;
  169.             if (!is_null($imageObject) and get_class($imageObject) === UploadedFile::class) {
  170.                 $image $this->saveFile($imageObject10000000'cotizaciones-repuestos');
  171.             }
  172.             $sparePart = (new FormRequestSparePart())->create($brand$model$vimPlate$numberCode$quantity$image$discount$description);
  173.             if (!is_null($sparePart)) {
  174.                 $spareParts[] = $sparePart->toArray();
  175.             }
  176.         }
  177.         /** @var UserClient $user */
  178.         $user $this->getUser();
  179.         $adviserId $request->request->getInt('quotation_adviser');
  180.         $adviser $adviserUserManager->byId($adviserId);
  181.         if (is_null($adviser)) {
  182.             $adviser $adviserUserManager->sparePartsRandom();
  183.         }
  184.         $entity $manager->createFromUser($user);
  185.         $entity->setSpareParts($spareParts);
  186.         $entity->setAdviser($adviser);
  187.         $manager->save($entity);
  188.         $requestSpareSpart $requestSparePartManager->fromQuotation($user$adviser$entity);
  189.         $mail->load($entity)->send();
  190.         $requestSparePartsAdviserMail->load($requestSpareSpart)->send();
  191.         $requestSparePartsClientMail->load($requestSpareSpart)->send();
  192.         return $this->redirectToRoute('quotation_thanks');
  193.     }
  194.     /**
  195.      * @Route("/cotizar/ingreso", name="quotation_login")
  196.      * @Template("front/quotation/quotation_login.html.twig")
  197.      */
  198.     public function quotationLogin(AuthenticationUtils $authenticationUtils): Response
  199.     {
  200.         if ($this->getUser()) {
  201.             return $this->redirectToRoute('quotation_request');
  202.         }
  203.         $error $authenticationUtils->getLastAuthenticationError();
  204.         $lastUsername $authenticationUtils->getLastUsername();
  205.         $this->locals['error'] = $error;
  206.         $this->locals['last_username'] = $lastUsername;
  207.         $response = new Response();
  208.         $content $this->renderView('front/quotation/quotation_login.html.twig'$this->locals);
  209.         $response->setContent($content);
  210.         return $response;
  211.     }
  212.     /**
  213.      * @Route("/cotizar/registro", name="quotation_register", methods={"GET"})
  214.      * @Template("front/quotation/quotation_register.html.twig")
  215.      */
  216.     public function quotationRegister(QuotationRequestManager $quotationRequestManager): array
  217.     {
  218.         $session $this->requestStack->getSession();
  219.         $entity $session->get(self::FORM_QUOTATION_REGISTER, new UserClient());
  220.         $session->remove(self::FORM_QUOTATION_REGISTER);
  221.         $errors $session->get(self::FORM_QUOTATION_REGISTER_ERROR, []);
  222.         $session->remove(self::FORM_QUOTATION_REGISTER_ERROR);
  223.         $session->remove('sd_form_quotation_register_errors');
  224.         $form $this->createForm(QuotationRegisterType::class, $entity, [
  225.             'action' => $this->generateUrl('quotation_register_action'),
  226.             'method' => 'POST',
  227.         ])->createView();
  228.         $this->locals['sd'] = $quotationRequestManager->find(1);
  229.         $this->locals['form'] = $form;
  230.         $this->locals['error'] = $errors;
  231.         return $this->locals;
  232.     }
  233.     /**
  234.      * @Route("/cotizar/registro", name="quotation_register_action", methods={"POST"})
  235.      */
  236.     public function quotationRegisterAction(Request $requestUserPasswordHasherInterface $hasherUserClientManager $managerUserAuthenticatorInterface $userAuthenticatorClientQuotationAuthenticator $clientQuotationAuthenticator): RedirectResponse
  237.     {
  238.         if ($this->getUser()) {
  239.             return $this->redirectToRoute('quotation_request');
  240.         }
  241.         $entity = new UserClient();
  242.         $form $this->createForm(QuotationRegisterType::class, $entity, [
  243.             'action' => $this->generateUrl('quotation_register_action'),
  244.             'method' => 'POST',
  245.         ]);
  246.         $form->handleRequest($request);
  247.         $entity->setRoles(['ROLE_USER']);
  248.         $entity->setType(UserClient::USER_TYPE_CLIENT);
  249.         $entity->setPassword($hasher->hashPassword($entity$entity->getEmail()));
  250.         if ($form->isValid() && $this->captchaValidate()) {
  251.             $manager->save($entity);
  252.             $userAuthenticator->authenticateUser($entity$clientQuotationAuthenticator$request);
  253.             return $this->redirectToRoute('quotation_request');
  254.         }
  255.         $error null;
  256.         if ($manager->byDocument($entity->getDocument())) {
  257.             $error 'document';
  258.         } elseif ($manager->byEmail($entity->getEmail())) {
  259.             $error 'email';
  260.         }
  261.         $session $this->requestStack->getSession();
  262.         $session->set(self::FORM_QUOTATION_REGISTER$entity);
  263.         $session->set(self::FORM_QUOTATION_REGISTER_ERROR$error);
  264.         return $this->redirectToRoute('quotation_register');
  265.     }
  266.     /**
  267.      * @Route("/cotizar/gracias", name="quotation_thanks")
  268.      * @Template("front/quotation/quotation_thanks.html.twig")
  269.      */
  270.     public function quotationThanks(QuotationRequestManager $quotationRequestManager): array
  271.     {
  272.         $this->locals['sd'] = $quotationRequestManager->find(1);
  273.         return $this->locals;
  274.     }
  275.     /**
  276.      * @Route("/cotizar-marcas", name="quotation_brands")
  277.      */
  278.     public function quotationBrands(Request $requestTruckBrandManager $truckBrandManager): JsonResponse
  279.     {
  280.         $brands $truckBrandManager->valid();
  281.         $brandsArray array_map(function (TruckBrand $brand) { return $brand->toArray(); }, $brands);
  282.         $response = new JsonResponse($brandsArray);
  283.         $response->headers->set('X-Robots-Tag''noindex, nofollow');
  284.         return $response;
  285.     }
  286.     /**
  287.      * @Route("/cotizar-modelos", name="quotation_models")
  288.      */
  289.     public function quotationModels(Request $requestTruckModelManager $truckModelManager): JsonResponse
  290.     {
  291.         $models $truckModelManager->valid();
  292.         $modelsArray array_map(function (TruckModel $model) { return $model->toArray(); }, $models);
  293.         $response = new JsonResponse($modelsArray);
  294.         $response->headers->set('X-Robots-Tag''noindex, nofollow');
  295.         return $response;
  296.     }
  297.     /**
  298.      * @Route("/cotizar-tonelajes", name="quotation_tonnages")
  299.      */
  300.     public function quotationTonnages(Request $requestTruckTonnageManager $truckTonnageManager): JsonResponse
  301.     {
  302.         $tonnages $truckTonnageManager->valid();
  303.         $tonnagesArray array_map(function (TruckTonnage $tonnage) { return $tonnage->toArray(); }, $tonnages);
  304.         $response = new JsonResponse($tonnagesArray);
  305.         $response->headers->set('X-Robots-Tag''noindex, nofollow');
  306.         return $response;
  307.     }
  308.     /**
  309.      * @Route("/cotizar-modelos-marca", name="quotation_models_by_brand")
  310.      */
  311.     public function quotationModelsByBrand(Request $requestTruckBrandManager $truckBrandManagerTruckModelManager $truckModelManager): JsonResponse
  312.     {
  313.         $brand $request->request->get('brand''');
  314.         if (!$brand) {
  315.             return new JsonResponse([]);
  316.         }
  317.         $brand $truckBrandManager->bySlug($brand);
  318.         if (is_null($brand)) {
  319.             return new JsonResponse([]);
  320.         }
  321.         $models $truckModelManager->byBrand($brand);
  322.         $modelsArray array_map(function (TruckModel $model) { return $model->toArray(); }, $models);
  323.         $response = new JsonResponse($modelsArray);
  324.         $response->headers->set('X-Robots-Tag''noindex, nofollow');
  325.         return $response;
  326.     }
  327.     /**
  328.      * @Route("/cotizar-modelos-tonelajes", name="quotation_models_by_tonnage")
  329.      */
  330.     public function quotationModelsByTonnage(Request $requestTruckModelManager $truckModelManagerTruckTonnageManager $truckTonnageManager): JsonResponse
  331.     {
  332.         $tonnage $request->request->get('tonnage''');
  333.         if (!$tonnage) {
  334.             return new JsonResponse([]);
  335.         }
  336.         $tonnage $truckTonnageManager->bySlug($tonnage);
  337.         if (is_null($tonnage)) {
  338.             return new JsonResponse([]);
  339.         }
  340.         $models $truckModelManager->byTonnage($tonnage);
  341.         $modelsArray array_map(function (TruckModel $model) { return $model->toArray(); }, $models);
  342.         $response = new JsonResponse($modelsArray);
  343.         $response->headers->set('X-Robots-Tag''noindex, nofollow');
  344.         return $response;
  345.     }
  346.     /**
  347.      * @Route("/cotizar-tonelajes-modelo", name="quotation_tonnages_by_model")
  348.      */
  349.     public function quotationTonnagesByModel(Request $requestTruckModelManager $truckModelManagerTruckTonnageManager $truckTonnageManager): JsonResponse
  350.     {
  351.         $model $request->request->get('model''');
  352.         if (!$model) {
  353.             return new JsonResponse([]);
  354.         }
  355.         $model $truckModelManager->bySlug($model);
  356.         if (is_null($model)) {
  357.             return new JsonResponse([]);
  358.         }
  359.         $tonnages $truckTonnageManager->byModel($model);
  360.         $tonnagesArray array_map(function (TruckTonnage $tonnage) { return $tonnage->toArray(); }, $tonnages);
  361.         $response = new JsonResponse($tonnagesArray);
  362.         $response->headers->set('X-Robots-Tag''noindex, nofollow');
  363.         return $response;
  364.     }
  365.     protected function getDataFromRequestArray($arrayint $index): ?string
  366.     {
  367.         return array_key_exists($indexis_array($array) ? $array : []) ? $array[$index] : null;
  368.     }
  369. }