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