<?php
namespace App\Controller\Front;
use App\Controller\Core\BaseFrontController;
use App\Entity\FormQuotationServices;
use App\Entity\Service;
use App\Form\Front\FormQuotationServicesType;
use App\Manager\AdviserUserManager;
use App\Manager\EstablishmentManager;
use App\Manager\FormQuotationServicesManager;
use App\Manager\RequestServiceManager;
use App\Manager\ServiceManager;
use App\Manager\ServicesManager;
use App\Manager\UserClientManager;
use App\Service\Mail\QuotationServicesMail;
use App\Service\Mail\RequestServiceAdviserMail;
use App\Service\Mail\RequestServiceClientMail;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
class ServiceController extends BaseFrontController
{
const FORM_QUOTATION_SERVICE = 'sd_form_quotation_service_entity';
/**
* @Route("/servicios", name="services")
* @Template("front/services/services.html.twig")
*/
public function services(ServicesManager $servicesManager, ServiceManager $serviceManager): array
{
$this->locals['sd'] = $servicesManager->find(1);
$this->locals['services'] = $serviceManager->valid();
return $this->locals;
}
/**
* @Route("/servicio/{slug}", name="service", methods={"GET"})
* @Template("front/services/service.html.twig")
*/
public function service(string $slug, ServiceManager $serviceManager, AdviserUserManager $adviserUserManager, EstablishmentManager $establishmentManager): array
{
/** @var Service|null $service */
$service = $serviceManager->bySlug($slug);
if (is_null($service)) {
throw new NotFoundHttpException('Service not found');
}
$entity = $this->getFormSession(self::FORM_QUOTATION_SERVICE, new FormQuotationServices());
$form = $this->getFormView(FormQuotationServicesType::class, $entity, 'service_action', ['slug' => $slug]);
$this->locals['service'] = $service;
$this->locals['advisers'] = $adviserUserManager->serviceAdviser();
$this->locals['establishments'] = $establishmentManager->inFormQuotationServices();
$this->locals['form'] = $form;
$this->locals['page_type'] = 'services';
return $this->locals;
}
/**
* @Route("/servicio/{slug}", name="service_action", methods={"POST"})
*/
public function serviceAction(string $slug, Request $request, ServiceManager $serviceManager, UserClientManager $userClientManager, RequestServiceManager $requestServiceManager, AdviserUserManager $adviserUserManager, FormQuotationServicesManager $manager, QuotationServicesMail $mail, RequestServiceAdviserMail $requestServiceAdviserMail, RequestServiceClientMail $requestServiceClientMail): RedirectResponse
{
/** @var Service|null $service */
$service = $serviceManager->bySlug($slug);
if (is_null($service)) {
throw new NotFoundHttpException('Service not found');
}
$entity = new FormQuotationServices();
$form = $this->getForm(FormQuotationServicesType::class, $entity, 'quotation_services_action', ['slug' => $slug]);
$form->handleRequest($request);
$entity->setService($service->getTitle());
$adviserId = $request->request->getInt('adviser_id');
$adviser = $adviserUserManager->byId($adviserId);
if (is_null($adviser)) {
$adviser = $adviserUserManager->serviceRandom();
}
$entity->setAdviser($adviser);
if ($form->isValid()) {
$entity = $manager->save($entity);
$user = $userClientManager->byDocument($entity->getDocumentNumber());
$user = $user ? : $userClientManager->byEmail($entity->getEmail());
if (is_null($user)) {
$user = $userClientManager->generate($entity->getEmail(), $entity->getName(), $entity->getDocumentNumber(), $entity->getPhone());
}
$requestService = $requestServiceManager->fromQuotation($user, $adviser, $entity);
$mail->load($entity)->send();
$requestServiceAdviserMail->load($requestService)->send();
$requestServiceClientMail->load($requestService)->send();
return $this->redirectToRoute('thanks_slug', ['slug' => 'cotizar-' . $service->getSlug()]);
}
$this->saveFormSession(self::FORM_QUOTATION_SERVICE, $entity);
return $this->redirectToRoute('service', ['slug' => $slug]);
}
}