src/Uniski/CommerceBundle/Event/CommerceSubscriber.php line 284

Open in your IDE?
  1. <?php
  2. namespace Uniski\CommerceBundle\Event;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Uniski\CommerceBundle\Entity\Booking;
  5. use Uniski\CommerceBundle\Event\BookingEvent;
  6. use Uniski\CommerceBundle\Event\PaymentEvent;
  7. use Uniski\CommerceBundle\Service\BookingManager;
  8. use Uniski\CommerceBundle\Service\PaymentManager;
  9. use Uniski\UserBundle\Service\NotificationManager;
  10. use Uniski\ResourceBundle\Service\RoomStockManager;
  11. use Uniski\CommerceBundle\Exception\OverPaymentException;
  12. use Uniski\CommerceBundle\Service\BookingDocumentService;
  13. use Uniski\ResourceBundle\Exception\StockUpdateException;
  14. use Uniski\HotelBedsBundle\Booking\DomainModel\BookingService;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use HotelBedsBundle\Booking\DomainModel\InvalidBookingException;
  17. use HotelBedsBundle\Booking\DomainModel\BookingNotConfirmedException;
  18. /**
  19. * Commerce subscriber
  20. */
  21. class CommerceSubscriber implements EventSubscriberInterface
  22. {
  23.   protected $notificationManager;
  24.   protected $stock;
  25.   protected $bookingManager;
  26.   protected $payment;
  27.   protected $em;
  28.   protected $bookingDocumentService;
  29.   function __construct(NotificationManager    $nm,
  30.                        RoomStockManager       $sm,
  31.                        BookingManager         $bm,
  32.                        PaymentManager         $pm,
  33.                        EntityManagerInterface          $em,
  34.                        BookingDocumentService $bookingDocumentService,
  35.                        BookingService         $bookingService
  36.   ) {
  37.     $this->notificationManager    $nm;
  38.     $this->stock                  $sm;
  39.     $this->bookingManager         $bm;
  40.     $this->payment                $pm;
  41.     $this->em                     $em;
  42.     $this->bookingDocumentService $bookingDocumentService;
  43.     $this->bookingService         $bookingService;
  44.   }
  45.   public static function getSubscribedEvents()
  46.   {
  47.     return [
  48.       'booking.saved' => 'onBookingSaved',
  49.       'payment.payed'  => 'onPaymentPayed',
  50.       'payment.failed' => 'onPaymentFailed',
  51.     ];
  52.   }
  53.   public function onBookingSaved(BookingEvent $event)
  54.   {
  55.     $booking $event->getBooking();
  56.     $info=$booking->getInfo();
  57.     $referenceIsset=isset($info['reference']);
  58.     if (($booking->getHotel()->getSource() == "CLOUDHOSPITALITY" || (substr($booking->getHotel()->getSource(), 02) == "TG") && !$referenceIsset) && $booking->getPaymentMethod() == Booking::PAYMENT_TRANSFER && $booking->getStatus() != Booking::STATUS_DELETED) {
  59.           try {
  60.               $status $this->bookingService->confirmBooking($booking);
  61.           } catch (StockUpdateException $e) {
  62.               $message '<p><strong>¡ERROR DEL SISTEMA!</strong>:<br>';
  63.               $message .= 'Se ha realizado un pago cuando no ha habia cupos suficientes o ha habido algún problema con el sistema de cupos<br>';
  64.               $message .= 'Revisa la reserva ' $booking->getToken() . '<br>';
  65.               $message .= 'Mas detalles: <br>' $e->getMessage() . '</p>';
  66.               $this->notificationManager->notifyAdmin('default'$message);
  67.               $this->bookingManager->logHistory($booking$message);
  68.           } catch (InvalidBookingException $e) {
  69.               $message '<p><strong>¡ERROR DEL SISTEMA!</strong>:<br>';
  70.               $message .= 'Se ha realizado un pago pero la información la reserva en HOTELBEDS es erronea<br>';
  71.               $message .= 'Finaliza la reserva ' $booking->getToken() . '<br>';
  72.               $this->notificationManager->notifyAdmin('default'$message);
  73.               $this->bookingManager->logHistory($booking$message);
  74.           } catch (BookingNotConfirmedException $e) {
  75.               $message '<p><strong>¡ERROR DE HOTELBEDS!</strong>:<br>';
  76.               $message .= 'Se ha realizado un pago pero HOTELBEDS no ha confirmado la reserva ' $e->status() . '<br>';
  77.               $message .= 'Finaliza la reserva manualmente' $booking->getToken() . '<br>';
  78.               $this->notificationManager->notifyAdmin('default'$message);
  79.               $this->bookingManager->logHistory($booking$message);
  80.           } catch (\Exception $e) {
  81.               $message '<p><strong>¡ERROR DEL SISTEMA!</strong>:<br>';
  82.               $message .= 'Ha habido algun error durante la confirmación de la reserva<br>';
  83.               $message .= 'Revisa la reserva, contacta con los clientes y finaliza la reserva manualmente si es posible<br>';
  84.               $message .= 'Reserva ' $booking->getToken() . '<br>';
  85.               $message .= 'Mas detalles: <br>' $e->getMessage() . '</p>';
  86.               $this->notificationManager->notifyAdmin('default'$message);
  87.               $this->bookingManager->logHistory($booking$message);
  88.           }
  89.       }
  90.     if ($event->getPreviousStatus()) {
  91.       if ($event->getComments()) $message $event->getComments();
  92.       else $message "Cambio de estado de " $event->getPreviousStatus() . " a " $booking->getStatus();
  93.       $this->bookingManager->logHistory($booking$message);
  94.     } else {
  95.       if ($event->getComments()) $message $event->getComments();
  96.       else $message "Se ha creado la reserva";
  97.       if ($booking->getStatus() == Booking::STATUS_PRE_BOOKED) {
  98.         //Send notification to administator when it's prebooked
  99.         $this->notificationManager->notifyAdmin(NotificationManager::ADMIN_BOOKING_PRE_BOOKED$booking);
  100.         //Send notification to user to thanks user with info and bonds attachments
  101.         $this->notificationManager->notifyUser(NotificationManager::USER_BOOKING_PRE_BOOKED$booking->getUser(), $booking);
  102.       }
  103.       if ($booking->getPaymentMethod() == Booking::PAYMENT_TRANSFER) {
  104.        //Send notification to administator when it's prebooked
  105.        $this->notificationManager->notifyAdmin(NotificationManager::ADMIN_BOOKING_TRANSFER$booking);
  106.        //Send notification to user to thanks user with info and bonds attachments
  107.        $this->notificationManager->notifyUser(NotificationManager::USER_BOOKING_TRANSFER$booking->getUser(), $booking); 
  108.       }
  109.       $this->bookingManager->logHistory($booking$message);
  110.     }
  111.   }
  112.   public function onPaymentPayed(PaymentEvent $event)
  113.   {
  114.     $booking $event->getTransaction()->getBooking();
  115.     $firstTimePayed $booking->getPayed() == 0;
  116.     try {
  117.       
  118.       $booking->addPayed($event->getAmount());
  119.     } catch(OverPaymentException $e) {
  120.       $message  '<p><strong>¡ERROR DEL SISTEMA!</strong>:<br>';
  121.       $message .= 'Se ha realizado un pago superior al valor pendiente de pago<br>';
  122.       $message .= 'Revisa la reserva ' $booking->getToken() .'<br>';
  123.       $message .= 'Más detalles: <br>' $e->getMessage() .'</p>';
  124.       $this->notificationManager->notifyAdmin('default'$message);
  125.       $this->bookingManager->logHistory($booking$message);
  126.     }
  127.     //Si es el primer cop que paga reserva cupo
  128.     if ($firstTimePayed) {
  129.       try {
  130.         $status $this->bookingService->confirmBooking($booking);
  131.       } catch(StockUpdateException $e) {
  132.         $message  '<p><strong>¡ERROR DEL SISTEMA!</strong>:<br>';
  133.         $message .= 'Se ha realizado un pago cuando no ha habia cupos suficientes o ha habido algún problema con el sistema de cupos<br>';
  134.         $message .= 'Revisa la reserva ' $booking->getToken() .'<br>';
  135.         $message .= 'Mas detalles: <br>' $e->getMessage() .'</p>';
  136.         $this->notificationManager->notifyAdmin('default'$message);
  137.         $this->bookingManager->logHistory($booking$message);
  138.       } catch (InvalidBookingException $e) {
  139.         $message  '<p><strong>¡ERROR DEL SISTEMA!</strong>:<br>';
  140.         $message .= 'Se ha realizado un pago pero la información la reserva en HOTELBEDS es erronea<br>';
  141.         $message .= 'Finaliza la reserva ' $booking->getToken() .'<br>'
  142.         $this->notificationManager->notifyAdmin('default'$message);
  143.         $this->bookingManager->logHistory($booking$message);
  144.       } catch (BookingNotConfirmedException $e) {
  145.         $message  '<p><strong>¡ERROR DE HOTELBEDS!</strong>:<br>';
  146.         $message .= 'Se ha realizado un pago pero HOTELBEDS no ha confirmado la reserva ' $e->status() . '<br>';
  147.         $message .= 'Finaliza la reserva manualmente' $booking->getToken() .'<br>'
  148.         $this->notificationManager->notifyAdmin('default'$message);
  149.         $this->bookingManager->logHistory($booking$message);
  150.       } catch (\Exception $e) {
  151.         $message  '<p><strong>¡ERROR DEL SISTEMA!</strong>:<br>';
  152.         $message .= 'Ha habido algun error durante la confirmación de la reserva<br>';
  153.         $message .= 'Revisa la reserva, contacta con los clientes y finaliza la reserva manualmente si es posible<br>';        
  154.         $message .= 'Reserva ' $booking->getToken() .'<br>';
  155.         $message .= 'Mas detalles: <br>' $e->getMessage() .'</p>';
  156.         $this->notificationManager->notifyAdmin('default'$message);
  157.         $this->bookingManager->logHistory($booking$message);
  158.       }
  159.     }
  160.     
  161.     //Update booking status
  162.     if ($booking->getPending() > 0) {
  163.       //////////////////////////////////////////////////////
  164.       ///                                                ///
  165.       ///  the user has NOT payed totally, pending debt  ///
  166.       ///    SO WE DO NOT ISSUE BONDS AND INVOICE        ///
  167.       ///                                                ///
  168.       //////////////////////////////////////////////////////
  169.       $booking->setStatus(Booking::STATUS_HALF_PAYED);
  170.       //Send notification to administator
  171.       $this->notificationManager->notifyAdmin(NotificationManager::ADMIN_BOOKING_PAYED$booking);
  172.       $this->notificationManager->notifyUser(NotificationManager::USER_BOOKING_PAYED,
  173.         $booking->getUser(),
  174.         $booking);
  175.       return  $this->bookingManager->save($booking);
  176.     }
  177.     //////////////////////////////////////////////////////
  178.     ///                                                ///
  179.     ///  the user has payed totally, no pending debt   ///
  180.     ///                                                ///
  181.     //////////////////////////////////////////////////////
  182.     $booking->setStatus(Booking::STATUS_PAYED);
  183.     //generate an invoice id and save documents
  184.     $this->bookingDocumentService->generateDocuments($booking);
  185.     //Send notification to administator
  186.     $this->notificationManager->notifyAdmin(NotificationManager::ADMIN_BOOKING_PAYED$booking);
  187.     //Send notification to user to thanks user with info and bonds and insurance attachments
  188.     $attachments = [$booking->getBond(), $booking->getInvoice()];
  189.     foreach ($booking->getMembers() as $member) {
  190.       if ($member->getInsurance()) {
  191.         //grouping by insurance id, in order to avoid duplicates
  192.         $insurance $member->getInsurance();
  193.         $attachments[$insurance->getId()] = $insurance->getDocument();
  194.       }
  195.     }
  196.     $this->notificationManager->notifyUser(NotificationManager::USER_BOOKING_PAYED,
  197.       $booking->getUser(),
  198.       $booking,
  199.       $attachments);
  200.     /*
  201.     // Disabled: Send notification to hotel
  202.     if ($booking->getHotel()->getEmail()) {
  203.       $this->notificationManager->notify(NotificationManager::HOTEL_BOOKING_PAYED,
  204.                                          $booking->getHotel()->getEmail(),
  205.                                          $booking);
  206.     }*/
  207.     $this->bookingManager->save($booking);
  208.   }
  209.   public function onPaymentFailed(PaymentEvent $event)
  210.   {
  211.     $booking $event->getTransaction()->getBooking();
  212.     
  213.     $message sprintf(
  214.       "Pago no completado en la reserva %s. Motivo: %s",
  215.       $event->getStatusMessage(),
  216.       $booking->getToken()
  217.     );
  218.     $this->bookingManager->logHistory($booking$message);
  219.     $this->notificationManager->notifyAdmin('default'$message);
  220.   }
  221. }