<?php
namespace App\Controller;
use Doctrine\Persistence\ManagerRegistry;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Psr\Log\LoggerInterface;
use App\Entity\Users;
use App\Entity\Booking as Booking;
use App\Entity\Payment as Payment;
use App\Form\Type\BookingType;
class BookingController extends AbstractController
{
public function __construct(private ManagerRegistry $doctrine, private LoggerInterface $logger, private TranslatorInterface $trans)
{
}
#[Route('/', name: 'homepage')]
#[Route('/sales', name: 'sales')]
public function index(Request $request): Response
{
//$this->loggedUser = $this->getUser();
//dd($this->loggedUser);
return $this->render('booking/list.html.twig', [
'controller_name' => 'BookingController',
'list' => 'booking',
'showPaid' => $request->get('showPaid')
]);
}
#[Route('/sales/booking/edit/{id}', name: 'editBooking')]
public function displayEditAction($id)
{
$this->loggedUser = $this->getUser();
$em = $this->doctrine->getManager();
$booking = $em->getRepository('App\Entity\Booking')->find($id);
$ref = $booking->createRef();
if ($booking->getCreatedby()->getUserid() == $this->loggedUser->getUserid() || $this->isGranted('ROLE_ADMIN'))
{
$form = $this->createForm(BookingType::class, $booking, ['loggedUser' => $this->loggedUser, 'disabled' => true]);
$office = $booking->getBookingoffice();
$link = $this->getBookingLink($office, $ref);
return $this->render('booking/edit.html.twig', array('form' => $form->createView(), 'booking' => $booking, 'user' => $this->loggedUser, 'link' => $link));
}
else
{
return $this->render('nopermissions.html.twig', array('showHead' => $request = !$this->getRequest()->isXmlHttpRequest()));
}
}
#[Route('/sales/booking/add', name: 'addBooking')]
public function displayAddAction()
{
$this->loggedUser = $this->getUser();
$em = $this->doctrine->getManager();
if ($this->isGranted('ROLE_SALES') || $this->isGranted('ROLE_ADMIN'))
{
$booking = new Booking();
$booking->setBookingDate(new \DateTime());
$form = $this->createForm(BookingType::class, $booking, ['loggedUser' => $this->loggedUser, 'disabled' => false]);
return $this->render('booking/edit.html.twig', array('form' => $form->createView(), 'user' => $this->loggedUser ));
}
else
{
return $this->render('nopermissions.html.twig', array('showHead' => $request = !$this->getRequest()->isXmlHttpRequest()));
}
}
#[Route('/sales/booking/save/{id}', name: 'saveBooking')]
public function saveAction($id, Request $request)
{
$this->logger->info('Save booking, id = '.$id);
$this->loggedUser = $this->getUser();
$em = $this->doctrine->getManager();
if ($request->isMethod('POST'))
{
$frmData = $request->get('booking');
if ($id != 'new' && $id != null)
{
$this->logger->info('Edit existing booking');
$findPayment = $id;
$booking = $em->getRepository('App\Entity\Booking')->find($id);
$booking->setLastupdatedon(new \DateTime());
$booking->setLastupdatedby($this->loggedUser);
if (!is_numeric($frmData['customer']))
$frmData['customer'] = $booking->getCustomer()->getCustomerid();
if (!is_numeric($frmData['departureairport']))
$frmData['departureairport'] = $booking->getDepartureairport()->getAirportid();
if (!is_numeric($frmData['arrivalairport']))
$frmData['arrivalairport'] = $booking->getArrivalairport()->getAirportid();
if (!is_numeric($frmData['bookingaircraft']))
$frmData['bookingaircraft'] = $booking->getBookingaircraft()->getAircraftid();
$request->set('booking', $frmData);
}
else
{
$this->logger->info('Create new booking');
$findPayment = false;
$booking = new Booking();
$booking->setCreatedon(new \DateTime());
$booking->setCreatedby($this->loggedUser);
$booking->setLastupdatedon(new \DateTime());
$booking->setLastupdatedby($this->loggedUser);
$booking->setBookingoffice($this->loggedUser->getOffice());
$booking->setTotalprice($frmData['totalprice']);
$booking->setPaid(0);
}
$form = $this->createForm(BookingType::class, $booking, ['loggedUser' => $this->loggedUser, 'disabled' => false]);
$form->handleRequest($request);
if ($form->isValid())
{
if (($this->loggedUser->getUserid()==$booking->getCreatedby()->getUserid() || $id=='new'))
{
$booking = $form->getData();
$em = $this->doctrine->getManager();
$payment = $em->getRepository('App\Entity\Payment')->findOneByBooking($findPayment);
if (!$payment)
$payment=new Payment();
$payment->setPaymentgateway($booking->getBookingoffice()->getPaymentgateway());
$payment->setBooking($booking);
$payment->setUpdated( new \DateTime() );
$em->persist($booking);
$em->persist($payment);
$em->flush();
$office = $this->loggedUser->getOffice();
$officeName = $office->getOfficename();
$ref = $booking->createRef();
$link = $this->getBookingLink($booking->getBookingoffice(), $ref);
//$this->mailConfirmationLink($booking,$link);
//BE 180928: SafeKey is no longer required as per instructions from ACS
//ben 150105: check to see if this is for the London/New York office, and if so, if SafeKey is accepted in the customer's country
/*if ($office->getSafekeycheck()) {
//check the customer country
$custCountry = $booking->getCustomer()->getCustomercountry();
$safekeyResponse = $custCountry->getAmexsafekey();
}
else*/
$safekeyResponse = true;
$linkMsg = '<a href="'.$link.'" target="_blank">Confirmation Link</a><br/><br/><input type="text" class="linkBox" value="'.$link.'" />';
$return = array('success' => 1, 'link' => $linkMsg, 'safekeymsg' => $safekeyResponse ? '' : $this->trans->trans('<strong>WARNING</strong><br /><br />Safekey is not supported in '.$custCountry->getName().'.<br />If customer is paying by AMEX, please refer to the SOP on the intranet.'), 'safekey' => $safekeyResponse);
} else
$return = array('success' => 0, 'msg' => $this->trans->trans('You do not have permission to save this Booking'));
}
else
{
$this->logger->info('Invalid form data');
$errors = $form->getErrors(true, false);
$this->logger->info('Form error count: '.count($errors));
$errorsString = (string) $errors;
/*$errors = $this->get('validator')->validate($booking);
$this->logger->info('Validator error count: '.count($errors));
$errorsString .= (string) $errors;*/
$return = array('success'=>0, 'msg'=>$errorsString);
}
}
$response = new Response(json_encode($return));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
private function getBookingLink($office, $ref)
{
$this->logger = $this->logger;
$link = $this->generateUrl('bookingLink', array('ref' => $ref), UrlGeneratorInterface::ABSOLUTE_URL);
$officeDomain = $office->getOfficedomain();
//echo($officeDomain.'<br />');
//ben 150331: if the booking office is The Travel Division, update the confirmation link URL
//$this->logger->info('getBookingLink: '.$link);
if (!strpos($link, $officeDomain))
{
$link = str_replace($_SERVER['SERVER_NAME'], $officeDomain, $link);
$this->logger->info('getBookingLink: Swap officeDomain: '.$link);
}
//echo($link.'<br />');
return $link;
}
private function mailConfirmationLink($booking, $link)
{
$customer = $booking->getCustomer();
$user = $booking->getCreatedby();
$recipient = trim($customer->getFirstname().' '.$customer->getSurname());
$sender = trim($user->getFirstname().' '.$user->getSurname());
$from = $this->getParameter('mailfrom');
$message = \Swift_Message::newInstance()
->setContentType('text/html')
->setSubject('Booking Confirmation')
->setFrom($from)
->setTo($customer->getEmail())
->setBody(
$this->renderView
(
'emails/ConfirmationLink.txt.twig',
array(
'recipient' => $recipient,
'sender'=> $sender,
'booking'=>$booking,
'link'=>$link,
)
)
);
return $this->get('mailer')->send($message);
}
}