src/Controller/CalendarController.php line 97

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use AF\AdminBundle\Enum\WeekDaysEnum;
  4. use AF\PHDatabase\Entity\PriceListCommodity;
  5. use App\Repository\App\ConfigurationRepository;
  6. use App\Repository\App\OfferTransRepository;
  7. use App\Service\EventService;
  8. use App\Service\PriceService;
  9. use App\Service\SlackNotify;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. /**
  15.  * Obsługa cenników
  16.  *
  17.  * Zwraca najniższą kwotę dla standardu, który definiuje się w CMS (według założeń jest to DBL – najtańszy standard).
  18.  *
  19.  * @see PriceService Serwis wyliczający ceny per dzień
  20.  */
  21. class CalendarController extends AbstractController
  22. {
  23.     public function __construct(private readonly OfferTransRepository $offerTransRepository,
  24.                                 private readonly EventService $eventService,
  25.                                 private readonly ConfigurationRepository $configurationRepository)
  26.     {
  27.     }
  28.     /**
  29.      * Zwraca najniższą kwotę per dzień
  30.      *
  31.      * Standard, dla którego będą pobierane cenniki definiuje się w CMS.
  32.      * Cenniki nie zostaną zwrócone, jeżeli standard nie zostanie prawidłowo zdefiniowany.
  33.      *
  34.      * @api {get} /api/v1/public/calendar/prices Cennik
  35.      * @apiDescription
  36.      *  <p> Endpoint domyślnie zwraca kwotę per dzień, licząc od dnia aktualnego, aż do ostatniego dnia, dla którego
  37.      *      został uzupełniony cennik. Istnieje możliwość przekazania własnego zakresu za pomocą parametrów
  38.      *      "start" i "end" w formacie Y-m-d, np. api/v1/public/calendar/prices?start=2020-01-01&end=2021-01-01) </p>
  39.      *  <p> Waluta kwoty to PLN, bez możliwości jej zmiany. </p>
  40.      *
  41.      *  <b>Uwaga!</b>
  42.      *  <p> Endpoint działa prawidłowo tylko wtedy, gdy zostały spełnione następujące warunki: </p>
  43.      *  <ol>
  44.      *      <li> Istnieje pokój, który został przypisany do shuum'owego standardu pokoju i ma aktywną flagę "Cena kalendarzowa" </li>
  45.      *      <li> Wybrany standard pokoju ma uzupełniony cennik w SPAsofcie i zaznaczoną opcję "DostepPrzezWWW" w tabeli shuum.Cenniki </li>
  46.      * </ol>
  47.      * @apiSampleRequest /api/v1/public/calendar/prices
  48.      * @apiVersion 1.0.0
  49.      * @apiGroup Kalendarz
  50.      * @apiParam {String} start Początek zakresu
  51.      * @apiParam {String} end Koniec zakresu
  52.      * @apiSuccess {Array} meta Dodatkowe informacje
  53.      * @apiSuccess {String} meta.maxDate Maksymalny dostępny zakres
  54.      * @apiSuccess {Array} results Dostępne kwoty, zobacz przykład Success 200
  55.      * @apiError {Array} messages Opisy błędów
  56.      * @apiExample Success 200
  57.      *  HTTP/1.1 200 OK
  58.      *  {
  59.      *      "meta": {
  60.      *          "maxDate": "2021-01-03"
  61.      *      },
  62.      *      "results": {
  63.      *          "2020": {
  64.      *              "12": {
  65.      *                  "30": "900.00",
  66.      *                  "31": "900.00"
  67.      *              }
  68.      *          },
  69.      *          "2021": {
  70.      *              "1": {
  71.      *                  "1": "900.00"
  72.      *              }
  73.      *          }
  74.      *      }
  75.      *  }
  76.      *
  77.      * @apiExample Error 400
  78.      * HTTP/1.1 400 Bad Request
  79.      *  {
  80.      *      "messages": [
  81.      *          "Brak pokoju odpowiadającego za ceny kalendarzowe."
  82.      *      ]
  83.      *  }
  84.      *
  85.      * @param Request $request Żądanie wysyłane do serwera
  86.      * @param ManagerRegistry $registry Obsługa baz danych
  87.      * @param PriceService $priceService Pobierania cenników
  88.      * @param SlackNotify $slackNotify Wysyłka notyfikacja na slacka
  89.      * @return JsonResponse
  90.      * @throws \Exception
  91.      */
  92.     public function calendarPrices(
  93.         Request $request,
  94.         ManagerRegistry $registry,
  95.         PriceService $priceService,
  96.         SlackNotify $slackNotify
  97.     ): JsonResponse
  98.     {
  99.         $config $registry->getRepository('App\Entity\App\Configuration')->findOneByName('booking_calendar_price');
  100.         if ( $config === null || $config->getValue() === null ) {
  101.             $slackNotify->send(null'[Kalendarz - cennik] Brak standardu''W CMS nie został 
  102.                 zdefiniowany standard, którego cennik miałby się pokazywać na kalendarzu.');
  103.             return new JsonResponse([
  104.                 'messages' => [
  105.                     'Wystąpił błąd podczas pobierania danych. Prosimy spróbować ponownie za chwilę.'
  106.                 ]
  107.             ], 400);
  108.         }
  109.         if ( $request->query->has('start') ) {
  110.             $dateStart = new \DateTime($request->query->get('start'));
  111.         } else {
  112.             $dateStart = new \DateTime();
  113.         }
  114.         if ( $request->query->has('end') ) {
  115.             $dateEnd = new \DateTime($request->query->get('end'));
  116.         } else {
  117.             $dateEnd null;
  118.         }
  119.         $prices $registry->getRepository(PriceListCommodity::class)->getLowestPrice(
  120.             $dateStart,
  121.             $dateEnd,
  122.             $config->getValue()
  123.         );
  124.         $results $priceService->getCalendarPrice($dateStart$dateEnd$prices);
  125.         $configs $this->configurationRepository->getBookingConfig(['booking_score_hotel''description_engine_standard']);
  126.         $maxDate $registry->getRepository(PriceListCommodity::class)->getMaxDate($config->getValue());
  127.         if ( $maxDate !== null ) {
  128.             $maxDate $maxDate->getPriceList()->getAvailableUntil()->format('Y-m-d');
  129.         }
  130.         $offerResponse = [];
  131.         if($request->query->has('slug'))
  132.         {
  133.             $slug $request->query->get('slug');
  134.             $offerTrans $this->offerTransRepository->getOfferBySlug($slug);
  135.             if($offerTrans) {
  136.                 $offer $offerTrans->getOffer();
  137.                 $offerResponse['offerId'] = $offer->getId();
  138.                 $offerResponse['minDate'] = $offer->getArrivalPeriodFrom()->format('Y-m-d');
  139.                 $offerResponse['maxDate'] = $offer->getArrivalPeriodTo()->format('Y-m-d');
  140.                 $offerResponse['minDays'] = $offer->getMinDaysNumber();
  141.                 $offerResponse['maxDays'] = $offer->getMaxDaysNumber();
  142.                 $offerResponse['minGuests'] = $offer->getMinGuestsNumber();
  143.                 $offerResponse['maxGuests'] = $offer->getMaxGuestsNumber();
  144.                 $offerResponse['requiredDays'] = WeekDaysEnum::getCalendarKeysFromRequiredDays($offer->getWeekDays());
  145.                 $offerResponse['requiredStartingDay'] = WeekDaysEnum::getCalendarKeysFromRequiredDays($offer->getWeekDaysStart());
  146.                 $offerResponse['arrivalFrom'] = $offer->getArrivalFrom() ? $offer->getArrivalFrom()->format('Y-m-d') : null;
  147.                 $offerResponse['arrivalTo'] = $offer->getArrivalFrom() ? $offer->getArrivalTo()->format('Y-m-d') : null;
  148.                 $offerResponse['excludes'] = $offer->getExcludes() ? json_decode($offer->getExcludes(), true) : [];
  149.                 $offerResponse['header'] = $offerTrans->getName();
  150.                 $offerResponse['description'] = $offerTrans->getOfferDescription();
  151.             }
  152.             else
  153.             {
  154.                 $offerResponse null;
  155.             }
  156.         }
  157.         $results $this->eventService->applyEventsIntoDates($results$this->eventService->getBookingEvents($maxDate));
  158.         return new JsonResponse([
  159.             'meta' => [
  160.                 'maxDate' => $maxDate,
  161.                 'bookingPercent' => $configs['booking_score_hotel'] ? (int) $configs['booking_score_hotel']->getValue() : '',
  162.                 'descriptionEngineStandard' => $configs['description_engine_standard'] ? $configs['description_engine_standard']->getValue() : ''
  163.             ],
  164.             'results' => $results,
  165.             'offer' => !empty($offerResponse) ? $offerResponse null
  166.         ]);
  167.     }
  168. }