src/Controller/BookingController.php line 29

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