src/Controller/BookingController.php line 29

  1. <?php
  2. namespace App\Controller;
  3. use Doctrine\Persistence\ManagerRegistry;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Mailer\MailerInterface;
  8. use Symfony\Component\Routing\Attribute\Route;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. use Symfony\Contracts\Translation\TranslatorInterface;
  11. use Psr\Log\LoggerInterface;
  12. use App\Entity\Users;
  13. use App\Entity\Booking as Booking;
  14. use App\Entity\Payment as Payment;
  15. use App\Form\Type\BookingType;
  16. class BookingController extends AbstractController
  17. {
  18. private $loggedUser;
  19. public function __construct(
  20. private ManagerRegistry $doctrine,
  21. private LoggerInterface $logger,
  22. private TranslatorInterface $trans,
  23. private readonly MailerInterface $mailer,
  24. private string $bookingUrlSecret)
  25. {
  26. }
  27. #[Route('/', name: 'homepage')]
  28. #[Route('/sales', name: 'sales')]
  29. public function index(Request $request): Response
  30. {
  31. //$this->loggedUser = $this->getUser();
  32. //dd($this->loggedUser);
  33. return $this->render('booking/list.html.twig', [
  34. 'controller_name' => 'BookingController',
  35. 'list' => 'booking',
  36. 'showPaid' => $request->query->get('showPaid', 0)
  37. ]);
  38. }
  39. #[Route('/sales/booking/add', name: 'addBooking')]
  40. public function displayAddAction()
  41. {
  42. $this->loggedUser = $this->getUser();
  43. $em = $this->doctrine->getManager();
  44. if ($this->isGranted('ROLE_SALES') || $this->isGranted('ROLE_ADMIN'))
  45. {
  46. $booking = new Booking();
  47. $booking->setBookingDate(new \DateTime());
  48. $form = $this->createForm(BookingType::class, $booking, ['loggedUser' => $this->loggedUser, 'disabled' => false]);
  49. return $this->render('booking/edit.html.twig', array('form' => $form, 'user' => $this->loggedUser ));
  50. }
  51. else
  52. {
  53. return $this->render('nopermissions.html.twig', array('showHead' => $request = !$this->getRequest()->isXmlHttpRequest()));
  54. }
  55. }
  56. #[Route('/sales/booking/edit/{id}', name: 'editBooking')]
  57. public function displayEditAction($id)
  58. {
  59. $this->loggedUser = $this->getUser();
  60. $em = $this->doctrine->getManager();
  61. $booking = $em->getRepository('App\Entity\Booking')->find($id);
  62. $ref = $booking->createRef($this->bookingUrlSecret);
  63. if ($booking->getCreatedby()->getUserid() == $this->loggedUser->getUserid() || $this->isGranted('ROLE_ADMIN'))
  64. {
  65. $form = $this->createForm(BookingType::class, $booking, ['loggedUser' => $this->loggedUser, 'disabled' => true]);
  66. $office = $booking->getBookingoffice();
  67. $link = $this->getBookingLink($office, $ref);
  68. return $this->render('booking/edit.html.twig', array('form' => $form, 'booking' => $booking, 'user' => $this->loggedUser, 'link' => $link));
  69. }
  70. else
  71. {
  72. return $this->render('nopermissions.html.twig', array('showHead' => $request = !$this->getRequest()->isXmlHttpRequest()));
  73. }
  74. }
  75. #[Route('/sales/booking/save/{id}', name: 'saveBooking')]
  76. public function saveAction($id, Request $request)
  77. {
  78. $this->logger->info('Save booking, id = '.$id);
  79. $this->loggedUser = $this->getUser();
  80. $em = $this->doctrine->getManager();
  81. if ($request->isMethod('POST'))
  82. {
  83. $frmData = $request->request->all('booking');
  84. if ($id != 'new' && $id != null)
  85. {
  86. $this->logger->info('Edit existing booking');
  87. $findPayment = $id;
  88. $booking = $em->getRepository('App\Entity\Booking')->find($id);
  89. $booking->setLastupdatedon(new \DateTime());
  90. $booking->setLastupdatedby($this->loggedUser);
  91. if (!is_numeric($frmData['customer']))
  92. $frmData['customer'] = $booking->getCustomer()->getCustomerid();
  93. if (!is_numeric($frmData['departureairport']))
  94. $frmData['departureairport'] = $booking->getDepartureairport()->getAirportid();
  95. if (!is_numeric($frmData['arrivalairport']))
  96. $frmData['arrivalairport'] = $booking->getArrivalairport()->getAirportid();
  97. if (!is_numeric($frmData['bookingaircraft']))
  98. $frmData['bookingaircraft'] = $booking->getBookingaircraft()->getAircraftid();
  99. $request->request->set('booking', $frmData);
  100. }
  101. else
  102. {
  103. $this->logger->info('Create new booking');
  104. $findPayment = false;
  105. $booking = new Booking();
  106. $booking->setCreatedon(new \DateTime());
  107. $booking->setCreatedby($this->loggedUser);
  108. $booking->setLastupdatedon(new \DateTime());
  109. $booking->setLastupdatedby($this->loggedUser);
  110. $booking->setBookingoffice($this->loggedUser->getOffice());
  111. $booking->setTotalprice($frmData['totalprice']);
  112. $booking->setPaid(0);
  113. }
  114. $form = $this->createForm(BookingType::class, $booking, ['loggedUser' => $this->loggedUser, 'disabled' => false]);
  115. $form->handleRequest($request);
  116. if ($form->isValid())
  117. {
  118. if (($this->loggedUser->getUserid() == $booking->getCreatedby()->getUserid() || $id == 'new'))
  119. {
  120. $booking = $form->getData();
  121. $em = $this->doctrine->getManager();
  122. $payment = $em->getRepository('App\Entity\Payment')->findOneByBooking($findPayment);
  123. if (!$payment)
  124. $payment=new Payment();
  125. $payment->setPaymentgateway($booking->getBookingoffice()->getPaymentgateway());
  126. $payment->setBooking($booking);
  127. $payment->setUpdated( new \DateTime() );
  128. $em->persist($booking);
  129. $em->persist($payment);
  130. $em->flush();
  131. $office = $this->loggedUser->getOffice();
  132. $officeName = $office->getOfficename();
  133. $ref = $booking->createRef($this->bookingUrlSecret);
  134. $link = $this->getBookingLink($booking->getBookingoffice(), $ref);
  135. //$this->mailConfirmationLink($booking,$link);
  136. //BE 180928: SafeKey is no longer required as per instructions from ACS
  137. //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
  138. /*if ($office->getSafekeycheck()) {
  139. //check the customer country
  140. $custCountry = $booking->getCustomer()->getCustomercountry();
  141. $safekeyResponse = $custCountry->getAmexsafekey();
  142. }
  143. else*/
  144. $safekeyResponse = true;
  145. $linkMsg = '<a href="'.$link.'" target="_blank">Confirmation Link</a><br/><br/><input type="text" class="linkBox" value="'.$link.'" />';
  146. $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);
  147. } else
  148. $return = array('success' => 0, 'msg' => $this->trans->trans('You do not have permission to save this Booking'));
  149. }
  150. else
  151. {
  152. $this->logger->info('Invalid form data');
  153. $errors = $form->getErrors(true, false);
  154. $this->logger->info('Form error count: '.count($errors));
  155. $errorsString = (string) $errors;
  156. /*$errors = $this->get('validator')->validate($booking);
  157. $this->logger->info('Validator error count: '.count($errors));
  158. $errorsString .= (string) $errors;*/
  159. $return = array('success'=>0, 'msg'=>$errorsString);
  160. }
  161. }
  162. $response = new Response(json_encode($return));
  163. $response->headers->set('Content-Type', 'application/json');
  164. return $response;
  165. }
  166. private function getBookingLink($office, $ref)
  167. {
  168. $this->logger = $this->logger;
  169. $link = $this->generateUrl('bookingLink', array('ref' => $ref), UrlGeneratorInterface::ABSOLUTE_URL);
  170. $officeDomain = $office->getOfficedomain();
  171. //echo($officeDomain.'<br />');
  172. //ben 150331: if the booking office is The Travel Division, update the confirmation link URL
  173. //$this->logger->info('getBookingLink: '.$link);
  174. if (!strpos($link, $officeDomain))
  175. {
  176. $link = str_replace($_SERVER['SERVER_NAME'], $officeDomain, $link);
  177. $this->logger->info('getBookingLink: Swap officeDomain: '.$link);
  178. }
  179. //echo($link.'<br />');
  180. return $link;
  181. }
  182. private function mailConfirmationLink($booking, $link)
  183. {
  184. $customer = $booking->getCustomer();
  185. $user = $booking->getCreatedby();
  186. $recipient = trim($customer->getFirstname().' '.$customer->getSurname());
  187. $sender = trim($user->getFirstname().' '.$user->getSurname());
  188. $from = $this->getParameter('mailfrom');
  189. $message = \Swift_Message::newInstance()
  190. ->setContentType('text/html')
  191. ->setSubject('Booking Confirmation')
  192. ->setFrom($from)
  193. ->setTo($customer->getEmail())
  194. ->setBody(
  195. $this->renderView
  196. (
  197. 'emails/ConfirmationLink.txt.twig',
  198. array(
  199. 'recipient' => $recipient,
  200. 'sender'=> $sender,
  201. 'booking'=>$booking,
  202. 'link'=>$link,
  203. )
  204. )
  205. );
  206. return $this->mailer->send($message);
  207. }
  208. }