/**
  * Booking Action
  * @param Request $request
  * @param Accommodation $accommodation
  * @return \Symfony\Component\HttpFoundation\RedirectResponse
  */
 public function bookingAction(Request $request, Accommodation $accommodation)
 {
     $em = $this->getDoctrine()->getManager();
     $session = $request->getSession();
     try {
         # User
         $role = $em->getRepository('UserBundle:Role')->findOneByName('guest');
         $unit = $em->getRepository('AppBundle:Unit')->find($request->request->get('unit'));
         $checkIn = new \DateTime($request->request->get('checkIn'));
         $checkOut = new \DateTime($request->request->get('checkOut'));
         $price = $request->request->get('price');
         $bookingForm = $this->createForm(new RegistrationType(array()));
         $bookingForm->handleRequest($request);
         # Array with form fields
         $data = $bookingForm->getData();
         if (!$this->container->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')) {
             $user = new User();
             $user->setRoles($role);
             $user->setUsername($data['username']);
             $user->setName($data['name']);
             $user->setSurename($data['surename']);
             $user->setEmail($data['email']);
             $user->setPassword($data['password']);
             $user->setIsActive(0);
             $user->encryptPassword();
             # User validation
             $validator = $this->get('validator');
             $errors = $validator->validate($user);
             $errorsString = (string) $errors;
             if (count($errors) > 0) {
                 $response = array();
                 $response['error'] = $errorsString;
                 $session->getFlashBag()->add('formErrors', (string) $errorsString);
                 return $this->redirect($this->generateUrl('app_accommodation_single', array('id' => $accommodation->getId())));
             }
         } else {
             $user = $this->getUser();
         }
         # Booking
         $booking = new Booking();
         $booking->setUnit($unit);
         $booking->setStatus(0);
         $booking->setPrice($price);
         $booking->setUser($user);
         $booking->setFromDate($checkIn);
         $booking->setToDate($checkOut);
         # Booking validation
         $validator = $this->get('validator');
         $errorsBooking = $validator->validate($booking);
         $errorsString = (string) $errorsBooking;
         if (count($errorsBooking) > 0) {
             $response = array();
             $response['error'] = $errorsString;
             $session->getFlashBag()->add('formErrors', (string) $errorsString);
             return $this->redirect($this->generateUrl('app_accommodation_single', array('id' => $accommodation->getId())));
         }
         # Booking not allowed
         if (!$this->get('bookingService')->bookingAllowed($booking)) {
             return $this->redirect($this->generateUrl('user_403'));
         }
         $em->persist($user);
         $em->flush();
         $em->persist($booking);
         $em->flush();
         $session->getFlashBag()->add('formSuccess', $this->get('translator')->trans('booking_success'));
         $this->bookingRequestEmail($accommodation->getUser(), $booking, 'host');
         $this->bookingRequestEmail($user, $booking, 'guest');
         if ($user->getIsActive()) {
             return $this->redirect($this->generateUrl('app_profile'));
         } else {
             return $this->redirect($this->generateUrl('app_home'));
         }
     } catch (ExportException $e) {
         $session->getFlashBag()->add('formError', $e->getMessage());
         return $this->redirect($this->generateUrl('app_home'));
     }
 }
 /**
  * @Route("/booking/confirmation/{uuid}", name="booking-confirmation")
  */
 public function confirmationAction(Request $request, $uuid)
 {
     $inq = $this->getDoctrineRepo('AppBundle:Inquiry')->findOneByUuid($uuid);
     // sanity check
     if ($inq == null) {
         throw $this->createNotFoundException();
     }
     if ($inq->getBooking() !== null) {
         // booking already confirmed
         return new Response('', 403);
         // TODO: display a nice message to the user?
     }
     $data = array('uuid' => $uuid);
     $form = $this->createFormBuilder($data, array('constraints' => array(new Callback(array($this, 'validateDiscountCode')))))->add('agree', 'checkbox', array('required' => false, 'constraints' => array(new NotBlank())))->add('discountCode', 'text', array('required' => false))->add('uuid', 'hidden')->getForm();
     $form->handleRequest($request);
     if ($form->isValid()) {
         $data = $form->getData();
         $bk = new Booking();
         $bk->setInquiry($inq);
         $bk->setStatus(Booking::STATUS_BOOKED);
         // save booking
         $em = $this->getDoctrine()->getManager();
         $em->persist($bk);
         $em->flush();
         // calcualte discount
         if (!empty($data['discountCode'])) {
             $dcode = $this->getDoctrineRepo('AppBundle:DiscountCode')->findOneByCode($data['discountCode']);
             $dcode->setStatus(DiscountCode::STATUS_USED);
             $dcode->setInquiry($inq);
             $p = $inq->getPrice() - 5;
             $inq->setPrice($p);
             $this->getDoctrine()->getManager()->flush();
         }
         // send email to provider & user
         //<editor-fold>
         $provider = $inq->getEquipment()->getUser();
         $from = array($this->getParameter('mailer_fromemail') => $this->getParameter('mailer_fromname'));
         $emailHtml = $this->renderView('Emails/booking_confirmation_provider.html.twig', array('mailer_image_url_prefix' => $this->getParameter('mailer_image_url_prefix'), 'provider' => $provider, 'inquiry' => $inq, 'equipment' => $inq->getEquipment()));
         $message = Swift_Message::newInstance()->setSubject('Du hast soeben eine Anfrage erhalten')->setFrom($from)->setTo($provider->getEmail())->setBody($emailHtml, 'text/html');
         $this->get('mailer')->send($message);
         if ($inq->getUser() !== null) {
             $email = $inq->getUser()->getEmail();
         } else {
             $email = $inq->getEmail();
         }
         $emailHtml = $this->renderView('Emails/booking_confirmation_user.html.twig', array('mailer_image_url_prefix' => $this->getParameter('mailer_image_url_prefix'), 'provider' => $provider, 'inquiry' => $inq, 'equipment' => $inq->getEquipment()));
         $message = Swift_Message::newInstance()->setSubject('Du hast soeben eine Anfrage erhalten')->setFrom($from)->setTo($email)->setBody($emailHtml, 'text/html');
         $this->get('mailer')->send($message);
         //</editor-fold>
         return $this->redirectToRoute('rentme');
     }
     return $this->render('booking/confirmation.html.twig', array('inquiry' => $inq, 'form' => $form->createView()));
 }