app/Customize/EventListener/ReservationListener.php line 51

Open in your IDE?
  1. <?php
  2. namespace Customize\EventListener;
  3. use Carbon\Carbon;
  4. use Eccube\Event\EventArgs;
  5. use Eccube\Event\EccubeEvents;
  6. use Eccube\Service\MailService;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  10. use Eccube\Entity\Order;
  11. use Eccube\Common\EccubeConfig;
  12. use Customize\Entity\ReservationActual;
  13. use Customize\Entity\ReservationInfo;
  14. use Symfony\Component\Mime\Address;
  15. use Eccube\Repository\BaseInfoRepository;
  16. use Symfony\Component\Mailer\MailerInterface;
  17. use Symfony\Component\Mime\Email;
  18. use Customize\Entity\OrderDetailAdditionalInfo;
  19. use Customize\Entity\TimeSlot;
  20. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  21. class ReservationListener implements EventSubscriberInterface
  22. {
  23.     protected $BaseInfo;
  24.     protected $mailer;
  25.     protected $mailService;
  26.     public function __construct(
  27.         public EntityManagerInterface $entityManager,
  28.         public SessionInterface $session,
  29.         public EccubeConfig $eccubeConfig,
  30.         public \Twig\Environment $twig,
  31.         BaseInfoRepository $baseInfoRepository,
  32.         MailerInterface $mailer,
  33.         MailService $mailService
  34.     ) {
  35.         $this->BaseInfo $baseInfoRepository->get();
  36.         $this->mailer $mailer;
  37.         $this->mailService $mailService;
  38.     }
  39.     /**
  40.      * Update or create reservation actual
  41.      *
  42.      * @param EventArgs $event
  43.      * @return void
  44.      */
  45.     public function onUpdateOrCreateReservationActual(EventArgs $event)
  46.     {
  47.         $reservationInfo $event->getArgument('reservationInfo');
  48.         $reservationActual $this->entityManager->getRepository(ReservationActual::class)
  49.             ->findOneBy(
  50.                 [
  51.                     'visit_day' => $reservationInfo->getVisitDay(),
  52.                     'shop' => $reservationInfo->getShop(),
  53.                     'time_slot_id' => $reservationInfo->getTimeSlotId()
  54.                 ]
  55.             );
  56.         $usePeople $reservationInfo->getUsePeople();
  57.         if ($reservationActual) {
  58.             $quantityReservation $reservationActual->getQuantity() ? $reservationActual->getQuantity() + 0;
  59.             $quantityCustomerReservation $reservationActual->getAmountCustomerReservation() ? $reservationActual->getAmountCustomerReservation() + $usePeople 0;
  60.             $reservationActual->setQuantity($quantityReservation);
  61.             $reservationActual->setAmountCustomerReservation($quantityCustomerReservation);
  62.         } else {
  63.             $timeSlot $this->entityManager->getRepository(TimeSlot::class)->findOneBy(
  64.                 ['id' => $reservationInfo->getTimeSlotId()]
  65.             );
  66.             $visitDay $reservationInfo->getVisitDay();
  67.             $reservationActual = new ReservationActual();
  68.             $reservationActual->setTimeSlotId($timeSlot);
  69.             $reservationActual->setShop($reservationInfo->getShop());
  70.             $reservationActual->setVisitDay($visitDay);
  71.             $reservationActual->setQuantity(1);
  72.             $reservationActual->setAmountCustomerReservation($usePeople);
  73.             $this->entityManager->persist($reservationActual);
  74.         }
  75.         $this->entityManager->flush();
  76.     }
  77.     /**
  78.      * Send email after register reservation
  79.      *
  80.      * @param EventArgs $event
  81.      * @return void
  82.      */
  83.     public function onSendMailReservationActual(EventArgs $event)
  84.     {
  85.         log_info('Start sending emails to register for reservations');
  86.         $reservationInfo $event->getArgument('reservationInfo');
  87.         $templateFileName '/Mail/register_reservation.twig';
  88.         $arrayParam = [
  89.             'visit_day' => $reservationInfo->getVisitDay(),
  90.             'time_start' => $reservationInfo->getTimeSlotId()->getStartTime(),
  91.             'time_end' => $reservationInfo->getTimeSlotId()->getEndTime(),
  92.             'customer_name' => $reservationInfo->getCustomerName01() . ' ' $reservationInfo->getCustomerName02(),
  93.             'customer_kana' => $reservationInfo->getCustomerKana01() . ' ' $reservationInfo->getCustomerKana02(),
  94.             'tel' => $reservationInfo->getTel(),
  95.             'email' => $reservationInfo->getEmail(),
  96.             'item' => $reservationInfo->getItem(),
  97.             'shop' => $reservationInfo->getShop(),
  98.             'use_people' => $reservationInfo->getUsePeople(),
  99.             'use_day' => $reservationInfo->getUseDay(),
  100.             'purpose' => $reservationInfo->getPurpose(),
  101.             'height' => $reservationInfo->getHeight(),
  102.             'budget' => $reservationInfo->getBudget(),
  103.             'characteristic' => $reservationInfo->getCharacteristic(),
  104.             'note' => $reservationInfo->getNote(),
  105.             'body_type' => $reservationInfo->getBodyType() ? json_decode($reservationInfo->getBodyType()) : $reservationInfo->getBodyType(),
  106.         ];
  107.         $body $this->twig->render($templateFileName$arrayParam);
  108.         $message = (new Email())
  109.             ->subject(ReservationInfo::RESERVATION_MAIL_SUBJECT)
  110.             ->from(new Address($this->BaseInfo->getEmail01(), $this->BaseInfo->getShopName()))
  111.             ->to($reservationInfo->getEmail())
  112.             ->replyTo($this->BaseInfo->getEmail03())
  113.             ->returnPath($this->BaseInfo->getEmail04());
  114.         $htmlFileName $this->mailService->getHtmlTemplate($templateFileName);
  115.         if (!is_null($htmlFileName)) {
  116.             $htmlBody $this->twig->render($htmlFileName$arrayParam);
  117.             $message
  118.                 ->text($body)
  119.                 ->html($htmlBody);
  120.         } else {
  121.             $message->html($body);
  122.         }
  123.         try {
  124.             $this->mailer->send($message);
  125.         } catch (TransportExceptionInterface $e) {
  126.             log_critical('Show error send email reservation', [$e->getMessage()]);
  127.         }
  128.     }
  129.     /**
  130.      * Check date visit is full slot
  131.      *
  132.      * @param EventArgs $event
  133.      * @return void
  134.      */
  135.     public function onCheckReservationIsFullSlot(EventArgs $event)
  136.     {
  137.         $reservationInfo $event->getArgument('reservationInfo');
  138.         $quantityMaxSetting $event->getArgument('quantity_max_setting');
  139.         $timeSlotId $reservationInfo->getTimeSlotId()->getId();
  140.         $shopName $reservationInfo->getShop();
  141.         $timeEnd Carbon::parse($reservationInfo->getTimeSlotId()->getEndTime())->format('H:i');
  142.         $dateVisit Carbon::parse($reservationInfo->getVisitDay())->format('Y-m-d');
  143.         $totalOrderTypeVisit $this->totalOrderTypeVisit($shopName$dateVisit$timeEnd);
  144.         $queryBuilder $this->entityManager->createQueryBuilder();
  145.         $queryBuilder->select('COUNT(DISTINCT re.id) as totalReservation')
  146.             ->from(ReservationInfo::class, 're')
  147.             ->where('re.time_slot_id = :time_slot_id')
  148.             ->andWhere('re.visit_day = :visit_day')
  149.             ->andWhere('re.shop = :shop_name')
  150.             ->setParameter('time_slot_id'$timeSlotId)
  151.             ->setParameter('visit_day'$dateVisit)
  152.             ->setParameter('shop_name'$shopName);
  153.         $results $queryBuilder->getQuery()->getResult();
  154.         return  $event->setArgument('is_full_slot'$results[0]['totalReservation'] + $totalOrderTypeVisit >= $quantityMaxSetting);
  155.     }
  156.     /**
  157.      * Count order type visit
  158.      *
  159.      * @param [string] $shopName
  160.      * @param [date] $dateVisit
  161.      * @param [time] $timeEnd
  162.      * @return integer
  163.      */
  164.     private function totalOrderTypeVisit($shopName$dateVisit$timeEnd): int
  165.     {
  166.         $queryBuilder $this->entityManager->createQueryBuilder();
  167.         $queryBuilder->select('COUNT(odai.id) as record_count_visit')
  168.             ->from(OrderDetailAdditionalInfo::class, 'odai')
  169.             ->leftJoin(Order::class, 'otb''WITH''odai.order_id = otb.id')
  170.             ->where('otb.OrderStatus <> 3')
  171.             ->andWhere('odai.wear_date = :date')
  172.             ->andWhere('odai.order_type = :order_type')
  173.             ->andWhere('odai.visit_store = :shop')
  174.             ->andWhere('SUBSTRING(odai.time_departure, 1, 5) = SUBSTRING(:end_time, 1, 5)')
  175.             ->setParameter('order_type''visit')
  176.             ->setParameter('end_time'$timeEnd)
  177.             ->setParameter('shop'$shopName)
  178.             ->setParameter('date'$dateVisit);
  179.         $results $queryBuilder->getQuery()->getResult();
  180.         return $results[0]['record_count_visit'];
  181.     }
  182.     /**
  183.      * Initialize event
  184.      *
  185.      * @return void
  186.      */
  187.     public static function getSubscribedEvents()
  188.     {
  189.         return [
  190.             EccubeEvents::FRONT_UPDATE_RESERVATION_ACTUAL => 'onUpdateOrCreateReservationActual',
  191.             EccubeEvents::FRONT_SEND_MAIL_AFTER_REGISTER_RESERVATION => 'onSendMailReservationActual',
  192.             EccubeEvents::FRONT_RESERVATION_CHECK_IS_FULL_SLOT => 'onCheckReservationIsFullSlot',
  193.         ];
  194.     }
  195. }