<?php
namespace App\Controller\Front;
use App\Controller\Core\BaseFrontController;
use App\Entity\FormClaim;
use App\Entity\FormContact;
use App\Entity\FormQuotation;
use App\Entity\Forms;
use App\Form\Front\FormClaimType;
use App\Form\Front\FormContactType;
use App\Form\Front\FormQuotationType;
use App\Manager\ContactManager;
use App\Manager\FormClaimManager;
use App\Manager\FormContactManager;
use App\Manager\FormQuotationManager;
use App\Manager\FormsManager;
use App\Manager\QuotationManager;
use App\Service\CRM\ApiCRM;
use App\Service\Mail\ClaimMail;
use App\Service\Mail\ContactMail;
use App\Service\Mail\QuotationMail;
use App\Service\Recaptcha\Validator as RecaptchaValidator;
use App\Traits\FormFileTrait;
use DateInterval;
use DateTime;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class FormController extends BaseFrontController
{
use FormFileTrait;
const FORM_CONTACT = 'sd_form_contact_entity';
const FORM_QUOTATION = 'sd_form_quotation_entity';
const FORM_CLAIM = 'sd_form_claim_entity';
/**
* @Route("/contacto", name="contact", methods={"GET"})
* @Template("front/form/contact.html.twig")
*/
public function contact(ContactManager $contactManager): array
{
$entity = $this->getFormSession(self::FORM_CONTACT, new FormContact());
$form = $this->getFormView(FormContactType::class, $entity, 'contact_action');
$this->locals['sd'] = $contactManager->find(1);
$this->locals['form'] = $form;
return $this->locals;
}
/**
* @Route("/contacto", name="contact_action", methods={"POST"})
*/
public function contactAction(Request $request, FormContactManager $manager, ContactMail $mail, ApiCRM $apiCRM, RecaptchaValidator $recaptchaValidator): RedirectResponse
{
$entity = new FormContact();
$form = $this->getForm(FormContactType::class, $entity, 'contact_action');
$form->handleRequest($request);
if ($form->isValid() and $recaptchaValidator->validate($this->getRecaptchaToken(), $this->getClientIp())) {
$manager->save($entity);
$mail->load($entity)->send();
$crmLead = $entity->leadToCRM();
$result = $apiCRM->send($crmLead);
$entity->setCrmBody($crmLead->toCRM());
$entity->setCrmResult($result);
$manager->save($entity);
return $this->redirectToRoute('thanks_slug', ['slug' => 'contacto']);
}
$this->saveFormSession(self::FORM_CONTACT, $entity);
return $this->redirectToRoute('contact');
}
/**
* @Route("/cotizar", name="quotation", methods={"GET"})
* @Template("front/form/quotation.html.twig")
*/
public function quotation(QuotationManager $quotationManager): array
{
$entity = $this->getFormSession(self::FORM_QUOTATION, new FormQuotation());
$form = $this->getFormView(FormQuotationType::class, $entity, 'quotation_action');
$this->locals['sd'] = $quotationManager->find(1);
$this->locals['form'] = $form;
return $this->locals;
}
/**
* @Route("/cotizar", name="quotation_action", methods={"POST"})
*/
public function quotationAction(Request $request, FormQuotationManager $manager, QuotationMail $mail, ApiCRM $apiCRM, RecaptchaValidator $recaptchaValidator): RedirectResponse
{
$entity = new FormQuotation();
$form = $this->getForm(FormQuotationType::class, $entity, 'quotation_action');
$form->handleRequest($request);
if ($form->isValid() and $recaptchaValidator->validate($this->getRecaptchaToken(), $this->getClientIp())) {
$manager->save($entity);
$mail->load($entity)->send();
$crmLead = $entity->leadToCRM();
$result = $apiCRM->send($crmLead);
$entity->setCrmBody($crmLead->toCRM());
$entity->setCrmResult($result);
$manager->save($entity);
return $this->redirectToRoute('thanks_slug', ['slug' => 'cotizar']);
}
$this->saveFormSession(self::FORM_QUOTATION, $entity);
return $this->redirectToRoute('quotation');
}
/**
* @Route("/libro-de-reclamaciones", name="claim", methods={"GET"})
* @Template("front/form/claim.html.twig")
*/
public function claim(FormClaimManager $formClaimManager, FormsManager $formsManager): array
{
/** @var Forms $forms */
$forms = $formsManager->find(1);
$responseDays = (int)$forms->getClaimResponseDays();
try {
$this->locals['answer_date'] = (new DateTime())->add(new DateInterval('P' . $responseDays . 'D'));
} catch (\Exception $exception) {
$this->locals['answer_date'] = new DateTime();
}
$entity = new FormClaim();
$code = $this->generateCode($formClaimManager->lastCode());
$entity->setCode($code);
$entity = $this->getFormSession(self::FORM_CLAIM, $entity);
$form = $this->getFormView(FormClaimType::class, $entity, 'claim_action');
$this->locals['form'] = $form;
$this->locals['entity'] = $entity;
return $this->locals;
}
/**
* @Route("/libro-de-reclamaciones", name="claim_action", methods={"POST"})
*/
public function claimAction(Request $request, FormClaimManager $manager, ClaimMail $mail, RecaptchaValidator $recaptchaValidator): RedirectResponse
{
$entity = new FormClaim();
$form = $this->getForm(FormClaimType::class, $entity, 'claim_action');
$entity->setFile(null);
if ($request->files->get('form_file')) {
$file = $this->saveFile($request->files->get('form_file'), 1000000, 0,'reclamos');
$entity->setFile($file);
}
$code = $this->generateCode($manager->lastCode());
$entity->setCode($code);
$form->handleRequest($request);
if ($form->isValid() and $recaptchaValidator->validate($this->getRecaptchaToken(), $this->getClientIp())) {
$entity->setSaved(true);
$manager->save($entity);
$mail->load($entity)->send();
$mail->loadClient($entity)->send();
$this->saveFormSession(self::FORM_CLAIM, $entity);
$this->requestStack->getSession()->remove('sd_claim_code');
return $this->redirectToRoute('thanks');
}
$this->saveFormSession(self::FORM_CLAIM, $entity);
return $this->redirectToRoute('claim');
}
public function generateCode($lastCode): string
{
$id = $this->lastCodeId($lastCode);
return 'R' . date('Y') . date('m') . '-' . str_pad((string)($id + 1), 6, '0', STR_PAD_LEFT);
}
public function lastCodeId($lastCode): int
{
$code = $this->lastCode($lastCode);
$array = explode('-', $code);
return count($array) == 2 ? (int)$array[1] : 0;
}
public function lastCode($lastCode): string
{
$code = $lastCode;
return is_null($code) ? 'R' . date('Y') . date('m') . '-' . '100000' : $code;
}
}