src/Controller/CateringController.php line 147

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Service\TessituraBundle\AddNotAvailableException;
  4. use App\Service\TessituraBundle\CartService;
  5. use App\Service\TessituraBundle\CateringService;
  6. use App\Service\TessituraBundle\CateringServiceException;
  7. use App\Service\TessituraBundle\Entity\CateringItemPerformance;
  8. use App\Service\TessituraBundle\Item\CartItemCatering;
  9. use App\Service\TessituraBundle\Item\CateringItemPerformance as ItemCateringItemPerformance;
  10. use App\Service\TessituraSDK\Entity\Remote\Performance;
  11. use App\Service\TessituraSDK\Entity\Remote\ProductionSeason;
  12. use App\Service\TessituraSDK\Resource\TXN\Performances;
  13. use App\Service\TessituraSDK\Resource\TXN\ProductionSeasons;
  14. use App\Service\TessituraSDK\TessituraClient;
  15. use App\Service\TessituraSDK\TessituraClientException;
  16. use Psr\Cache\CacheItemPoolInterface;
  17. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  20. use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
  21. use Symfony\Component\DependencyInjection\ContainerInterface;
  22. use Symfony\Component\HttpFoundation\JsonResponse;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\HttpFoundation\Response;
  25. use Symfony\Component\Serializer\Serializer;
  26. use Symfony\Component\Serializer\SerializerInterface;
  27. use Symfony\Contracts\Translation\TranslatorInterface;
  28. use Symfony\Component\Validator\Constraints\DateTime;
  29. class CateringController extends AbstractController
  30. {
  31.     /**
  32.      * @var TessituraClient
  33.      */
  34.     private $client;
  35.     /**
  36.      * @var Serializer
  37.      */
  38.     private $serializer;
  39.     /**
  40.      * @var CacheItemPoolInterface|TagAwareAdapterInterface
  41.      */
  42.     private $cache;
  43.     /**
  44.      * @var TranslatorInterface
  45.      */
  46.     private $translator;
  47.     /**
  48.      * @var ParameterBagInterface
  49.      */
  50.     private $params;
  51.     /**
  52.      * CartController constructor.
  53.      *
  54.      * @param TessituraClient $client
  55.      * @param SerializerInterface $serializer
  56.      * @param ContainerInterface $container
  57.      * @param TranslatorInterface $translator
  58.      * @param ParameterBagInterface $params
  59.      */
  60.     public function __construct(
  61.         TessituraClient $client,
  62.         SerializerInterface $serializer,
  63.         ContainerInterface $container,
  64.         TranslatorInterface $translator,
  65.         ParameterBagInterface $params
  66.     ) {
  67.         $this->setContainer($container);
  68.         $this->client $client;
  69.         $this->serializer $serializer;
  70.         $this->cache $container->get('cache.app');
  71.         $this->translator $translator;
  72.         $this->params $params;
  73.     }
  74.     /**
  75.      * @Route(
  76.      *     "/{_locale}/catering/{performanceId}/",
  77.      *     name="catering-performance",
  78.      *     defaults={
  79.      *          "_locale": "fi"
  80.      *     },
  81.      *     requirements={
  82.      *          "_locale": "fi|sv|en"
  83.      *     }
  84.      * )
  85.      * @param Request $request
  86.      * @param CateringService $cateringService
  87.      * @param $performanceId
  88.      *
  89.      * @return \Symfony\Component\HttpFoundation\Response
  90.      * @throws TessituraClientException
  91.      */
  92.     public function cateringPerformanceAction(Request $requestCateringService $cateringService$performanceId)
  93.     {
  94.         $compiled false;
  95.         try {
  96.             $performance $cateringService->getPerformance($performanceId);
  97.             $items $cateringService->getDefaultItems();
  98.             if ($items) {
  99.                 $orderedItems $cateringService->orderItems($items);
  100.                 $compiled $this->render('partial/catering-items-partial.html.twig', [
  101.                     'ordered_items' => $orderedItems,
  102.                     'items' => $items,
  103.                     'disabled' => true
  104.                 ]);
  105.             }
  106.         } catch (TessituraClientException $exception) {
  107.             if ($exception->getCode() === TessituraClientException::TESSITURA_NOT_FOUND) {
  108.                 throw $this->createNotFoundException();
  109.             }
  110.             throw $exception;
  111.         }
  112.         return $this->render('page/catering.html.twig', [
  113.             'items' => [],
  114.             'performance' => $performance,
  115.             'default_items' => $compiled instanceof Response $compiled->getContent() : ''
  116.         ]);
  117.     }
  118.     /**
  119.      * @Route(
  120.      *     "/{_locale}/catering/",
  121.      *     name="catering",
  122.      *     defaults={
  123.      *          "_locale": "fi"
  124.      *     },
  125.      *     requirements={
  126.      *          "_locale": "fi|sv|en"
  127.      *     }
  128.      * )
  129.      * @param Request $request
  130.      * @param CateringService $cateringService
  131.      *
  132.      * @return \Symfony\Component\HttpFoundation\Response
  133.      */
  134.     public function cateringAction(Request $requestCateringService $cateringService)
  135.     {
  136.         $compiled false;
  137.         $items $cateringService->getDefaultItems();
  138.         if ($items) {
  139.             $orderedItems $cateringService->orderItems($items);
  140.             $compiled $this->render('partial/catering-items-partial.html.twig', [
  141.                 'ordered_items' => $orderedItems,
  142.                 'items' => $items,
  143.                 'disabled' => true
  144.             ]);
  145.         }
  146.         return $this->render('page/catering.html.twig', [
  147.             'performance' => false,
  148.             'default_items' => $compiled instanceof Response $compiled->getContent() : ''
  149.         ]);
  150.     }
  151.     /**
  152.      * @Route(
  153.      *     "/{_locale}/catering/api/v1/catering-items/",
  154.      *     name="get-api-catering-items",
  155.      *     defaults={
  156.      *          "_locale": "fi"
  157.      *     },
  158.      *     requirements={
  159.      *          "_locale": "fi|sv|en"
  160.      *     },
  161.      *     methods={"GET"}
  162.      * )
  163.      * @param Request $request
  164.      * @param CateringService $cateringService
  165.      *
  166.      * @return JsonResponse
  167.      * @internal param CartService $cart
  168.      */
  169.     public function getItemsAction(Request $requestCateringService $cateringService)
  170.     {
  171.         $performanceId $request->get('performanceId');
  172.         $statuses json_decode($request->get('statuses'));
  173.         $performance $cateringService->getPerformance($performanceId);
  174.         $items $cateringService->getAvailable($performanceId);
  175.         $orderedItems $cateringService->orderItems($items);
  176.         $compiled $this->render('partial/catering-items-partial.html.twig', [
  177.             'performance' => $performance,
  178.             'ordered_items' => $orderedItems,
  179.             'items' => $items,
  180.             'pre_status' => $cateringService->getCorrespondingCateringMessage($statuses->pre ?? 0),
  181.             'int1_status' => $cateringService->getCorrespondingCateringMessage($statuses->int1 ?? 0),
  182.             'int2_status' => $cateringService->getCorrespondingCateringMessage($statuses->int2 ?? 0),
  183.             'post_status' => $cateringService->getCorrespondingCateringMessage($statuses->post ?? 0),
  184.         ]);
  185.         return new JsonResponse([
  186.             'success' => (bool)$items,
  187.             'html' => $compiled->getContent()
  188.         ]);
  189.     }
  190.     /**
  191.      * @Route(
  192.      *     "/{_locale}/cart/api/v1/catering-performances/",
  193.      *     name="get-api-catering-performances",
  194.      *     defaults={
  195.      *          "_locale": "fi"
  196.      *     },
  197.      *     requirements={
  198.      *          "_locale": "fi|sv|en"
  199.      *     },
  200.      *     methods={"GET"}
  201.      * )
  202.      * @param Request $request
  203.      * @param CateringService $cateringService
  204.      *
  205.      * @return JsonResponse
  206.      * @internal param CartService $cart
  207.      */
  208.     public function getPerformancesAction(Request $requestCateringService $cateringService)
  209.     {
  210.         $date $request->get('date');
  211.         $timestamp strtotime($date);
  212.         $startDate = new \DateTime();
  213.         $startDate->setTimestamp($timestamp);
  214.         $endDate = new \DateTime();
  215.         $endDate->setTimestamp(strtotime('+1 day'$timestamp));
  216.         /** @var ItemCateringItemPerformance[] $performances */
  217.         $performances $cateringService->getPerformancesCustom($startDate$endDate);
  218.         $performances array_values(
  219.                 array_filter($performances, function ($performance) {
  220.                     return !$performance->isOnnenpaikat();
  221.                 })
  222.         );
  223.         if (!$performances) {
  224.             return new JsonResponse([
  225.                 'success' => false,
  226.                 'message' => $this->translator->trans('catering_no_performances')
  227.             ]);
  228.         }
  229.         return new JsonResponse([
  230.             'success' => true,
  231.             'performances' => $this->serializer->normalize($performancesnull, ['groups' => ['json']])
  232.         ]);
  233.     }
  234.     /**
  235.      * @Route(
  236.      *     "/{_locale}/cart/api/v1/catering-guests/",
  237.      *     name="get-api-catering-guests",
  238.      *     defaults={
  239.      *          "_locale": "fi"
  240.      *     },
  241.      *     requirements={
  242.      *          "_locale": "fi|sv|en"
  243.      *     },
  244.      *     methods={"GET"}
  245.      * )
  246.      * @param Request $request
  247.      * @param CateringService $cateringService
  248.      *
  249.      * @return JsonResponse
  250.      * @throws TessituraClientException
  251.      */
  252.     public function getGuestsAction(Request $requestCateringService $cateringService)
  253.     {
  254.         $statuses json_decode($request->get('statuses'));
  255.         $message false;
  256.         try {
  257.             $validStatuses $this->params->get('tessitura.catering_valid_performance_statuses');
  258.         } catch (\InvalidArgumentException $exception) {
  259.             $validStatuses = [];
  260.         }
  261.         $duplicatesRemoved array_unique((array)$statuses);
  262.         $uniqueStatus reset($duplicatesRemoved);
  263.         if (count($duplicatesRemoved) === && !in_array($uniqueStatus$validStatuses)) {
  264.             $message $cateringService->getCorrespondingCateringMessage($uniqueStatus);
  265.         }
  266.         if ($message) {
  267.             return new JsonResponse([
  268.                 'success' => false,
  269.                 'message' => $message
  270.             ]);
  271.         }
  272.         $count $cateringService->getMaxBookingAvailable();
  273.         return new JsonResponse([
  274.             'success' => true,
  275.             'statuses' => json_encode($statuses),
  276.             'available' => $count
  277.         ]);
  278.     }
  279.     /**
  280.      * @Route(
  281.      *     "/{_locale}/cart/api/v1/catering-add/",
  282.      *     name="post-api-catering-add",
  283.      *     defaults={
  284.      *          "_locale": "fi"
  285.      *     },
  286.      *     requirements={
  287.      *          "_locale": "fi|sv|en"
  288.      *     },
  289.      *     methods={"POST"}
  290.      * )
  291.      * @param Request $request
  292.      * @param CartService $cartService
  293.      *
  294.      * @return JsonResponse
  295.      * @internal param CartService $cart
  296.      */
  297.     public function addToCartAction(Request $requestCartService $cartService)
  298.     {
  299.         $priceType = (int)$this->params->get('tessitura.catering_default_pricetype');
  300.         $performanceId = (int)$request->get('performanceId');
  301.         $count = (int)$request->get('count');
  302.         $bookingPerformanceId = (int)$request->get('bookingPerformanceId');
  303.         $bookingGuests = (int)$request->get('bookingGuests');
  304.         $successMessage "";
  305.         $collectItemsCountTotal 0;
  306.         $collectItemsCount 0;
  307.         try {
  308.             $cartService->evaluateFundsInCart(CartItemCatering::class);
  309.         } catch (AddNotAvailableException $exception) {
  310.             return new JsonResponse([
  311.                 'success' => false,
  312.                 'message' => $this->translator->trans('fund_in_cart_error')
  313.             ]);
  314.         }
  315.         // If both vars are true -> This add to cart item shouldn't be collect item
  316.         if ($bookingPerformanceId && $bookingGuests) {
  317.             try {
  318.                 $cartService->reserveCateringTableBooking($bookingPerformanceId$bookingGuests$priceType);
  319.                 $successMessage '';
  320.             } catch (CateringServiceException $exception) {
  321.                 switch ($exception->getCode()) {
  322.                     case CateringServiceException::TABLE_NOT_BOOKABLE:
  323.                         return new JsonResponse([
  324.                             'success' => false,
  325.                             'message' => $this->translator->trans('catering_booking_failure')
  326.                         ]);
  327.                         break;
  328.                     case CateringServiceException::TABLE_ALL_READY_BOOKED:
  329.                         // Not a problem
  330.                         $successMessage '';
  331.                         break;
  332.                 }
  333.             }
  334.         } else {
  335.             $collectItemsCount $count;
  336.         }
  337.         // Calculate all collect items in cart
  338.         $collectItemsCountTotal array_reduce($cartService->getCateringProducts(), function ($carry$item) use ($performanceId) {
  339.             if ((int)$item->getRelatedCartPerformance()->getType()->getId() === $this->params->get('tessitura.performance_type_catering_collect')) {
  340.                 $carry++;
  341.             }
  342.             return $carry;
  343.         }, 0);
  344.         // Add just added to the total count (when dealing with collect items)
  345.         $collectItemsCountTotal += (int)$collectItemsCount;
  346.         if ((int)$collectItemsCountTotal $this->params->get('tessitura.catering_collect_max_booking')) {
  347.             return new JsonResponse([
  348.                 'success' => false,
  349.                 'message' => $this->translator->trans('catering_collect_too_many_failure')
  350.             ]);
  351.         }
  352.         try {
  353.             $cartService->reserveCatering($performanceId$count$priceType);
  354.         } catch (TessituraClientException $exception) {
  355.             return new JsonResponse([
  356.                 'success' => false,
  357.                 'message' => $this->translator->trans('catering_cart_add_failure')
  358.             ]);
  359.         }
  360.         return new JsonResponse([
  361.             'success' => true,
  362.             'message' => $successMessage
  363.         ]);
  364.     }
  365. }